Objective
The underlying implementation of reflection in Java is that the classloader of the JVM obtains the fully qualified name to create the class.
Body gets the Reflection type Object
1. Class name.
Do not perform static fast
2..class<?> GetClass ()
Returns the runtime class of this Object.
3.static class<?> forname (String className)
Returns the class object associated with the class or interface with the given string name. performs static fast
Get constructors, properties, and methods for the current class through the class object
1.constructor<?>[] Getdeclaredconstructors ()
Returns an array of Constructor objects reflecting all the constructors declared by the class represented by this class OB Ject. Get all constructors for the current class declaration
2.constructor<?>[] getconstructors ()
Returns An array containing Constructor objects Reflecting all the public constructors of the class represented by this class object. Get the current class common constructor
3.field[] Getdeclaredfields ()
Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this class O Bject. Gets the properties of the current class itself declaration
4.field[] GetFields ()
Returns an arrays containing Field objects reflecting all the accessible public fields of the class or interface Represente D by this Class object. Gets the public properties that the current class inherits and its own definition
5.method[] Getdeclaredmethods ()
Returns an array of Method objects reflecting all the methods declared by the class or interface represented by this class Object. method to obtain the current class self-declaration definition
6.method[] GetMethods ()
Returns an array containing Method objects reflecting all the public member methods of the class or interface represented By this class object, including those declared by the class or interface and those inherited from Superclasses and Superin Terfaces. Obtaining a public method of the current class inheritance and its own definition
Note:
In the ordinary method of the class with a synchronous lock, which is the object of the current class, and a static method with a synchronous lock, which is the class object of the current classes
7. Sample code to obtain all declared methods
To create an object of the current class by reflection
1. Load the target class into memory and do not load if the target class exists in the current memory
2. Using deferred load mode, the target class is not loaded immediately when the program code executes, but is loaded when the target class is used.
3.T newinstance ()
Creates a new instance of the class represented by this class object.
Throws:
Illegalaccessexception-if the class or its nullary constructor are not accessible.
Instantiationexception-if This class represents an abstract class, an interface, an array class, a primitive type, or VO Id Or if the class has no nullary constructor; Or if the instantiation fails for some and other reason.
Exceptionininitializererror-if the initialization provoked by this method fails.
Securityexception-if a security manager, S, is present and any of the following conditions is met:? Invocation of S.CHEC Kmemberaccess (this, member.public) denies creation of new instances of this class? The caller's class loader is not the SA Me as or an ancestor of the class loader for the current class and invocation of S.checkpackageaccess () denies access to T He package of the This class
Accessing properties and execution methods through reflection
1.FieldAPI
Methods inherited from class Java.lang.reflect.AccessibleObject
1) public void Setaccessible (Boolean flag)
Throws Securityexceptionset the accessible flag for this object to the indicated Boolean value. A value of TRUE indicates that the reflected object should suppress Java language access checking when it was used. A value of False indicates that the reflected object should enforce Java language access checks. Set current properties to access
2. Code examples
Summarize