获取类对象

  1. // 获取类对象
  2. Class<?> clzz1 = ReflectTestEntity.class;
  3. System.out.println(clzz1.hashCode());
  4. Class<?> clzz2 = new ReflectTestEntity().getClass();
  5. System.out.println(clzz2.hashCode());
  6. Class<?> clzz3 = Class.forName("com.jyusun.origin.study.basic.反射.ReflectTestEntity");
  7. System.out.println(clzz3.hashCode());

获取实例的几种方式

  1. // 获取类对象
  2. Class<ReflectTestEntity> clazz = ReflectTestEntity.class;
  3. // 获取实例
  4. ReflectTestEntity entity1 = clazz.newInstance();
  5. System.out.println(entity1.toString());
  6. // 获取无参构造函数
  7. Constructor<ReflectTestEntity> constructor1 = clazz.getConstructor();
  8. ReflectTestEntity entity2 = constructor1.newInstance();
  9. System.out.println(entity2.toString());
  10. // 带参构造函数
  11. Constructor<ReflectTestEntity> constructor2 = clazz.getConstructor(String.class, int.class);
  12. ReflectTestEntity entity3 = constructor2.newInstance("小花", 18);
  13. System.out.println(entity3.toString());
  14. // 获取所有的构造函数
  15. Constructor<?>[] constructors = clazz.getConstructors();
  16. for (Constructor<?> constructor3 : constructors) {
  17. System.out.println("构造函数:" + constructor3.toString());
  18. }

ReflectTestEntity{name=’null’, age=’0’} ReflectTestEntity{name=’null’, age=’0’} ReflectTestEntity{name=’小花’, age=’18’}

构造函数:public com.jyusun.origin.study.basic.反射.ReflectTestEntity(java.lang.String,int)

构造函数:public com.jyusun.origin.study.basic.反射.ReflectTestEntity()

获取方法

  1. // 获取实例
  2. Class<?> clazz = ReflectTestEntity.class;
  3. // 获取对象
  4. Object entity = clazz.newInstance();
  5. // 获取类中所有公开方法 包含继承的方法
  6. Method[] methods = clazz.getMethods();
  7. // 获取本类中所有方法 不包含继承的方法
  8. Method[] declaredMethods = clazz.getDeclaredMethods();
  9. // 获取无参方法
  10. Method method = clazz.getMethod("test");
  11. method.invoke(entity);
  12. // 获取有方法
  13. Method method1 = clazz.getMethod("test",String.class);
  14. method1.invoke(entity,"测试有参");
  15. // 获取无参方法
  16. Method privateMethod3 = clazz.getDeclaredMethod("testPrivate",String.class);
  17. // 设置访问权限无效
  18. privateMethod3.setAccessible(true);
  19. privateMethod3.invoke(entity,"私有方法需要设置访问权限无效");
  20. // 获取静态方法
  21. Method staticMethod4 = clazz.getMethod("testStatic", String.class);
  22. staticMethod4.invoke(null,"静态方法Class.Method");

无参方法 有参方法:测试有参

私有 有参方法:私有方法需要设置访问权限无效

静态 有参方法:静态方法Class.Method

获取字段

  1. // 获取类对象
  2. Class<?> clazz = ReflectTestEntity.class;
  3. // 获取对象
  4. Object obj = clazz.newInstance();
  5. // 获取公开 和 父类继承的字段
  6. Field[] fields1 = clazz.getFields();
  7. // 获取本类 所有的字段(包含私有)
  8. Field[] fields2 = clazz.getDeclaredFields();
  9. // 获取单个属性操作
  10. Field nameField = clazz.getDeclaredField("name");
  11. // 设置私有化字段访问权限
  12. nameField.setAccessible(true);
  13. // Set 参数值
  14. nameField.set(obj, "测试字段反射");
  15. System.out.println(obj.toString());
  16. // Get 参数值
  17. Object nameProperty = nameField.get(obj);
  18. System.out.println(nameProperty);

ReflectTestEntity{name=’测试字段反射’, age=’0’}

测试字段反射

获取注解

  1. // 获取实例
  2. Class<?> clazz = ReflectTestEntity.class;
  3. Field field = clazz.getDeclaredField("name");
  4. TestAnnotation annotation = field.getAnnotation(TestAnnotation.class);
  5. System.out.println(annotation.value());

胖虎