Reflection mechanism---Java basic core part



Reflection mechanism-basic core part of java

The reflection in java can dynamically obtain various detailed information of the class when the program is running, including member properties, member methods, and construction methods. In order to manipulate the properties and methods of the class or, it is the soul of the framework

1. Get the Class class of the object

The JVM loads the bytecode file of the class into memory through the class loader and then generates a corresponding Class object. After obtaining the Class object, various operations can be performed. There are three ways to obtain the Class object.

  1. Through the Class.forName() method
  2. Get through the class.class member
  3. Get through the object.getClass()
		// method 1
        Class objectClass1 = Class.forName("java.lang.Object");
        // method 2
        Class<Object> objectClass2 = Object.class;
        //method 3
        Class<? extends Object> objectClass3 = new Object().getClass();

And it should be noted that no matter what method is used to obtain the Class object of the same class, the same object is obtained.

 		System.out.println(objectClass1 == objectClass2);//true
        System.out.println(objectClass1 == objectClass3);//true
        System.out.println(objectClass2 == objectClass3);//true

2. Get the member object of the class

There are four ways to get the member objects of the class

  1. getField(): Only public attributes can be obtained
  2. getFields(): Get an object array of all public objects
  3. getDeclaredField(): You can get any attribute, but you can only operate on public
  4. getDeclaredFields(): You can get any attribute, but you can only operate on public
		Person person = new Person(1, 2, 3, 4);
        Class<? extends Person> personClass = person.getClass();
        //只能获取public属性
        Field variable1 = personClass.getField("variable1");
        //获取所有public对象的对象数组
        Field[] fields = personClass.getFields();
        //通过set方法设置某个Person对象的具体值
        variable1.set(person, 1);
        //通过get可以获取某个Person对象的具体值
        Integer value1 = (Integer) variable1.get(person);

        //可以获取任意属性,但是只能对public进行操作
        Field variable4 = personClass.getDeclaredField("variable4");
        Field[] declaredFields = personClass.getDeclaredFields();
        //通过setAccessible方法突破限制,暴力反射
        variable4.setAccessible(true);
        Integer value4 = (Integer) variable4.get(person);

3. Get the member method of the class

Similar to obtaining the member object of the class, there are four methods to obtain the member method object of the class

  1. getMethod(): Get public member method object
  2. getMethods(): Get an array of public member method objects
  3. getDeclaredMethod(): You can get any member method object, but you can only operate on public
  4. getDeclaredMethods(): You can get any array of member method objects, but you can only operate on public
 		 Person person = new Person(1, 2, 3, 4);
        Class<? extends Person> personClass = person.getClass();

        Method[] methods = personClass.getMethods();
        //通过方法的名字和参数的Class类列表来获取方法对象
        Method setVariable1 = personClass.getMethod("setVariable1", Integer.class);
        //通过invoke方法来调用该方法.参数是调用该方法的对象,以及方法的参数列表.结果就是返回值
        Void result1= (Void) setVariable1.invoke(person, 2);

        Method[] declaredMethods = personClass.getDeclaredMethods();
        Method privateMethod = personClass.getDeclaredMethod("privateMethod", String.class);
        //同样可以通过setAccessible来突破限制,暴力反射
        privateMethod.setAccessible(true);
        String result2= (String) privateMethod.invoke(person, "MarkLau");

4. Get the constructor object of the class

There are four ways to get the constructor object of the class. If it is too similar, I won't introduce it.

  1. getConstructor();
  2. getConstructors();
  3. getDeclaredConstructor();
  4. getDeclaredConstructors();
		 Person person = new Person();
        Class<? extends Person> personClass = person.getClass();

        //由于所有的构造方法名字都相同所以不需要方法名,只需要通过参数就可以进行判断
        Constructor<? extends Person> constructor = personClass.getConstructor(Integer.class, Integer.class, Integer.class, Integer.class);
        //调用newInstance方法,再将参数传入就可以生成一个新的对象
        Person person1 = constructor.newInstance(1, 2, 3, 4);
        //通过Class对象来创建的对象只能使用无参构造
        Person person2 = personClass.newInstance();

Guess you like

Origin blog.csdn.net/qq_44823898/article/details/111086118