JAVA Reflection Features

Source: Internet
Author: User

1. Reflection (Concept): The program can change the program structure and variable type in the run time, mainly refers to the program can access, detect and modify its own state or behavior of a capability.

2. Characteristics of Reflection:

• Determine the class to which any object belongs at run time

• Constructs an object of any class at run time

• Determine the member variables and methods that any one class has at run time

• Methods to invoke any object at run time

3. Reflection of the prequel: Class type Class class Java has a class is very special, is the class class, we can understand it as encapsulating the information of the class, many explained that class does not have a constructor, in fact, there is,

It's just that the constructor is private (the constructor is set to private, so that developers are not allowed to create instances of class classes themselves, just like the constructor in singleton mode). We can look at the source code in the JDK:

 classClass<t>Implementsjava.io.Serializable, Java.lang.reflect.GenericDeclaration, JAVA.LANG.REFLECT.TYP E, java.lang.reflect.AnnotatedElement {Private Static Final intannotation= 0x00002000; Private Static Final intENUM = 0x00004000; Private Static Final intSynthetic = 0x00001000; Private Static native voidregisternatives (); Static{registernatives (); }    /** Constructor.     Only the Java Virtual machine creates Class * objects. */    PrivateClass () {} ... ..


The note clearly tells us that this class is created with a JVM, so we don't have to bother.  If we get the type information of a class, we can use reflection to get its various members and methods. (Note: Class started more as a generic service since the JDK1.5 version) so how do we get a type of information? Suppose we have a monkey class:

4. Get the constructor for a class

First introduce the constructor class, which encapsulates the reflected constructor, class has four ways to get the constructor object

public constructor<?>[] GetConstructors () returns all the public constructor collections in the class, with the default constructor subscript 0

public constructor<t> getconstructor (class<?> .... parametertypes) returns the specified public constructor, which is a collection of constructor parameter types

public constructor<?>[] Getdeclaredconstructors () returns all constructors in the class, including the private

public constructor<t> getdeclaredconstructor (class<?> .... parametertypes)

Returns any specified constructor from the name, it is still very understood, with declared is to get all the construction methods, including the private, ha, we can call the original is not allowed to call the private constructor, see the code

/** 2 * Get Construction method constructor

3 * GetConstructor () only for public

4 * Getdeclaredconstructor () Global access All

5 *

6 * * *

7

8//Specify a parameter list to get a specific method

9 Constructor con = cls1.getdeclaredconstructor (new Class[]{string.class});

Ten con.setaccessible (TRUE); To set accessible permissions

One Object obj = con.newinstance (new object[]{"Liyang"});

System.out.println (obj); Print the information for this object

13

14//Get all the constructor methods set

Constructor con1[] = cls1.getdeclaredconstructors ();

Con1[1].setaccessible (TRUE);

Obj1 Object = con1[1].newinstance (new object[]{"Tom"});

System.out.println (OBJ1);

5. Get member variables for a class

public Field Getdeclaredfield (String name) gets the member of any given name

public field[] Getdeclaredfields () gets all the member variables

public Field GetField (String name) gets the arbitrary public member variable

public field[] GetFields () gets all the public member variables

/**2 * Get member Variable field

3 * GetField ()

4 * Getdeclaredfield ()

5 * * *

6 Field mem = Cls1.getdeclaredfield ("name");

7 mem.setaccessible (TRUE);

8 System.out.println ("We get form field:" +mem.get (obj));

6. Ways to get classes

public method[] GetMethods () Gets the collection of all common methods

Public Method GetMethod (String name,class<?> ... parametertypes) gets the specified public method parameter 1: Method name parameter 2: Parameter type collection

public method[] Getdeclaredmethods () get all the methods

Public Method Getdeclaredmethod (String name,class<?> ... parametertypes) gets any of the specified methods

7.

This article refers to: http://www.cnblogs.com/gulvzhe/archive/2012/01/27/2330001.html

Http://www.cnblogs.com/octobershiner/archive/2012/03/18/2404751.html

JAVA Reflection Features

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.