- 反射示例
- 获取反射对象
- 源码
- 通过反射机制动态创建对象
- 通过反射调用方法
- 通过反射设置属性
- 通过反射获取泛型
- 反射操作注解(重要)
package example.reflect;
public class demo1 {
public static void main(String[] args) throws ClassNotFoundException {
//一个类只有一个class对象
Class stu = Class.forName("example.reflect.entity.Stu");
System.out.println(stu);
}
}
源码该方法返回类型为Class类,是Java反射的源头!
@CallerSensitive
public static Class> forName(String className)
throws ClassNotFoundException {
Class> caller = Reflection.getCallerClass();
return forName0(className, true, ClassLoader.getClassLoader(caller), caller);
}
通过反射机制动态创建对象
首先明确一点:jdk8以后已不推荐直接使用newInstance来构建对象
以此代替(调用的是无参构造)
ClassstuClass = Stu.class; Stu stu1 = stuClass.getDeclaredConstructor().newInstance();
完整代码
package example.reflect;
import example.reflect.entity.Stu;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectClass {
public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
Class stuClass = Stu.class;
//无参构造
Stu stu1 = stuClass.getDeclaredConstructor().newInstance();
System.out.println(stu1);
}
}
通过反射调用方法
package example.reflect;
import example.reflect.entity.Stu;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectClass {
public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
Class stuClass = Stu.class;
//无参构造
Stu stu1 = stuClass.getDeclaredConstructor().newInstance();
// System.out.println(stu1);
//通过反射调用普通方法
Method setAge = stuClass.getDeclaredMethod("setAge", int.class);
//使用invoke方法进行激活
setAge.invoke(stu1,18);
System.out.println(stu1.getAge());
}
}
通过反射设置属性
package example.reflect;
import example.reflect.entity.Stu;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectClass {
public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
Class stuClass = Stu.class;
//无参构造
Stu stu1 = stuClass.getDeclaredConstructor().newInstance();
// System.out.println(stu1);
//通过反射使用属性
Field name = stuClass.getDeclaredField("name");
//关闭安全检测
name.setAccessible(true);
name.set(stu1,"张三");
System.out.println(stu1.getName());
}
}
通过反射获取泛型
getGenericReturnType方法用于获取返回值类型
package example.reflect;
import example.reflect.entity.Stu;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
public class GetExtendType {
public void test(Map map, List list){
map.forEach((x,y)->{
System.out.println(x+y);
});
}
public static void main(String[] args) throws NoSuchMethodException {
//通过反射获取泛型
Method test = GetExtendType.class.getMethod("test", Map.class, List.class);
//获取方法参数类型
Type[] genericParameterTypes = test.getGenericParameterTypes();
for (Type genericParameterType : genericParameterTypes) {
// System.out.println(genericParameterType);
//判断是否属于参数化类型
if(genericParameterType instanceof ParameterizedType){
//获取真实的参数
Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments();
for (Type actualTypeArgument : actualTypeArguments) {
System.out.println(actualTypeArgument);
}
}
}
}
}
反射操作注解(重要)
package example.reflect;
import example.annotation.AnnoField;
import example.annotation.MyAnno;
import example.reflect.entity.Stu;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
public class GetAnnotation {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException {
Class stuClass = Stu.class;
// Stu stu = stuClass.getDeclaredConstructor().newInstance();
//通过反射获取类上的注解
Annotation[] annotations = stuClass.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
//获取注解中的值
MyAnno annotation = stuClass.getAnnotation(MyAnno.class);
String value = annotation.value();
System.out.println(value);
//获取属性上的注解
Field age = stuClass.getDeclaredField("age");
AnnoField annotation1 = age.getAnnotation(AnnoField.class);
System.out.println(annotation1.col());
System.out.println(annotation1.type());
}
}



