Java reflection mechanism analysis (2)-functions and Examples

Source: Internet
Author: User

From the Java reflection mechanism analysis (I) API, we can see many interfaces and classes. What can we do through these interfaces?


From the previous API, we can see that it can complete the following functions:


1) obtain the class

A. Use getclass () (each class has this function)

I. String STR = "ABC ";

Ii. Class C1 = Str. getclass ();

 

B. Use class. forname () (static method is most often used)

I. Class C1 = Class. forname ("Java. Lang. String ");

Ii. Class C2 = Class. forname ("Java. AWT. Button ");

C. Use the. Class syntax

I. Class C1 = string. Class;

Ii. Class C2 = java. AWT. Button. Class;

Iii. Class C4 = int. Class;

Iv. Class C5 = int []. Class;

D. Use the type syntax (the type syntax of the primitive Wrapper Classes basic data type package class)

I. Class C1 = Boolean. type;

 

2) Obtain attributes (for examples of this function, see Java reflection mechanism analysis (2 ))

The following four methods can be used to obtain attributes. For more information, see java. Lang. Class.

 

Public field getfield

(String name)

Returns a Field object that reflects the specified public member fields of the class or interface represented by this class object.

Public field [] getfields ()

Returns an array containing some field objects. These objects reflect all accessible public fields of the class or interface represented by this class object.

Public Field

Getdeclaredfield (string name)

Returns a Field object that reflects the declared fields specified by the class object or interface.

Public field []

Getdeclaredfields ()

Returns an array of the Field object. These objects reflect all fields declared by the class object or interface.

 

3) Obtain method ()

The following four methods can be obtained. For details, refer to Java. Lang. Class.


Public Method

Getmethod (string name,

...)

Returns a method object that reflects the specified public member methods of the class or interface represented by this class object.

Public method [] getmethods ()

Returns an array containing some method objects, these objects reflect the public member methods of the classes or interfaces represented by this class Object (including those declared by the class or interface and those inherited from the superclass and superinterfaces ).

Public Method

Getdeclaredmethod (string name ,...)

Returns a method object that reflects the declared methods specified by the class object or interface.

Public method []

Getdeclaredmethods ()

Returns an array of method objects. These objects reflect all the methods declared by the class object or interface, including public, protected, default (Package) Access, and private methods, but does not include the inherited methods.

 

4) Get the constructor of the class

The following four methods can be obtained. For details, refer to Java. Lang. Class.


Public Constructor

Getconstructor (class <?>... )

Returns a constructor object that reflects the specified public constructor of the Class Object.

Public constructor <?> []

Getconstructors ()

Returns an array containing certain constructor objects. These objects reflect all the common constructor methods of the classes represented by this class object.

Public constructor <t>

Getdeclaredconstructor (class <?> ...)

Returns a constructor object that reflects the specified constructor of the class object or interface.

Public constructor <?> []

Getdeclaredconstructors ()

Returns an array of constructor objects that reflect all constructor methods of class declarations represented by this class object. They are public, protected, default (Package) Access, and private constructor

Sample Code:

Import Java. lang. reflect. constructor; </P> <p> public class testconstructor {<br/>/** <br/> * @ Param ARGs <br/> * @ throws classnotfoundexception <br/> *@ throws securityexception <br/> * @ throws nosuchmethodexception <br/> */<br/> Public static void main (string [] ARGs) throws exception {<br/> // todo auto-generated method stub <br/> class classtype = Class. forname (ARGs [0]); <br/> constructor = classtype. getconstructor (); </P> <p> system. out. println (constructor. tostring (); </P> <p >}</P> <p>}

 

5) create a class instance

 

Call the newinstance method of the Class Object of the class.

Example:

Import Java. lang. reflect. constructor; </P> <p> public class testconstructor {<br/>/** <br/> * @ Param ARGs <br/> * @ throws classnotfoundexception <br/> *@ throws securityexception <br/> * @ throws nosuchmethodexception <br/> */<br/> Public static void main (string [] ARGs) throws exception {<br/> // todo auto-generated method stub <br/> class classtype = Class. forname (ARGs [0]); <br/> // constructor = classtype. getconstructor (); <br/> Object inst = classtype. newinstance (); <br/> system. out. println (insT); <br/>}< br/>}

Call the newinstance method of the default constructor object

Example:

Import Java. lang. reflect. constructor; </P> <p> public class testconstructor {<br/>/** <br/> * @ Param ARGs <br/> * @ throws classnotfoundexception <br/> *@ throws securityexception <br/> * @ throws nosuchmethodexception <br/> */<br/> Public static void main (string [] ARGs) throws exception {<br/> // todo auto-generated method stub <br/> class classtype = Class. forname (ARGs [0]); <br/> constructor = classtype. getconstructor (); <br/> Object inst = constructor. newinstance (); <br/> system. out. println (insT); <br/>}< br/>}

Call the newinstance method with the constructor object

Example:

Class classtype = user. class <br/> constructor constructor2 = <br/> classtype. getdeclaredconstructor (Int. class, String. class); </P> <p> Object inst = constructor2.newinstance (1, "123"); </P> <p> system. out. println (insT );

 

Related Article

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.