反射(Reflection),是指Java程序具有在运行期分析类以及修改其本身状态或行为的能力。 通俗点说 就是 通过反射我们可以动态地获取一个类的所有属性和方法,还可以操作这些方法和属性.
package com.study.rejection;
public class ReflectionTest1 {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
//使用反射操作对象需要用到编译之后的Class 对象
User user = new User();
Class<?> c = user.getClass();
System.out.println(c);
Class<?> c2 = User.class;
System.out.println(c2);
Class<?> c3 = Class.forName("com.study.rejection.User");
System.out.println(c3);
Object obj = c3.newInstance(); //调用类的无参构造器
System.out.println(obj);
}
}
属性的操作
package com.study.rejection;
import java.lang.reflect.Field;
public class ReflectionTest1 {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException {
//使用反射操作对象需要用到编译之后的Class 对象
Class<?> c3 = Class.forName("com.study.rejection.User");
System.out.println(c3);
Object obj = c3.newInstance(); //调用类的无参构造器
System.out.println(obj);
//获取属性
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field:fields) {
System.out.println(field.getName() +"========"+field.getType());
}
Field field = obj.getClass().getDeclaredField("id");
System.out.println(field.getName() +"----"+field.getType());
field.setAccessible(true); //接触访问限制,不安全
field.set(obj,"1"); //设置值
System.out.println(obj);
Object id = field.get(obj);
System.out.println(id);
}
}
方法的操作
package com.study.rejection;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectionTest2 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
Class<?> c3 = Class.forName("com.study.rejection.User");
Method[] methods = c3.getDeclaredMethods();
for (Method m : methods) {
System.out.println(m.getName());
Class<?>[] ps = m.getParameterTypes();
for (Class c : ps) {
System.out.println(c);
}
}
Object obj = c3.newInstance();;
Method method = c3.getDeclaredMethod("setId", String.class);
method.invoke(obj,"2"); //这样就可以不使用setAccessible
Method getMethod = c3.getDeclaredMethod("getId");
Object res = getMethod.invoke(obj);;
System.out.println(obj);
System.out.println(res);
}
}
泛型的实质
package com.study.rejection;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class ReflectionTest3 {
public static void main(String[] args) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
//通过反射来看泛型的实质
//泛型是在编译之后被丢弃,那么不同类型的泛型信息保存在局部变量表中,等到
//程序真正运行的时候 通过反射 调用其对应的方法就可以将被擦除的泛型信息
//重新加载到内存中
List<String> stringList = new ArrayList<>();
stringList.add("HHH");
stringList.add("ww123");
Class<?> c = stringList.getClass(); //获取Class 对象
Method method = c.getDeclaredMethod("add",Object.class); //获取方法
method.invoke(stringList,123); //使用方法往数组中添加不同类型的数据
System.out.println(stringList);
}
}
构造器
package com.study.rejection;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
public class ReflectionTest4 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Class<?> c = Class.forName("com.study.rejection.User");
Constructor constructor = c.getConstructor();//获取无参构造器
Object obj = constructor.newInstance();
System.out.println(obj);
Constructor constructor2 = c.getConstructor(String.class,String.class,Integer.class, List.class);//获取有参构造器
List<String> hobbies = new ArrayList<String>();
hobbies.add("basketball");
Object obj2 = constructor2.newInstance("1","HHA",18,hobbies);
System.out.println(obj2);
}
}