1. Concept
Reflection is the mapping of the various components in the Java class into the corresponding Java classes. we've already talked about class, and we've learned that a Java class is represented by an object of class, and that the components of a class are: member variables, methods, construction methods, packages, and so on, which are represented by a Java class.
For example, in life, we often see cars, if the car is a class, then the car engine, gearbox and so on is a class. The class class that represents the Java class obviously has to provide a series of methods to obtain the variables, methods, constructors, modifiers, packages and other information, which are represented by the instance object of the corresponding class, that is, the Field,method,contructor, the package, and so on.
2. Mechanism and application
In the running state, for any one class, you can know the properties and methods of the class, and for any object, it can call any of its methods and properties; This dynamic acquisition of information and the method of dynamically invoking the object is reflection, and the dynamic mechanism of Java reflection is reflection, Refers to the load and discovery that can be used during the run, using classes that are completely unknown during compilation. In other words, the Java program class can load a class that is known by the runtime and learns its full construct (without the method definition).
At compile time, there is no way to know what class the object or class might belong to, and the program relies only on information from the runtime to discover the real information about that object and class.
3. Role
By reflection we can give the program code access to the internal information of the class loaded into the JVM, obtaining information about the property information, methods, and construction methods of the loaded class.
The first introduction to the class is the origin and entry of the Java reflection mechanism, where the user obtains various information related to the class, provides methods for obtaining the class information, inherits from the object class, and the class class is the common drawing for all classes.
Some methods of the class classes are as follows:
4. Application of various reflections? Construction method Constructor Class
A class has multiple construction methods, according to the number and type of parameters do not distinguish, Java construction method is not sequential, using the method of constructing method can reflect the corresponding class, by the construction method can get the corresponding instance object, according to the method of reflection to get the construction method is exactly which class of construction method, As shown in the following:
<span style= "Font-family:microsoft yahei;font-size:14px;" ><strong>constructor Constructor1 = String.class.getConstructor (Stringbuffer.class); String str2 = (string) constructor1.newinstance (new StringBuffer ("abc"));</strong></span>
Just know is a construction method, but do not know which construction method, only when running to know
In the example above we see that there are two stringbuffer,
The first expression used is the number of strings in which the construction method, from the above example, using the parameter is StringBuffer construction method. Represents the type to use when obtaining a method.
The second expression is used when a StringBuffer object must be passed in, and an instance object of this class can be obtained by using the newinstance reflection of the constructor, creating an instance object of this type, thereby invoking a method by passing in the same type parameter of the construction method to the incoming representation. Call the obtained method to use the same type of instance object as above.
It is worth noting that the parameters must be matched, and if they do not match, an error will be added. Compile the time just to perform a grammar check, do not execute the code to the right of the equal sign, but only when running to know that it is a String, so the following to add (String).
Ps: In addition to the construction method with the Newinstance () method, class also has, that is, class.newinstance (), such as:
<span style= "Font-family:microsoft yahei;font-size:14px;" ><strong>stringobj= (String) class.forname ("java.lang.String"). newinstance (); </strong></span >
The method internally obtains the default construction method, and then creates the instance object with the constructor method.
? member variable filed class
Define a Reflectpoint class, and then make the call in a class.
<span style= "Font-family:microsoft yahei;font-size:14px;" ><strong>reflectpoint pt1 = new Reflectpoint (3, 5); Field Fieldy = Pt1.getclass (). GetField ("Y"); System.out.println (Fieldy.get (PT1)); System.out.println (Fieldy);</strong></span>
Field Fieldy = Pt1.getclass (). GetField ("y"); If you think the value of this expression is 5, then it is wrong, Fieldy is not a variable on the object, but on the class, to use it to fetch the corresponding value on an object.
Fieldy.get (PT1), above we know that Fieldy is a variable on a class, the return type is a type, or a class, and its get method can be used to reflect the corresponding instance object and get the corresponding value on the instance object.
? Reflection of Member Methods
is a method defined in a class that represents a member method in a class that reflects the individual components of a class into their corresponding Java classes.
Get a method in the class, using GetMethod to get a class of method type, and can match the corresponding parameter type and class name in parentheses. The reflection mode gets the method in the bytecode, and then takes out this method to use for an object.
<span style= "Font-family:microsoft yahei;font-size:14px;" ><strong>method Methodcharat = String.class.getMethod ("CharAt", Int.class); System.out.println (Methodcharat.invoke (str1, 1));</strong></span>
The invocation of a method must be based on an object:
1. That is, in the object str1 called the definition charAt method (Methodcharat) method, the following 1 is the parameter of the method
2. Use reflection to get the method in the bytecode, and then come up with this method for an object. Invoke represents the invocation.
3. That is, the method executes the invoked action, andinvoke is the method on the method object.
Therefore, the first in Invoke is the corresponding instance object, and the second parameter, regardless of the parameter, is called a parameter.
To give a simple example:
Draw a circle or train stop is just a human signal, the operation by itself to perform. The method is that the object itself is called, and the person just sends a notification or signal, and the action performed is just his own. The role of a person is to signal and inform, that is, who owns the data who is an expert, that is, the object itself.
What method is called when the object is not needed? The answer is a static method. Static method calls do not require an object, so it is definitely a static method that does not require an object. A method is used to reflect a given instance object.
<span style= "Font-family:microsoft yahei;font-size:14px;" ><strong>method Methodcharat = String.class.getMethod ("CharAt", Int.class); SYSTEM.OUT.PRINTLN (Methodcharat.invoke (NULL, 1));</strong></span>
5. Summary
reflection is the reflection of each component in the Java class into the corresponding class , and familiarity with and mastery of the reflection will help us to develop the program better. Study studies are still continuing.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Tour (2)-Reflection