First, Introduction
Many times our programs may need to recognize objects and classes at run time, such as polymorphism, which is the object that is actually referenced based on the runtime environment for dynamic judgments. There are two main ways to identify objects and classes at run time: 1. RTTI, specifically the class object, assumes that we already know all types at compile time. 2. The reflection mechanism that runs the information we discover and use classes at run time.
Second, RTTI
RTTI (run-time type infomation), run-time type information. You can identify the type of an object at run time. Type information is represented at run time by class objects, class objects contain information about classes, and you can use class objects to create instances of classes.
Each class corresponds to a class object, which is placed in the. class file, and when the first active use of a type is made in our program, the class object corresponding to that type is loaded into memory, and in this article the JVM and the like are described in terms of what is appropriate for the first active use.
Since Rtti has a great relationship with class objects, that is, with a class object, you can do a lot of things, so how do we get to the class object? There are three methods of 1. Class.forName ("Fully qualified name"); (where the fully qualified name is the package name + class name). 2. Class literal constants, such as String.class, corresponding to the class object of the string class. 3. Obtain a class object by using the GetClass () method, such as String str = "abc"; Str.getclass ();.
What can we do with a class object that corresponds to a category? We can get the parent class of the class, the interface, the object that created the class, the constructor of the class, the field, the method, and so on. In short, the power is quite large.
Let's use an example to familiarize ourselves with the various uses of class objects.
View Code
Operation Result:
View Code
Description: The class object can be obtained using the method1, METHOD2, method3 three methods, and the result of the operation is equivalent. But there is a slight difference between the three. The difference is from the class's initialization point of view. such as Class.forName ("Fully qualified name") causes the loading, linking, initialization of the type, while. class does not initialize the class. Obviously, GetClass is definitely going to initialize the class, because this method relies on the object of the class.
Here we compare the difference between the. Class and The Forname () method by an example.
View Code
Operation Result:
View Code
Description: Further validation from the results. class does not initialize classes, and. forname () initializes the class. Also, the use of a constant static domain does not result in the initialization of the class.
Third, reflection
Unlike Rtti, which must know all types in the compiler, reflection does not have to know all types at compile time, it can use dynamically loaded classes during the run, and this class does not have to be known at compile time. Reflection is mainly supported by the field, Method, constructor class of the Java.lang.reflect class library. Objects of these classes are created by the JVM at run time to represent unknown classes.
The difference between the two is more profound: for Rtti, the compiler opens and checks the. class file at compile time, and for reflection, the. class file is not available at compile time, so open and check the. class file at run time.
In its first example, we have used the constructor, method class, and now we have more specific understanding of constructor, method, field class.
View Code
Input: Boy
Operation Result:
View Code
Description: Reflection allows us to create an instance of a class, access a private method of a class outside the class, and a private field. The reflection is really powerful ~
IV. Application of dynamic Proxy-reflection
Dynamically creates the agent and dynamically processes calls to the proxy method. All calls made on the dynamic agent are redirected to a single calling processor.
The following is an example of a dynamic proxy
View Code
Operation Result:
View Code
Description: Filtering can be done in the Invoke method. Filter out a method that starts with do and forwards.
V. Summary
Rtti and reflection analysis to this end, Rtti and reflection is really strong, can help us do a lot of things, with the absolute power of the place, thank you for your view of the Garden friends ~
"Java Basics" Rtti and reflection Java