To understand Java reflection in detail, you have to understand the concepts of Java class loading and what is run-time dynamic loading, and so on. This article put aside the rest of the concept, a brief introduction of Java reflection, detailed introduction will be in the future after a systematic and comprehensive understanding after the deployment.
Reflection is the key to Java being considered a dynamic language, which allows the program to get internal information for any class at run time . This capability of Java may not be used much in Web applications, but it is common in some Java component development processes, such as spring, Hibernate, and so on. Understanding and familiarity with the Java reflection mechanism is a great help in understanding the Java framework.
We first write a test class and compile it as a class byte code.
1 /**2 * @authorTurbo3 *4 * September 17, 20165 */6 Public classTest {7 Public voidTest () {8SYSTEM.OUT.PRINTLN ("Class load Reflection test");9 }Ten}
After compiling, we create another new package in Eclipse and create a new Main.java class that puts this test.class file into the folder class of the. Bin directory of the Eclipse workspace. At this point we see the following directories in the Eclipse project:
The files we see in the. bin directory of the Eclipse workspace are as follows:
Our usual notation is that two Java files call each other, and now only the class file, how to call the method in the test class? Let's see how the Main.java class is done.
1 PackageDay_12_class;2 3 ImportJava.lang.reflect.Method;4 5 /**6 * @authorTurbo7 *8 * September 17, 20169 */Ten Public classMain { One A /** - * @paramargs - * the */ - Public Static voidMain (string[] args)throwsException { -Class<?> clazz = Class.forName ("Test"); -Object object =clazz.newinstance (); +method = Clazz.getmethod ("Test")); - Method.invoke (object); + } A at}
First, to understand what the class class is, the JVM typically uses run-time type information to choose the correct method to execute, and the class that holds these types of information is classes. Each Java class behaves as a class object in the JVM at run time, as indicated above, by calling the static method forname to return the class object through the test class name. This is still a class object, note that it is not an object class, and returns an instance by calling the Newinstance method of class.
The methods class provides information about a method on a class or interface that is used to encapsulate a class of reflection class methods. The method class is also returned by using the class's GetMethod method to pass in the methods name. The API description for the GetMethod method is as follows:
After getting the method class, we call the Invoke method to execute the test method in the test class. The API for the Invoke method is as follows:
Finally, let's take a look at the results:
A method in the test class was successfully invoked using the Java reflection mechanism.
Because of the Java reflection, class loading and other knowledge is not comprehensive and in-depth, do not dare to talk about this, so here only the initial Java reflection is a what, how it used in what place, it is the implementation of the process is what, its principle is what and so on still need to go deep study.
Initial knowledge of Java reflection