反射学习小结 (初学者自学总结) 学习理由: 框架底层重要的实现原理之一(反射)

学习理由:

框架底层重要的实现原理之一(反射)

反射学习小结
(初学者自学总结)
学习理由:
框架底层重要的实现原理之一(反射)

反射就是在"java运行系统"时期 到"类加载器验证字节码的正确性"

  即运行期间动态的操作类加载器的字节码文件从而获得整个类文件。

1.获取一个类的class对象的方式

eg:获取Person实体类

  ①:通过.class获取:System.out.println(Person.class);

  ②:通过class对象中forName()获取:Class<?> aClass = Class.forName("deep.entity.Person");

  ③:通过类的实例获取:Person person = new Person(); System.out.println(person.getClass());

2、主要方法使用练习

  我们有一个BookEntity实体类,通过反射来加载这个类

  我封装了四个方法,分别打印类信息、属性信息、方法信息、构造器信息

 

 ①:获取类的信息:

    

 1 private static void getClassInfo(){
 2 
 3      //获取class对象
 4 
 5        Class<BookEntity> clazz = BookEntity.class;
 6 
 7      System.out.println("类的全名称:" + clazz.getName());
 8 
 9        System.out.println("类的简单类名:" + clazz.getSimpleName());
10 
11      System.out.println("类的修饰符:" + Modifier.toString(clazz.getModifiers()));
12 
13    }

反射学习小结
(初学者自学总结)
学习理由:
框架底层重要的实现原理之一(反射)

 

②:获取类中所有属性的信息:

    

private static void getClassFieldInfo() {

    Class<BookEntity> clazz = BookEntity.class;

    Object instance = clazz.newIntance();

    Field bookNameField = clazz.getDeclareField("bookName");

    //bookName字段是私有的需要设置字段访问权限

    bookNameField.setAccessible(true);

    //调用字段对应set方法

    bookNameField.set(instance,"java");

    //获取刚刚设置到字段中的值

    Object value = bookNameField.get(instance);

    System.out.println(value);

  }

 反射学习小结
(初学者自学总结)
学习理由:
框架底层重要的实现原理之一(反射)

附上:set方法的源码/get方法的源码

  

 1  @CallerSensitive
 2     public void set(Object obj, Object value)
 3         throws IllegalArgumentException, IllegalAccessException
 4     {
 5         if (!override) {
 6             if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
 7                 Class<?> caller = Reflection.getCallerClass();
 8                 checkAccess(caller, clazz, obj, modifiers);
 9             }
10         }
11         getFieldAccessor(obj).set(obj, value);
12     }
 1  @CallerSensitive
 2     public Object get(Object obj)
 3         throws IllegalArgumentException, IllegalAccessException
 4     {
 5         if (!override) {
 6             if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
 7                 Class<?> caller = Reflection.getCallerClass();
 8                 checkAccess(caller, clazz, obj, modifiers);
 9             }
10         }
11         return getFieldAccessor(obj).get(obj);
12     }
13 
14  

③:获取方法的所有信息:

 1 /**
 2      * 获取方法的所有信息
 3      * @throws SecurityException 
 4      * @throws NoSuchMethodException 
 5      * @throws IllegalAccessException 
 6      * @throws InstantiationException 
 7      * @throws InvocationTargetException 
 8      * @throws IllegalArgumentException 
 9      */
10     private static void getClassForMethodInfo() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
11         // TODO Auto-generated method stub
12         
13         //获取Class对象
14         Class<BookEntity> clazz = BookEntity.class;
15         
16         //获取所有public修饰的方法
17         Method[] methods = clazz.getMethods();
18         for(Method method : methods){
19             System.out.println(method);
20             System.out.println("访问修饰符:" + Modifier.toString(method.getModifiers()));
21             System.out.println("返回类型名称:" + method.getReturnType().getSimpleName());
22             System.out.println("方法名称:" + method.getName());
23             System.out.println("参数类型:" + Arrays.toString(method.getParameterTypes()));
24             System.out.println("参数个数:" + method.getParameterCount() );
25             System.out.println("----------------------------------------------");
26         }
27         
28         //通过反射实例化一个对象
29         BookEntity instance = clazz.newInstance();
30         
31         //获取单个方法
32         Method set = clazz.getDeclaredMethod("setBookPrice",Double.class);
33         //开启方法访问权限(如果访问修饰符非public)
34         set.invoke(instance,45.7);
35         
36         //再获取get方法来获取刚刚设置的值
37         Method get = clazz.getDeclaredMethod("getBookPrice");
38         
39         System.out.println(get.invoke(instance));
40         
41         
42         //获取当前类中所有定义的方法
43         methods = clazz.getDeclaredMethods();
44         for(Method method : methods){
45             System.out.println(method);
46         }
47     }
  1 public java.lang.String deep.entity.BookEntity.getBookName()
  2 访问修饰符:public
  3 返回类型名称:String
  4 方法名称:getBookName
  5 参数类型:[]
  6 参数个数:0
  7 ----------------------------------------------
  8 public java.lang.String deep.entity.BookEntity.getBookAuthor()
  9 访问修饰符:public
 10 返回类型名称:String
 11 方法名称:getBookAuthor
 12 参数类型:[]
 13 参数个数:0
 14 ----------------------------------------------
 15 public java.lang.Integer deep.entity.BookEntity.getBookId()
 16 访问修饰符:public
 17 返回类型名称:Integer
 18 方法名称:getBookId
 19 参数类型:[]
 20 参数个数:0
 21 ----------------------------------------------
 22 public java.lang.Double deep.entity.BookEntity.getBookPrice()
 23 访问修饰符:public
 24 返回类型名称:Double
 25 方法名称:getBookPrice
 26 参数类型:[]
 27 参数个数:0
 28 ----------------------------------------------
 29 public void deep.entity.BookEntity.setBookPrice(java.lang.Double)
 30 访问修饰符:public
 31 返回类型名称:void
 32 方法名称:setBookPrice
 33 参数类型:[class java.lang.Double]
 34 参数个数:1
 35 ----------------------------------------------
 36 public void deep.entity.BookEntity.setBookAuthor(java.lang.String)
 37 访问修饰符:public
 38 返回类型名称:void
 39 方法名称:setBookAuthor
 40 参数类型:[class java.lang.String]
 41 参数个数:1
 42 ----------------------------------------------
 43 public void deep.entity.BookEntity.setBookName(java.lang.String)
 44 访问修饰符:public
 45 返回类型名称:void
 46 方法名称:setBookName
 47 参数类型:[class java.lang.String]
 48 参数个数:1
 49 ----------------------------------------------
 50 public void deep.entity.BookEntity.setBookId(java.lang.Integer)
 51 访问修饰符:public
 52 返回类型名称:void
 53 方法名称:setBookId
 54 参数类型:[class java.lang.Integer]
 55 参数个数:1
 56 ----------------------------------------------
 57 public final void java.lang.Object.wait() throws java.lang.InterruptedException
 58 访问修饰符:public final
 59 返回类型名称:void
 60 方法名称:wait
 61 参数类型:[]
 62 参数个数:0
 63 ----------------------------------------------
 64 public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
 65 访问修饰符:public final
 66 返回类型名称:void
 67 方法名称:wait
 68 参数类型:[long, int]
 69 参数个数:2
 70 ----------------------------------------------
 71 public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
 72 访问修饰符:public final native
 73 返回类型名称:void
 74 方法名称:wait
 75 参数类型:[long]
 76 参数个数:1
 77 ----------------------------------------------
 78 public boolean java.lang.Object.equals(java.lang.Object)
 79 访问修饰符:public
 80 返回类型名称:boolean
 81 方法名称:equals
 82 参数类型:[class java.lang.Object]
 83 参数个数:1
 84 ----------------------------------------------
 85 public java.lang.String java.lang.Object.toString()
 86 访问修饰符:public
 87 返回类型名称:String
 88 方法名称:toString
 89 参数类型:[]
 90 参数个数:0
 91 ----------------------------------------------
 92 public native int java.lang.Object.hashCode()
 93 访问修饰符:public native
 94 返回类型名称:int
 95 方法名称:hashCode
 96 参数类型:[]
 97 参数个数:0
 98 ----------------------------------------------
 99 public final native java.lang.Class java.lang.Object.getClass()
100 访问修饰符:public final native
101 返回类型名称:Class
102 方法名称:getClass
103 参数类型:[]
104 参数个数:0
105 ----------------------------------------------
106 public final native void java.lang.Object.notify()
107 访问修饰符:public final native
108 返回类型名称:void
109 方法名称:notify
110 参数类型:[]
111 参数个数:0
112 ----------------------------------------------
113 public final native void java.lang.Object.notifyAll()
114 访问修饰符:public final native
115 返回类型名称:void
116 方法名称:notifyAll
117 参数类型:[]
118 参数个数:0
119 ----------------------------------------------
120 45.7
121 public java.lang.String deep.entity.BookEntity.getBookName()
122 public java.lang.String deep.entity.BookEntity.getBookAuthor()
123 public java.lang.Integer deep.entity.BookEntity.getBookId()
124 public java.lang.Double deep.entity.BookEntity.getBookPrice()
125 public void deep.entity.BookEntity.setBookPrice(java.lang.Double)
126 public void deep.entity.BookEntity.setBookAuthor(java.lang.String)
127 public void deep.entity.BookEntity.setBookName(java.lang.String)
128 public void deep.entity.BookEntity.setBookId(java.lang.Integer)

④获取构造器信息:

 1 /**
 2      * 获取构造器信息
 3      * @throws SecurityException 
 4      * @throws NoSuchMethodException 
 5      * @throws InvocationTargetException 
 6      * @throws IllegalArgumentException 
 7      * @throws IllegalAccessException 
 8      * @throws InstantiationException 
 9      */
10     private static void getClassForConstructor() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
11         // TODO Auto-generated method stub
12         //获取class对象
13         Class<BookEntity> clazz = BookEntity.class;
14         //获取所有public修饰的函数
15         Constructor<?>[] constructors = clazz.getConstructors();
16         
17         for(Constructor constructor : constructors){
18             System.out.println(constructor);
19         }
20         
21         System.out.println("------------------------------------------");
22         
23         //湖区类中所有定义的构造函数
24         constructors = clazz.getDeclaredConstructors();
25         for(Constructor constructor : constructors){
26             System.out.println(constructor);
27             System.out.println("访问修饰符:" + Modifier.toString(constructor.getModifiers()));
28             System.out.println("构造器名称:" + constructor.getName());
29             System.out.println("参数列表:" + Arrays.toString(constructor.getParameterTypes()));
30             System.out.println("----------------------------------------");
31         }
32         
33         //调用指定构造函数
34         Constructor<BookEntity> constructor = clazz.getDeclaredConstructor(int.class,String.class);
35         constructor.setAccessible(true);
36         BookEntity instance = constructor.newInstance(100,"三国演义");
37         System.out.println(instance);
38         System.out.println(instance.getBookName());
39         System.out.println(instance.getBookAuthor());
40         System.out.println(instance.getBookId());
41         System.out.println(instance.getBookPrice());
42     }

 反射学习小结
(初学者自学总结)
学习理由:
框架底层重要的实现原理之一(反射)