Summary of Java reflection mechanism (quoted from others)

The concept of Java reflection mechanism:
Java reflection mechanism means that in the running state, for any class, you can know all the properties and methods of this class; for any object, you can call any of its methods and properties; this dynamic The information obtained and the function of dynamically calling the method of the object are called reflection mechanism of the java language.

About Class (java.lang.Class):

Java has an Object class, which is the source of inheritance of all Java classes. It declares several methods that should be rewritten in all Java classes: hashCode(), equals(), clone(), toString(), getClass() Wait. where getClass() returns a Class object.
The Class class is very special. It inherits from Object like general classes, and its entities are used to express classes and interfaces at runtime of Java programs, and are also used to express enum, array, primitive Java types (boolean, byte, char, short, int, long, float, double ) and the keyword void. When a class is loaded, or when the class loader's defineClass() is called by the JVM, the JVM automatically generates a Class object.
Class is the origin of the Reflection story. For any class you want to explore, you must first generate a Class object for it, and then you can invoke dozens of Reflection APIs through the latter. The Reflection mechanism allows a program to use Reflection APIs to obtain internal information of any class with a known name during execution, including: package, type parameters, superclass, implemented interfaces, inner classes, outer classes, fields, constructors, methods, modifiers And so on, and can dynamically generate instances, change the content of fields or invoke methods during the execution process.

1. Class is a class, a class that describes the class (that is, the class itself), encapsulates the Method that describes the method, the Field that describes the field, and the Constructor that describes the constructor.
2. After the object looks in the mirror (reflection), you can get information: data member names, methods and constructors of a class, and which interfaces a class implements.
3. For each class, the JRE reserves an object of constant Class type for it. A Class object contains information about a particular class.
4. Class objects can only be created by the system.
5. A class has only one instance of Class in the JVM.

There are three ways to obtain classes by reflection mechanism:

public class Person { private String name; /** * If this class is to be able to create an instance through reflection, it must have an explicit no-argument constructor */ public Person() { } public Person(String name) { this.name = name; } public void showEffect() { System.out.println("This dress up" + name); } }

 

/**
 * Three ways to obtain classes by reflection mechanism
 */  
@Test  
public void testGetClass() throws ClassNotFoundException {  
    Class clazz = null;  
  
    //1 Get directly through the class name.Class  
    clazz = Person.class;  
    System.out.println("By class name: " + clazz);  
  
    //2 Obtained by the getClass() method of the object, which is used less (usually Object is passed, and it is only used when I don't know what type it is)  
    Object obj = new Person();  
    clazz = obj.getClass();  
    System.out.println("通过getClass(): " + clazz);  
  
    //3 Obtained by the full class name, which is used more, but may throw ClassNotFoundException exception  
    clazz = Class.forName("com.java.reflection.Person");  
    System.out.println("Get by full class name: " + clazz);  
}

Use newInstance to create an object:

Note: For a class to be able to create instances through reflection, it must have an explicit no-argument constructor, which is a mandatory requirement.

/**
 * The newInstance() method of the Class class creates an object of the class.
 */  
@Test  
public void testNewInstance()  
        throws ClassNotFoundException, IllegalAccessException, InstantiationException {  
  
    Class clazz = Class.forName("com.java.reflection.Person");  
  
    //Create an object of the class using the newInstance() method of the Class class  
    //The parameterless constructor of the class that is actually called (this is why when writing a class, you need to write a parameterless constructor, which is for reflection)  
    //Generally, if a class declares a constructor with parameters, it also declares a constructor without parameters  
    Object obj = clazz.newInstance();  
    System.out.println(obj);  
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326267721&siteId=291194637