JAVA Learning Notes--reflection __java

Source: Internet
Author: User

1. Class Loader
A Class Loader
is responsible for loading the. class file into the inner and generating the corresponding class object for it.
Although we do not need to care about the class loading mechanism, we understand this mechanism to better understand the operation of the program.
b The composition of the class loader
Bootstrap ClassLoader Root class Loader
Extension ClassLoader Extended class loader
Sysetm ClassLoader System Class Loader
2. Reflection
A. The Java reflection mechanism is in the running state, for any class, all the properties and methods of the class can be known, and any one of its methods and properties can be invoked for any object; This dynamically acquired information, and the function of the method that dynamically invokes the object, is called the reflection mechanism of the Java language.
B. To dissect a class, you must first obtain the bytecode file object for that class. And anatomy uses the methods in class classes. So get to the object of the class type for each byte-code file first.
Class classes:
Member variables: Field
Construction Method: Constructor
Member Methods: Method
C. How to get a class file object:
The GetClass () method of the A.object class P.getclass ()
B. Static properties of data types Person.class
Static methods in the C.class class
       Public Static Class forname (String className);//classname is the package name. Class Name
    
     Note: A third way is generally used, because the third returns a string rather than a specific class name, so that the string can be configured into a configuration file.
  
3. Get the construction method through reflection and create the object procedure
A. Getting bytecode files: Using the Class.forName () method
Static class<?> forname (String className): Returns a class object associated with a class or interface with a given string name.
B. Getting construction method objects
constructor< t> getconstructor (class< ... parametertypes): Returns a constructor object that reflects the specified public constructor method for the class represented by this class object.
constructor< >[] GetConstructors (): Returns an array containing some constructor objects that reflect the class of the classes represented by this class object.all public construction methods。
constructor< t> getdeclaredconstructor (class< ... parametertypes): Returns a constructor object that reflects this Class Specifies the constructor method for the class or interface that the object represents.
constructor< >[] Getdeclaredconstructors (): Returns an array of constructor objects that reflect the class declaration represented by this class object.All construction Methods。
C. Creating objects using the constructor newinstance () method
T newinstance (Object ... initargs): Use the constructor represented by this constructor object to create a new instance of the Declaration class for the constructor, and initialize the instance with the specified initialization parameters.
If you want to get the private constructor object, you can set up a violent access to the constructor object first
public void Setaccessible (Boolean flag) throws SecurityException: sets the accessible flag of this object to the indicated Boolean value. A value of TRUE indicates that the reflected object should cancel the Java language access check when it is in use.

< pre>

4. Get member variable procedure by reflection
A. Gets the constructor method and creates the object
B. Get a member variable by using the Byte code class object
Field GetField (String name): Returns a Field object That reflects the specified public member field of the class or interface represented by this class object.
Field[] GetFields (): Returns an array containing some field objects that reflect all accessible public fields of the class or interface represented by this class object.
Field Getdeclaredfield (String name): Returns a Field object that reflects the specified declared field of the class or interface represented by this class object.
Field[] Getdeclaredfields (): Returns an array of field objects that reflect all the fields declared by the class or interface represented by this class object.
C. Set the field value represented by this field object on the specified object variable to the specified new value by using the Filed object method
Void Set (Object obj, Object value): Sets the field represented by this field object on the specified object variable To the specified new value.
If you want to access the object's private variables, you can also use brute force access to the field
public void Setaccessible (Boolean flag) throws SecurityException: This object's a The ccessible flag is set to the Boolean value indicated. A value of TRUE indicates that the reflected object should cancel the Java language access check when it is in use.

< pre>

5. Get member methods by reflection
A. Gets the constructor method and creates the object
B. Get method object via Bytecode object
methods GetMethod (String name, class< ... param Etertypes): Returns a Method object that reflects the specified public member methods of the class or interface represented by this class object.
Method[] GetMethods (): Returns an array containing some method objects that reflect the public membe of the class or interface represented by this class object, including those that are declared by the class or interface and that inherit from the superclass and the interface R method.
Method Getdeclaredmethod (String name, class< ... parametertypes): Returns a Method object that reflects the reference of the class or interface represented by this class object A method has been declared. The first parameter represents the method name, the second parameter represents the Class type of the parameter of the method, and the parameterless method does not require a second parameter,
method[] Getdeclaredmethods (): Returns an array of methods objects that reflect the class object Represents all methods declared by a class or interface, including public, protected, default (package) access, and private methods, but not inherited methods.
C. This method of invoking an object by means object invoke (Object obj, Object ... args): invokes the underlying method represented by this method object for the specified object with the specified parameter. The first parameter represents the object to invoke, and the second parameter represents the actual parameter that invokes the method. The parameterless method does not require a second argument

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.