One: Overview of Reflections
Reflection is the soul of frame design, the Class,class class used to represent A. class file (bytecode) before using the bytecode that must be represented before use.
The Java reflection mechanism is in the running state, for any class, can know all the properties of this class, methods and constructors, for any one object, can call any of its methods and properties; This dynamically acquired information and the ability to dynamically invoke the method of the object is called the reflection mechanism of the Java language.
Two: the reflective entrance:java.lang.Class
The objects in the daily development are divided into two types, the basic type and the reference type:
- Basic type, (fixed 8 kinds)
- Integer: Byte, short, int, long
- Decimal: Float, double
- Character: Char
- Boolean value: Boolean
- Reference type
- All reference types inherit from Java.lang.Object
- Classes, enumerations, arrays, interfaces are reference types
- java.io.Serializable interfaces, basic types of wrapper classes (such as java.lang.Double) are also reference types
For each object, the JVM instantiates an instance of Java.lang.Class, and Java.lang.Class gives us the ability to access the object's property and type information at run time. The class also provides the ability to create new classes and objects. Most importantly, class is the entry that calls the other reflection APIs, and we have to get a class instance before we can do the following.
(1), what is in a class
The class name, class modifier, package information, parent class, implemented interface, property, method, constructor (construction method), and annotation multipart component.
(2), attributes are composed of several parts
modifier, type, attribute name, attribute value four parts.
(3), methods are composed of several parts
modifier, return type, method name, argument list, method body, return value
(4), the structure of the composition of several parts
Modifier, constructor name (class name), argument list, method body
Three: Access to the entrance: Java.lang.Class
1.object.getclass method
If we've got an object, it's easy to use its getclass method to get a Class object (which is limited to objects of reference type, of course):
"String".getClass();
The returned object strClass is of type String.
2.. class syntax
If we do not currently have an object of a class and cannot use the GetClass () method, then you can use another method to get class: Add the. class to the name you want to get, such as this:
Integer. class . newinstance (); int. class. newinstance ();
As you can see, this method can be used not only for reference types, but also for basic types.
3.class.forname ()
If we have the full path of a class, we can use the Class.forName ("Class complete path") to get the corresponding class, which can only be used for reference types, such as:
class<?> C = class.forname ("java.lang.String"); Class<?> aclass = Class.forName ("Com.ws.learn.Test");
4. All methods at a glance
Four: Get member information from class
java.lang.reflect.Memberis an interface that represents the members of class, each of which has a type, whether it is inherited from the parent class, and whether it can be accessed directly.
Member has three implementation classes:
java.lang.reflect.Constructor: Represents the constructor of the Class
java.lang.reflect.Field: Represents the member variable of the Class
java.lang.reflect.Method: Represents the Member method of the Class
Get constructor
java.lang.ClassThe following methods are provided to get the constructor for the class:
Note: Constructors cannot inherit from parent class
Constructor GetConstructor (class[] params) returns a concrete constructor with the public property Constructor GetConstructors () based on the arguments of the constructor function Returns all constructors with the public Property array constructor Getdeclaredconstructor (class[] params) based on the parameters of the constructor, Returns a concrete constructor (without public and non-public properties) Constructor getdeclaredconstructors () returns all the constructor arrays in the class (without public and non-public properties)
Get member Variable
java.lang.ClassProvides the following methods for getting member variables of the class:
Field GetField (String name) Returns a concrete member variable with the public property, based on the variable name field[] GetFields () returns an array of member variables with the Public property field Getdeclaredfield (String name) returns a member variable (without public and non-public attributes) based on the variable name field[] Getdelcaredfields () Returns an array of all member variables (without public and non-public properties)
Get member methods
java.lang.ClassThe following methods are provided to get the member methods of the class:
Method GetMethod (String name, class[] params) returns a concrete way with the public property, based on the method name and arguments method[] GetMethods () Returns all methods that have the Public property array method Getdeclaredmethod (String name, class[] params) , based on the method name and parameters, Returns a concrete method (without public and non-public attributes) method[] Getdeclaredmethods () returns all the method arrays in the class (without public and non-public properties)
Reflection in Java