Java reflection mechanism

Posted by guitarlvr on Tue, 01 Mar 2022 14:34:30 +0100

Overview of Java reflection mechanism

1.Java reflection: the mechanism refers to that in the running state of the program, the object of any class can be constructed, the class to which any object belongs can be understood, the member variables and methods of any class can be understood, and the properties and methods of any object can be called. This function of dynamically obtaining program information and dynamically calling objects is called the reflection mechanism of Java language. Reflection is regarded as the key to dynamic language. Reflection makes Java a quasi dynamic language. Disadvantages increase insecurity.
2. Dynamic language (weakly typed language) is a language that determines the data type only at run time. There is no need to declare the type of a variable before using it. Usually, the value of a variable is the type of the value assigned For example, Php, Asp, JavaScript, Python, Perl, etc Static language (strongly typed language) is a language that can determine the data type of variables at compile time. Most static languages require the data type of variables before using them For example, Java, C, C + +, C # and so on Weakly typed languages are languages in which data types can be ignored.
3. Static language is a language that can determine the data type of variables at compile time. Most static type languages require that the data type must be declared before using variables. For example: C + +, Java, Delphi, c# etc. Dynamic languages are languages that determine data types at run time.
4.Class: a class that describes a class. Class is the implementation of a specific class defined in the Java language. The definition of a class includes member variables, member methods, the interface implemented by the class, and the parent class of the class. The object of class is used to represent the classes and interfaces in the currently running Java application. For example, each array belongs to a class object, and all arrays with the same element type and dimension share a class object. Basic Java types (boolean, byte, char, short,int, long, float and double) and void types can also be represented as class objects.

Understand Class class and get Class real column

Class loading and ClassLoader

Class c1 = Class.forName("com.fianl_.reflection_.pojo");
        Class c2 = Class.forName("com.fianl_.reflection_.pojo");
        Class c3 = Class.forName("com.fianl_.reflection_.pojo");
        System.out.println(c1.hashCode());
        System.out.println(c2.hashCode());
        System.out.println(c3.hashCode());

The operation results are as follows:
Conclusion: C1 c2. c3. Like hashCode, it indicates that a Class has only one Class object in memory. After a Class is loaded, the whole structure of the Class will be encapsulated in the Class object

Class some common methods:

getName()
One Class Object describes the properties of a specific class, Class Class getName with String Return this as Class The entity represented by the object (class, interface, array class, basic type, or void)name. 
newInstance()
Class There is also a useful method to create an instance of a class. This method is called newInstance(). For example: x.getClass.newInstance(),Created the same x New instances of the same type. newInstance()Method calls the default constructor (parameterless constructor) to initialize the new object.
getClassLoader()
Returns the class loader for this class.
getComponentType()
Returns a that represents the type of an array component Class. 
getSuperclass()
Return indicates this Class The entity represented (class, interface, basic type or void)Super class Class. 
isArray()
Judge this Class Object represents an array class.
getClassLoader()
/******************************************************************/
Gets the class loader for this class.

getComponentType()

If the current class represents an array, returns the component representing the array Class Object, otherwise return null. 

getConstructor(Class[])

Return to current Class Object represents the specified public constructor subobject of the class.

getConstructors()

Return to current Class Object represents an array of all publicly constructed sub objects of the class.

getDeclaredConstructor(Class[])

Return to current Class Object represents a specified construction sub object of the class.

getDeclaredConstructors()

Return to current Class Object represents an array of all the described construction sub objects of the class.

getDeclaredField(String)

Return to current Class Object represents a specified domain object of a class or interface.

getDeclaredFields()

Return to current Class Object represents an array of all the declared domain objects of the class or interface.

getDeclaredMethod(String,Class[])

Return to current Class Object represents a specified method object of a class or interface.

getDeclaredMethods()

return Class An array of all the described methods of the class or interface represented by the object.

getField(String)

Return to current Class Object represents the specified public member domain object of the class or interface.

getFields()

Return to current Class Object represents an array of all accessible public domain objects of the class or interface.

getInterfaces()

Returns the interface implemented by the class or interface represented by the current object.

getMethod(String,Class[])

Return to current Class Object represents the specified public member method object of the class or interface.

getMethods()

Return to current Class An array of all public member method objects of the class or interface represented by the object, including declared and inherited methods from the parent class.

getModifiers()

Returns the name of the class or interface Java Language modifier code.

getName()

return Class The type represented by the object(Class, interface, array or base type)The full pathname string of the.

getResource(String)

Finds a resource by the specified name.

getResourceAsStream(String)

Find a resource with a given name.

getSigners()

Gets the class tag.

getSuperclass()

If this object represents a division Object Any class other than,Then return the parent object of this object.

isArray()

If Class Object represents an array true,Otherwise return false. 

isAssignableFrom(Class)

determine Class Whether the class or interface represented by the object is the same as that specified by the parameter Class Represents the same class or interface, or its parent class.

isInstance(Object)

This method is Java language instanceof Dynamic equivalence method of operation.

isInterface()

Determine the specified Class Whether the object represents an interface type.

isPrimitive()

Determine the specified Class Does the object represent a Java The base type of the.

newInstance()

Create a new instance of the class.

toString()

Converts an object to a string.

Three methods to get Class

1. Know the specific class and obtain it through the class attribute of the class. This method is the most safe and reliable and has the highest performance

Class a2=pojo.class;
        System.out.println(a2.hashCode());

2. Know the instance of a Class and call the getClass() method of the instance to obtain the Class object

pojo pojo = new pojo();
        Class a1=pojo.getClass();
        System.out.println(a1.hashCode());

3 know the full name of a Class, and the Class can be obtained through the static method forName() of Class class under the Class path. ClassNotFoundException may be thrown

 Class c1 = Class.forName("com.fianl_.reflection_.pojo");
        System.out.println(c1.hashCode());

Run code:

Class c1 = Class.forName("com.fianl_.reflection_.pojo");
        System.out.println(c1.hashCode());
        //. know the instance of a Class and call the getClass() method of the instance to get the Class object
        pojo pojo = new pojo();
        Class a1=pojo.getClass();
        System.out.println(a1.hashCode());
        //Know the specific class and obtain it through the class attribute of the class. This method is the most safe and reliable and has the highest performance
        Class a2=pojo.class;
        System.out.println(a2.hashCode());

result:

Topics: Java reflection intellij-idea