Reverse reflection mechanism of Java smoking

show me the code and take to me , to do it more clear to say
GitHub project JavaHouse synchronous included
like to point a praise chant! Your support is my share of power!

Introduced

Is a reflection not follow the routine the cards at the mechanism. Usually we are in contact, "Ortho", for the time being this name. Although we use less, but in fact reflected in our daily development. Such as our common null pointer exception, or a Spring annotation configuration creates a variety of objects, and so on.

Normal operation

ReflectTest reflectTest = new ReflectTest();
reflectTest.setName("boolenbl公众号");
System.out.println(reflectTest.getName());

Generally, we use the above method to create and initialize the object attributes. And there are strict access rules. But on reflection at its opposite whom. He used the Class, Method, Field and other categories, free to create an object, access to public, private way to obtain private, shared approach.

Class class

Only private Class Constructor for class, so we can not initialize the object, but the Class class is not for us to use. Class JVM class can only be invoked. There are pictures and the truth:

Gets the Class class

There are three methods to obtain Class category. code show as below:

// 方法1
System.out.println(ReflectTest.class);
// 方法2
System.out.println(new ReflectTest().getClass());
// 方法3  包名+类名 (JDBC那种操作) 有异常
System.out.println(Class.forName("基础.ReflectTest"));
// 获取类名
System.out.println(ReflectTest.class.getName());

Acquisition method

With this method you can ignore the rules of access, access to all methods. code show as below:

System.out.println(Arrays.toString(ReflectTest.class.getDeclaredMethods()));

Acquiring property

With this method you can ignore the rules of access, access to all properties. code show as below:

System.out.println(Arrays.toString(ReflectTest.class.getDeclaredFields()));

Gets constructor

reflectTestClass.getConstructor();

From normal to abnormal (reflecting operation target)

Class<ReflectTest> reflectTestClass = ReflectTest.class;
Method method = reflectTestClass.getMethod("setName", String.class);
Constructor<ReflectTest> constructor = reflectTestClass.getConstructor();
reflectTest = constructor.newInstance();
method.invoke(reflectTest, "公众号");
System.out.println(reflectTest.getName());

The complete code

package 基础;

import lombok.Data;
import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @Description: 反射测试
 * @Author: boolean
 * @Date: 2019/12/26 17:01
 */
@Data
public class ReflectTest {
    private String name;
    private String pass;

    @Test
    public void test() throws NoSuchMethodException, ClassNotFoundException, IllegalAccessException, InvocationTargetException, InstantiationException {
        /**
         * 前提获取 Class 类
         */
        // 方法1
        System.out.println(ReflectTest.class);
        // 方法2
        System.out.println(new ReflectTest().getClass());
        // 方法3  包名+类名 (JDBC那种操作) 有异常
        System.out.println(Class.forName("基础.ReflectTest"));
        // 获取类名
        System.out.println(ReflectTest.class.getName());
        // 获取方法
        System.out.println(Arrays.toString(ReflectTest.class.getDeclaredMethods()));
        // 获取属性
        System.out.println(Arrays.toString(ReflectTest.class.getDeclaredFields()));

        // 正常创建对象并初始化
        ReflectTest reflectTest = new ReflectTest();
        reflectTest.setName("boolenbl公众号");
        System.out.println(reflectTest.getName());

        // 反射创建对象并初始化
        Class<ReflectTest> reflectTestClass = ReflectTest.class;
        Constructor<ReflectTest> constructor = reflectTestClass.getConstructor();
        reflectTest = constructor.newInstance();
        Method method = reflectTestClass.getMethod("setName", String.class);
        method.invoke(reflectTest, "公众号");
        System.out.println(reflectTest.getName());
    }

    public void test1(){

    }

    public void test2(){

    }

    public void test3(){

    }
}

operation result

class 基础.ReflectTest
class 基础.ReflectTest
class 基础.ReflectTest
基础.ReflectTest
[public boolean 基础.ReflectTest.equals(java.lang.Object), public java.lang.String 基础.ReflectTest.toString(), public int 基础.ReflectTest.hashCode(), public java.lang.String 基础.ReflectTest.getName(), public void 基础.ReflectTest.setName(java.lang.String), public void 基础.ReflectTest.test() throws java.lang.NoSuchMethodException,java.lang.ClassNotFoundException,java.lang.IllegalAccessException,java.lang.reflect.InvocationTargetException,java.lang.InstantiationException, protected boolean 基础.ReflectTest.canEqual(java.lang.Object), public void 基础.ReflectTest.test3(), public void 基础.ReflectTest.test2(), public void 基础.ReflectTest.test1(), public java.lang.String 基础.ReflectTest.getPass(), public void 基础.ReflectTest.setPass(java.lang.String)]
[private java.lang.String 基础.ReflectTest.name, private java.lang.String 基础.ReflectTest.pass]
boolenbl公众号
公众号

reference

https://www.cnblogs.com/chanshuyi/p/head_first_of_reflection.html
sczyh30.com/posts/Java/java-reflection-1/
https://juejin.im/post/598ea9116fb9a03c335a99a4

No micro-channel public attention, ready to move end reading

No public .jpg

Guess you like

Origin www.cnblogs.com/chenzhuantou/p/12105276.html