Introduction to the
Reflection of the concept is put forward by Smith for the first time in 1982, is mainly refers to the program can access, test and modify its state or behavior of a kind of ability, and can according to the state of their actions and results, to adjust or modify the state of the described application behavior and related semantic, through reflection can be called a private method and private property, Most frames also use reflection.
function
The reflection mechanism provides the following functions:
-
Determine the class of any object at run time;
-
Construct an object of any class at runtime;
-
Determine which member variables and methods any class has at run time.
-
Call a method of any object at runtime;
-
Generate dynamic proxies.
Reflection and effects in JAVA
Suppose there are two programmers. One programmer needs to use a class written by the second programmer when writing a program, but the second programmer does not complete the class he wrote. The first programmer’s code will not compile. At this point, Java reflection allows the first programmer to compile his own code without getting the classes written by the second programmer.
It knows the basic structure of a class, and this ability to explore the structure of a Java class is called “self-examination” of Java classes. In Eclipse, the compiler automatically lists all the methods and properties that the object can use with a click for the user to choose from. This makes use of the principles of Java reflection, which detects and examines the objects we create.
Three ways to get a Class object
Class classes have no common constructor and cannot be instantiated with the new operator; Only through the object’s getClass method, or through class.forname (…). To get an example.
- The name of the class. The class
Class personClazz = Person.class;
Copy the code
- Instance. GetClass ()
Person person = new Person();
Class personClazz1 = person.getClass();
Copy the code
- Class.forname (” Full path of Class “)
Class personClazz2 = Class.forName( "com.muse.reflect.Person" );
Copy the code
Common methods of the Class Class
-
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.
Steps for generating objects
The sample code
Advantages and disadvantages of using JAVA reflection
- Advantages:
- It can dynamically obtain instances of classes at runtime, greatly improving the flexibility and expansibility of the system.
- Combined with Java dynamic compilation, you can achieve unmatched power
- Disadvantages:
- The performance of using reflection is low
- Using reflection is relatively unsafe
- The encapsulation of a class is broken, and private methods and properties of the class can be obtained by reflection