Beginners understand the basics of java reflection

  • 2021-10-24 22:46:31
  • OfStack

Catalog 1. Reflection Overview 2. Common api 3. Summary of 4 Ways to Create Class Objects

1. Overview of Reflex

Reflection (reflection) is regarded as the key of dynamic language. Reflection mechanism allows programs to obtain the internal information of any class by means of Reflection and API at execution time, and can directly manipulate the internal attributes and methods of any object. After the class is loaded, an object of type Class is generated in the method area of heap memory (there is only one Class object per class), which contains the complete structure information of the class. We can see the structure of the class through this object. This object is like a mirror. We can see the structure of the class through this mirror, so we call it reflection figuratively.

2. Common api

java.lang.Class Represents 1 class

java.lang.reflect.Method Method that represents a class

java.lang.reflect.Field Member variables representing a class

java.lang.reflect.Constructor Constructor that represents a class

static Class forName(String name) Returns an Class object with the specified class name name

Object newInstance() Call the parameterless constructor to return 1 instance of the Class object

getName() Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object

Class getSuperClass() Returns the Class object of the parent class of the current Class object

Class [] getInterfaces() Gets the interface of the current Class object

ClassLoader getClassLoader() Returns the class loader for the class

java.lang.reflect.Method0 Returns an Class representing the superclass of the entity represented by this Class

Constructor[] getConstructors() Returns an array containing some Constructor objects

Field[] getDeclaredFields() Returns an array of Method objects

getMethod(String name,Class … paramTypes) Returns 1 Method object whose formal parameter type is paramType

Example:


public class Person {
    private String name;
    private int age;
    public Person() {
        System.out.println("Person Class is initialized. . . . . ^_^");
    }
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Person{" +
            "name='" + name + '\'' +
            ", age=" + age +
            '}';
    }
}

Reflex


public class ClassDemo {
    public static void main(String[] args) throws
        Exception {
        // 1.  Create Class Object 
        //Class<Person> clazz = (Class<Person>)
        Class.forName("Person");
        Class<?> clazz =Class.forName("Person");
            // 2.  Pass Class Object calls its method 
            // 2.1.  Instantiate an object through reflection 
            //Person person = clazz.newInstance();
            Object o = clazz.newInstance(); //  This name is equivalent to 
        Person p = new Person()
            // 2.2.  Gets the full class name (package name) of the class + Class name) 
            String name = clazz.getName();
        System.out.println(name);
        // 2.3.  Gets the simple name of the class, that is, the name of the class, excluding the package name 
        System.out.println(clazz.getSimpleName());
        // 2.4.  Get constructor 
        Constructor<?>[] constructors =
            clazz.getConstructors();
        for (Constructor<?> constructor :
             constructors) {
            System.out.println(constructor.getName());
        }
        // 2.5.  Get a field 
        Field[] fields = clazz.getFields();
        for (Field field : fields) {
            System.out.println(field.getName());
        }
        // 2.6.  Acquisition method 
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            System.out.println(method.getName());
        }
    }
}

3. 4 Ways to Create Class Objects


public class Demo {
    public static void main(String[] args) throws
        Exception {
        //  No. 1 1 Ways: Call Class.forName()
        Class clazz =Class.forName("java.lang.String");
        //  No. 1 2 One way: Create it from the bytecode file of the class 
        Class clazz1 = String.class;
        //  No. 1 3 By calling the object's getClass() Method to create a 
        String str = new String();
        Class clazz2 = str.getClass();
        //  No. 1 4 Type: Created by the class loader 
        ClassLoader loader =String.class.getClassLoader();
        Class<?> clazz3 =loader.loadClass("java.lang.String");
    }
}

Summarize

That's all for this article. I hope it will be helpful to you, and I hope you can pay more attention to more contents of this site!


Related articles: