
For any class you want to dynamically load and run, you have to obtain the corresponding class object firstĪ) If a specific class is known, it is obtained through the class attribute of the class. Class file loaded into the JVMĮach Class instance will remember which Class instance it was generated fromĪll loaded structures in a Class can be completely obtained through ClassĬlass is the root of Reflection. A Class object contains information about a specific structure (class/interface/enum/annotation/primitive type/void / ).Ĭlass objects can only be created by the systemĪ loaded Class has only one Class instance in the JVMĪ class object corresponds to a. For each Class, JRE reserves an object of constant Class type. The information that can be obtained after the object looks in the mirror: the attributes, methods and constructors of a Class, and which interfaces a Class implements. In fact, the so-called reflection will be well understood from the running results of the program, that is, the name of the Class can be obtained through object reflection. The type of the return value of the above method is a Class class, which is the source of Java reflection. The following methods are defined in the Object class, which will be inherited by all subclasses : represents the member variable of the class Such operations are always slower than directly performing the same operation.

We can tell the JVM what we want to do and it meets our requirements. Using reflection is basically an interpretive operation. Judge the member variables and methods of any class at run timeĬall the member variables and methods of any object at run timeĪdvantages and disadvantages of Java reflectionĪdvantages: it can dynamically create objects and compile, reflecting great flexibilityĭisadvantages: it has an impact on performance. Research and application of Java reflection mechanismįunctions provided by Java reflection mechanismĭetermine the class of any object at run timeĬonstruct an object of any class at run time We can see the Class structure through this mirror. We can see the Class structure through this object. This object contains the complete Class structure information. The reflection mechanism allows programs to obtain the internal information of any class with the help of the reflection API during execution, and can directly operate the internal properties and methods of any object.Ĭlass c = Class.forName ("")Īfter loading the Class, an object of Class type (a Class has only one Class object) is generated in the method area of the heap memory. Reflection is the key to Java being regarded as a dynamic language. Java's dynamics makes programming more flexible! We can use the reflection mechanism to obtain characteristics similar to dynamic language. Java is not a dynamic language, but Java can be called a "quasi dynamic language". Main dynamic languages: Object-C, c#, JavaScript, PHP, Python, etc.Ĭorresponding to dynamic languages, languages with immutable runtime structure are static languages, such as Java, C and C + +.
Java reflection get method annotation code#
Generally speaking, the code can change its structure according to some conditions at run time. It is a kind of language that can change its structure at run time: for example, new functions, objects and even code can be introduced, existing functions can be deleted or other structural changes. Reflection mechanism Static VS dynamic language Int id() default -1 //If the module default value is - 1, it means that it does not exist Annotated parameters: parameter type + parameter name () Public void it is value and the annotation has only one value, value can be omitted If there is no default value, we must assign a value to the = "maoerxian" ,schools = "university") When defining an annotation element, we often use an empty string with 0 as the default value If there is only one parameter member, the general parameter name is valueĪn annotation element must have a value. You can declare the default value of the parameter through default

The return value type is the type of the parameter (the return value can only be the basic type, class, string, enum) The name of the method is the name of the parameter. Public class Test01 extends overridden String toString() Įach of these methods actually declares a configuration parameter

We can access these metadata through reflection mechanism programming Where can Annotation be used? > can be attached to package, class, method, field, etc., which is equivalent to adding additional auxiliary letters to them. You can also add some parameter values, such as SuppressWarnings(value="unchecked") The function of Annotation: > not the program itself, it can explain the program, (this is no different from comments) > it can be read by other programs (such as compilers).įormat of Annotation: > Annotation exists in the code as Annotation name". Annotation and reflection What is annotationĪnnotation is a new technology introduced from JDK5.0.
