Java——反射
一、反射概述
Java反射机制:指在运行时去获取一个类的变量和方法信息,然后通过获取到的信息来创建对象、调用方法的一种机制。由于这种动态性,可以极大的增强程序的灵活性,程序不用在编译器就完成确定,在运行期仍然可以扩展
二、获取Class类的对象
想要通过反射使用一个类,首先需要获取到该类的字节码文件对象,也就是类型为Class的对象
获取Class类型的对象:
- 使用类的class属性来获取该类对应的Class对象。 举例:Student.class将会返回Student类对应的Class对象
- 调用对象的getClass()方法,返回该对象所属类对应的Class对象
该方法是Object类中的方法,所有的java对象都可以调用该方法 - 使用Class类的静态方法forName(String className),该方法需要传入字符串参数,该字符串参数的值时某个类的全路径,也就是完整包名的路径
Student类
public class Student {
//成员变量:一个私有、一个默认、一个公共
private String name;
int age;
public String address;
//构造方法:一个私有,一个默认,两个公共
public Student() {
}
private Student(String name) {
this.name = name;
}
Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
//成员方法:一个私有,四个公共
private void function() {
System.out.println("function");
}
public void method1() {
System.out.println("method");
}
public void method2(String s) {
System.out.println("method:" + s);
}
public String method3(String s, int i) {
return s + "," + i;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
测试类
public class ReflectDemo {
public static void main(String[] args) throws ClassNotFoundException {
//使用类的class属性来获取该类对应的Class对象
Class<Student> c1 = Student.class;
System.out.println(c1);
Class<Student> c2 = Student.class;
System.out.println(c2);
System.out.println("----------------------------");
//调用对象的getClass()方法,返回该对象所属类对应的Class对象
Student s = new Student();
Class<? extends Student> c3 = s.getClass();
System.out.println(c3);
System.out.println("----------------------------");
//使用Class类的静态方法forName(String className),
//该方法需要传入字符串参数,该字符串参数的值时某个类的全路径,也就是完整包名的路径
Class<?> c4 = Class.forName("Demo1.Student");
System.out.println(c4);
}
}
三、反射获取构造方法
1、Class类中用于获取构造方法的方法:
- Constructor<?>[] getConstructors():返回所有公共构造方法对象的数组
- Constructor<?>[] getDeclaredConstructors():返回所有构造方法对象的数组
- Constructor< T > getConstructor(Class<?>…parameterTypes):返回单个构造方法对象
- Constructor< T > getDeclaredConstructor(Class<?>…parameterTypes):返回单个构造方法对象
2、Constructor类中用于创建对象的方法:
- T newInstance(Object…initargs):根据指定的构造方法创建对象
3、代码演示
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class ReflectDemo2 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
//获取Class对象
Class<?> c = Class.forName("Demo1.Student");
//Constructor<?>[] getConstructors():返回所有公共构造方法对象的数组
Constructor<?>[] cons = c.getConstructors();
for (Constructor con : cons) {
System.out.println(con);
}
System.out.println("--------------------");
//Constructor<?>[] getDeclaredConstructors():返回所有构造方法对象的数组
Constructor<?>[] cons2 = c.getDeclaredConstructors();
for (Constructor con : cons2) {
System.out.println(con);
}
System.out.println("--------------------");
//Constructor< T > getConstructor(Class<?>...parameterTypes):返回单个构造方法对象
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance();
System.out.println(obj);
System.out.println("--------------------");
//Constructor< T > getDeclaredConstructor(Class<?>...parameterTypes):返回单个构造方法对象
Constructor<?> con2 = c.getDeclaredConstructor();
Object obj2 = con2.newInstance();
System.out.println(obj2);
}
}
4、练习1
通过反射实现如下操作:
- Student s = new Student(“林青霞”,30,“西安”);
- System.out.println(s);
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class ReflectTest {
public static void main(String[] args) throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
Class<?> c = Class.forName("Demo1.Student");
Constructor<?> con = c.getConstructor(String.class, int.class, String.class);
Object obj = con.newInstance("林青霞", 30, "西安");
System.out.println(obj);
}
}
注意:基本数据类型也可以通过.class得到对应的Class类型
5、练习2
通过反射实现如下操作:
- Student s = new Student(“林青霞”);
- System.out.println(s);
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class ReflectTest2 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Class<?> c = Class.forName("Demo1.Student");
Constructor<?> con = c.getDeclaredConstructor(String.class);
//暴力反射
//取消访问检查
con.setAccessible(true);
Object obj = con.newInstance("林青霞");
System.out.println(obj);
}
}
注意:使用类的私有方法时,需要进行暴力反射,取消访问检查。
public void setAccessible(boolean flag):flag值为true时,取消访问检查
四、反射获取成员变量
1、Class类中用于获取成员变量的方法:
- Field[] getFields():返回所有公共成员变量对象的数组
- Field[] getDeclaredFields():返回所有成员变量对象的数组
- Field[] getFiled(String name):返回单个公共成员变量对象
- Filed[] getDeclaredField(String name):返回单个成员变量对象
2、Field类中用于给成员变量赋值的方法:
- void set(Object obj,Object value):给obj对象的成员变量赋值为value
3、代码演示
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
public class ReflectDemo3 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Class<?> c = Class.forName("Demo1.Student");
//Field[] getFields():返回所有公共成员变量对象的数组
Field[] fields = c.getFields();
for (Field f : fields) {
System.out.println(f);
}
System.out.println("--------------");
//Field[] getDeclaredFields():返回所有成员变量对象的数组
Field[] declaredFields = c.getDeclaredFields();
for (Field df : declaredFields) {
System.out.println(df);
}
System.out.println("--------------");
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance();
//Field[] getFiled(String name):返回单个公共成员变量对象
//Filed[] getDeclaredField(String name):返回单个成员变量对象
Field addressField = c.getField("address");
addressField.set(obj, "西安");
System.out.println(obj);
}
}
4、练习
通过反射实现如下操作:
- Student s = new Student;
- s.name = “林青霞”;
- s.age = 30;
- s.address = “西安”;
- System.out.println(s);
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
public class ReflectTest3 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException {
Class<?> c = Class.forName("Demo1.Student");
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance();
Field nameField = c.getDeclaredField("name");
Field ageFiled = c.getDeclaredField("age");
Field addressField = c.getField("address");
nameField.setAccessible(true);
ageFiled.setAccessible(true);
nameField.set(obj, "林青霞");
ageFiled.set(obj, 30);
addressField.set(obj, "西安");
System.out.println(obj);
}
}
五、反射获取成员方法
1、Class类中用于获取成员方法的方法:
- Method[] getMethods():返回所有公共成员方法对象的数组,包括继承的
- Method[] getDeclaredMethods():返回所有成员方法对象的数组,不包括继承的
- Method getMethod(String name, Class<?>…parameterTypes):返回单个公共成员方法对象
- Method getDeclaredMethod(String name, Class<?>…parameterTypes):返回单个成员方法对象
2、Method类中用于调用成员方法的方法:
- Object invoke(Object obj, Object…args):调用obj对象的成员方法,参数是args,返回值是Object类型
3、代码演示
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectDemo4 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Class<?> c = Class.forName("Demo1.Student");
//Method[] getMethods():返回所有公共成员方法对象的数组,包括继承的
Method[] methods = c.getMethods();
for (Method m : methods) {
System.out.println(m);
}
System.out.println("-------------------");
//Method[] getDeclaredMethods():返回所有成员方法对象的数组,不包括继承的
Method[] methods2 = c.getDeclaredMethods();
for (Method m : methods2) {
System.out.println(m);
}
System.out.println("-------------------");
//Method getMethod(String name, Class<?>...parameterTypes):返回单个公共成员方法对象
//Method getDeclaredMethod(String name, Class<?>...parameterTypes):返回单个成员方法对象
//Object invoke(Object obj, Object...args):调用obj对象的成员方法,参数是args,返回值是Object类型
Method m = c.getMethod("method1");
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance();
m.invoke(obj);
}
}
4、练习
通过反射实现如下操作:
- Student s = new Student();
- s.method1();
- s.method2(“林青霞”);
- String ss = s.method3(“林青霞”,30);
- System.out.println(ss);
- s.function();
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectTest4 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Class<?> c = Class.forName("Demo1.Student");
//Student s = new Student();
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance();
//s.method1();
Method method1 = c.getMethod("method1");
method1.invoke(obj);
System.out.println("------------------");
//s.method2("林青霞");
Method method2 = c.getMethod("method2", String.class);
method2.invoke(obj, "林青霞");
System.out.println("------------------");
//String ss = s.method3("林青霞",30);
//System.out.println(ss);
Method method3 = c.getMethod("method3", String.class, int.class);
Object s = method3.invoke(obj, "林青霞", 30);
System.out.println(s);
System.out.println("------------------");
//s.function();
Method function = c.getDeclaredMethod("function");
function.setAccessible(true);
function.invoke(obj);
}
}
六、反射练习之越过泛型检查
有一个ArrayList< Integer>集合,想在集合中添加一个字符串数据,如何实现?
反射可以实现越过泛型检查,代码如下:
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
public class reflectTest5 {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
ArrayList<Integer> array = new ArrayList<Integer>();
array.add(10);
Class<? extends ArrayList> c = array.getClass();
Method m = c.getMethod("add", Object.class);
m.invoke(array, "hello");
m.invoke(array, "world");
System.out.println(array);
}
}
七、反射练习之运行配置文件指定内容
通过配置文件运行类中的方法
配置文件class.txt:
className=Demo2.Student
methodName=study
实现代码:
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;
public class ReflectDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
//加载数据
Properties pro = new Properties();
FileReader fr = new FileReader("Reflect\\class.txt");
pro.load(fr);
fr.close();
String className = pro.getProperty("className");
String methodName = pro.getProperty("methodName");
//通过反射来使用
Class<?> c = Class.forName(className);
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance();
Method method = c.getMethod(methodName);
method.invoke(obj);
}
}