Overview of Java reflection

1. Reflection of Java: Mechanism means that in the running state of the program, you can construct the object of any class, know the class of any object, know the member variables and methods of any class, and call the properties and methods of any object. This ability to dynamically retrieve program information and invoke objects is called the Reflection mechanism of the Java language. Reflection is seen as the key to dynamic languages, and reflection makes Java a quasi-dynamic language. Disadvantages increase insecurity.

2. Dynamic languages (weakly typed languages) are languages in which data types are determined at runtime. Variables do not have to be typed before being used. Such as Php, Asp, JavaScript, Python, Perl and so on.

Static languages (strongly typed languages) are those in which the data type of a variable can be determined at compile time. Most static languages require the data type to be lived before using the variable. Such as Java, C, C++, C#, etc. Weakly typed languages are languages in which data types can be ignored.

3. Static languages are those in which the data type of a variable is determined at compile time. Most statically typed languages require that the data type of a variable be declared before it can be used. For example: C++, Java, Delphi, C# and so on. Dynamic languages are languages that determine data types at run time.

4.Class: A Class that describes a Class. A Class Class is an implementation that defines a specific Class in the Java language. The definition of a class contains member variables, member methods, interfaces that the class implements, and its parent class. Objects of Class are used to represent 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.

2. Understand the Class Class and get the Class columns

3. 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());
Copy the code

The running results are as follows:

Conclusion: C1.c2.c3. hashCode states that a Class has only one Class object in memory. Once a Class is loaded, the entire structure of the Class is encapsulated in a Class object

Class some common methods:

A Class object describes the properties of a particular Class. The most common method in Class is getName, which returns the name of the entity (Class, interface, array Class, primitive type, or void) represented by the Class object as a String. NewInstance ()Class also has a useful method for creating an instance of a Class. This method is called newInstance(). For example, x.getclass.newinstance () creates a newInstance of the same type as x. The newInstance() method calls the default constructor (no arguments constructor) to initialize the new object. GetClassLoader () returns the classloader for the class. GetComponentType () returns a Class representing the type of an array component. GetSuperclass () returns the Class representing the superclass of the entity (Class, interface, primitive type, or void) represented by this Class. IsArray () determines whether this Class object represents an array Class. GetClassLoader () / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / class loader for the class. GetComponentType () returns the Class object representing the array component if the current Class represents an array, or null otherwise. GetConstructor (Class[]) returns the specified public constructor child object of the Class represented by the current Class object. GetConstructors () returns an array of all publicly constructed child objects of the Class represented by the current Class object. GetDeclaredConstructor (Class[]) returns a specified constructor child object of the Class represented by the current Class object. GetDeclaredConstructors () returns an array of all declared constructional subobjects of the Class represented by the current Class object. GetDeclaredField (String) returns a specified domain object of the Class or interface represented by the current Class object. GetDeclaredFields () returns an array of all declared domain objects for the Class or interface represented by the current Class object. GetDeclaredMethod (String,Class[]) returns a specified method object of the Class or interface represented by the current Class object. GetDeclaredMethods () returns an array of all declared methods of the Class or interface represented by the Class object. GetField (String) returns the specified public member field object of the Class or interface represented by the current Class object. GetFields () returns an array of all accessible public domain objects of the Class or interface represented by the current Class object. GetInterfaces () returns the interface implemented by the class or interface represented by the current object. GetMethod (String,Class[]) returns the specified public member method object of the Class or interface represented by the current Class object. GetMethods () returns an array of all public member method objects of the Class or interface represented by the current Class object, including declared and inherited methods from the parent Class. GetModifiers () returns Java language modifier code for this class or interface. GetName () returns the full pathname string of the type (Class, interface, array, or base type) represented by the Class object. GetResource (String) Finds the resource by the specified name. GetResourceAsStream (String) Finds the resource with the given name. GetSigners () gets the class tag. GetSuperclass () returns the parent Object of this Object if it represents any class other than Object. IsArray () Returns true if the Class object represents an array, false otherwise. IsAssignableFrom (Class) determines whether the Class object represents the same Class or interface as the Class or its parent. IsInstance (Object) This method is the dynamic equivalent of the Java language instanceof operation. IsInterface () determines whether the specified Class object represents an interface type. IsPrimitive () determines whether the specified Class object represents a Java base type. NewInstance () creates a newInstance of the class. ToString () converts an object to a string.Copy the code

Three ways to get a Class

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

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

2. Knowing an instance of a Class, call the instance’s getClass() method to get the Class object

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

3. Know the full name of a Class that is on the classpath and is available through the static forName() method of the Class Class

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

Run code:

Class c1 = Class.forName("com.fianl_.reflection_.pojo"); System.out.println(c1.hashCode()); Pojo = new pojo(); pojo = new pojo(); Class a1=pojo.getClass(); System.out.println(a1.hashCode()); Class a2=pojo.class; class a2=pojo.class; System.out.println(a2.hashCode());Copy the code

Results:

PS: In case you can’t find this article, please click “like” to browse and find it