Reference: http://geek.csdn.net/news/detail/239924
Java Reflection mechanism
Defined:
The Java reflection mechanism is that in the running state, for any class, you can know all the properties and methods of the class, and for any object, it can call any of its methods and properties; This dynamically acquired information and the ability to dynamically invoke the object's methods are called the reflection mechanisms of the Java language.
In a nutshell, reflection enables you to know the properties and methods of any class at run time.
Why use a reflection mechanism? Creating objects directly doesn't make sense, which involves the concept of dynamic and static
Static compilation: Determines the type at compile time, binds the object, which is passed.
Dynamic compilation: The runtime determines the type, binding object. Dynamic compilation maximizes the flexibility of Java and embodies the application of polymorphism, which reduces the coupling between classes.
Advantages:
can achieve dynamic creation of objects and compilation, showing great flexibility, especially in the development of Java EE, its flexibility is very obvious. For example, a large-scale software, it is impossible to design the perfect one at a time, when the program compiled, released, when the discovery needs to update some features, we can not want users to uninstall the previous, and then reinstall the new version, if so, this software is certainly not many people use. Using static words, the entire program needs to be recompiled once to achieve the function of the update, and the use of reflection mechanism, it can not uninstall, only need to be dynamically created and compiled at run time, you can implement this function.
Disadvantages:
Have an impact on performance. Using reflection is basically an interpretation operation, and we can tell the JVM what we want to do and it satisfies our requirements. This type of operation is always slower than just performing the same operation directly.
Understanding class classes and class types
To understand reflection first understand the class, which is the basis of the reflection implementation.
The class is an instance object of the Java.lang.Class class, and class is the classes of all classes (there is a-class named Class)
For ordinary objects, we will typically create and represent this:
Code CODE1 = new Code ();
It says that all classes are objects of class, so how do you do that, in the following ways:
Class C = new Class ();
But when we look at the source of the class, it's written like this:
Private Class (ClassLoader loader) {
ClassLoader = loader;
}
You can see that the constructor is private, and only the JVM can create class objects, so it is not possible to new class objects like normal classes, although we cannot new a class object, but we can get a class object from an existing class, there are three ways, as follows:
Class C1 = Code.class;
This means that any class has an implied static member variable class, which is obtained by obtaining the class's static member variable class.
Class C2 = Code1.getclass ();
Code1 is an object of code, which is obtained by means of the GetClass () method of the object of a class.
Class C3 = Class.forName ("Com.trigl.reflect.Code");
This method is called by the class class to invoke the Forname method, which is obtained by the full qualified name of a class
Here, C1, C2, and C3 are all class objects, they are exactly the same, and have a scientific name, called the class type of code.
Here is strange, the front is not to say code is the object of class, and C1, C2, C3 is also the object of class, then code and c1, C2, C3 is not the same? Why is it called code what kind of type? Don't dwell on whether or not they are the same, as long as you understand what the class type is doing, as the name implies, the class type is the type of the class, that is, describe what a class is, what it is, so we can know the properties and methods of a class by the class type, and can invoke the properties and methods of a class. This is the basis of reflection.
To give a simple example code:
1 Public classReflectdemo {2 Public Static voidMain (string[] args)throwsClassNotFoundException {3 //The first type: Class c1 = Code.class;4Class Class1=reflectdemo.class; 5 System.out.println (Class1.getname ());6 7 //The second type: Class C2 = Code1.getclass ();8Reflectdemo demo2=NewReflectdemo ();9Class C2 =Demo2.getclass ();Ten System.out.println (C2.getname ()); One A //the third type: Class C3 = Class.forName ("Com.trigl.reflect.Code"); -Class CLASS3 = Class.forName ("Com.tengj.reflect.ReflectDemo"); - System.out.println (Class3.getname ()); the } -}View Code
Execution Result:
View Code
Java Reflection-related operations
Before we know how to get the class, what can we do with this class?
Summarized as follows:
Get Member Method
Get member Variable field
Get Constructor constructor
Here's a concrete introduction to
Get Member Method information
Obtaining a method separately is obtained through the following methods of class:
Public Method Getdeclaredmethod (String name, Class
The reflection mechanism of Java