[Java reflection mechanism] _ reflection Application -- get the structure notes of the class

Source: Internet
Author: User

[Java reflection mechanism] _ reflection Application -- get the structure notes of the class

Objectives of this chapter:
You can use reflection to obtain all the Implemented interfaces.
You can use reflection to obtain the parent class inherited by a class.
You can use reflection to obtain all the constructor methods of a class.
You can use reflection to obtain all the methods of a class.
You can use reflection to obtain all attributes of a class.

Details

In-depth reflection mechanism-get the class structure

In actual development, the above program is the place where reflection should be the most. Of course, the reflection mechanism provides far more functions than that, and a complete class structure can be obtained through reflection, in this way, Java is used. lang. the reflect package contains the following classes:

Constructor: indicates the constructor in the class.
Field: indicates the attributes in the class.
Method: indicates the method in the class.

These three classes are subclasses of the accessibleobject class.

Operation class:

Package Org. lxh. demo15; interface China {// define the China interface public static final string National = "China"; // define the global constant public static final string author = "Niuer grazing "; // define the global constant public void saychina (); // Public String sayhello (string name, int age) without parameters and return values ); // define the method with two parameters and return the content} public class person implements China {private string name; private int age; Public Person () {// No parameter construction} public person (string name) {This. nam E = Name; // set the name attribute} public person (string name, int age) {This (name); this. age = age;} public void saychina () {system. out. println ("Author:" + author + ", nationality:" + national);} Public String sayhello (string name, int age) {return name + ", hello! I am: "+ age +" years old! ";} Public void setname (string name) {This. name = Name;} public void setage (INT age) {This. age = age;} Public String getname () {return this. name;} public int getage () {return this. age ;}}

3.1 obtain all interfaces implemented by the class

To obtain all the interfaces implemented by a class, you must use the getinterfaces () method in the class. This method is defined as follows:
Public class [] getinterfaces ()
This method returns an array of objects of the class. Then, you can directly use the getname () method in the class to output the array.

If you want to obtain all the interfaces implemented by a class, you can use
Public class <?> [] Getinterfaces ()

Because a class can implement multiple interfaces at the same time, all interfaces implemented are returned in the form of an array.

Package org. lxh. demo15.classinfodemo; public class getinterfacedemo {public static void main (string ARGs []) {class <?> C1 = NULL; // declare the class object try {C1 = Class. forname ("org. lxh. demo15.person "); // instantiated object} catch (classnotfoundexception e) {e. printstacktrace ();} class <?> C [] = c1.getinterfaces (); For (INT I = 0; I <C. length; I ++) {system. out. println ("Name of the implemented interface:" + C [I]. getname (); // name of the output interface }}}
3.2. Obtain the parent class inherited by the class.

Format:
Public class <? Super T> getsuperclass ()
This method returns the class instance location, which is the same as the previously obtained interface. You can get the name through the getname () method.

Obtain the parent class

Package org. lxh. demo15.classinfodemo; public class getsuperclassdemo {public static void main (string ARGs []) {class <?> C1 = NULL; try {C1 = Class. forname ("org. lxh. demo15.person");} catch (classnotfoundexception e) {e. printstacktrace ();} class <?> C2 = c1.getsuperclass (); // obtain the parent class system. Out. println ("parent class name:" + c2.getname ());}}

3.3 obtain all constructor methods in the class.

Package org. lxh. demo15.classinfodemo; import java. Lang. Reflect. constructor; public class getconstructordemo01 {public static void main (string ARGs []) {class <?> C1 = NULL; try {C1 = Class. forname ("org. lxh. demo15.person");} catch (classnotfoundexception e) {e. printstacktrace ();} constructor <?> CON [] = c1.getconstructors (); For (INT I = 0; I <con. length; I ++) {system. out. println ("constructor:" + con [I]); // output constructor, printed directly }}}

The constructor class has the following methods:

Get modifier: Public int getmodifiers ()
Get Method Name: Public String getname ()
Type of the obtained parameter: public class <?> [] Getparametertypes ()

Package org. lxh. demo15.classinfodemo; import java. Lang. Reflect. constructor; public class getconstructordemo02 {public static void main (string ARGs []) {class <?> C1 = NULL; try {C1 = Class. forname ("org. lxh. demo15.person");} catch (classnotfoundexception e) {e. printstacktrace ();} constructor <?> CON [] = c1.getconstructors (); For (INT I = 0; I <con. length; I ++) {class <?> P [] = CON [I]. getparametertypes (); // obtain all system parameters in the constructor. out. print ("constructor:"); // output structure, print system directly. out. print (CON [I]. getmodifiers () + ""); // get the modifier system. out. print (CON [I]. getname (); // obtain the constructor name system. out. print ("("); For (Int J = 0; j <p. length; j ++) {system. out. print (P [J]. getname () + "Arg" + I); If (j <p. length-1) {system. out. print (","); // output ","} system. out. println ("){}");}}}

After the operation, all modifiers are converted to numbers.

The following restoration Modifier

Public static string tostring (INT mod)
Int mo = CON [I]. getmodifiers (); // obtain the permission
System. Out. Print (modifier. tostring (Mo) + ""); // restore Permissions

The following changes are made:

Package Org. lxh. demo15.classinfodemo; import Java. lang. reflect. constructor; import Java. lang. reflect. modifier; public class getconstructordemo03 {public static void main (string ARGs []) {class <?> C1 = NULL; try {C1 = Class. forname ("org. lxh. demo15.person");} catch (classnotfoundexception e) {e. printstacktrace ();} constructor <?> CON [] = c1.getconstructors (); For (INT I = 0; I <con. length; I ++) {class <?> P [] = CON [I]. getparametertypes (); // obtain all system parameters in the constructor. out. print ("constructor:"); // output structure, print int mo = CON [I] directly. getmodifiers (); // obtain the permission system. out. print (modifier. tostring (Mo) + ""); // restore the permission system. out. print (CON [I]. getname (); // obtain the constructor name system. out. print ("("); For (Int J = 0; j <p. length; j ++) {system. out. print (P [J]. getname () + "Arg" + I); If (j <p. length-1) {system. out. print (","); // output ","} system. out. println ("){}");}}}

3.4 obtain methods in the class

Public method [] getdeclaredmethods () throws securityexception // output all methods of this class
Public method [] getmethods () throws securityexception // output all methods of this class

Method operation:
Get all returned values: public class <?> Getreturntype ()
Obtain all the parameters: public class <?> [] Getparametertypes ()
Get modifier: Public int getmodifiers ()
Get exception information: public class <?> [] Getexceptiontypes ()

Package Org. lxh. demo15.classinfodemo; import Java. lang. reflect. method; // import the constructor package import Java. lang. reflect. modifier; // import the constructor package public class getmethoddemo {public static void main (string ARGs []) {class <?> C1 = NULL; // declare the class object try {C1 = Class. forname ("org. lxh. demo15.person "); // instantiated object} catch (classnotfoundexception e) {e. printstacktrace ();} method M [] = c1.getmethods (); // obtain all methods for (INT I = 0; I <m. length; I ++) {class <?> R = m [I]. getreturntype (); // class of the returned value type <?> P [] = m [I]. getparametertypes (); // get the type of all parameters int xx = m [I]. getmodifiers (); // get the modifier system. out. print (modifier. tostring (XX) + ""); // output modifier system. out. print (R + ""); system. out. print (M [I]. getname (); system. out. print ("("); For (Int J = 0; j <p. length; j ++) {system. out. print (P [J]. getname () + "" + "Arg" + J); If (j <p. length-1) {system. out. print (",") ;}} class <?> Ex [] = m [I]. getexceptiontypes (); // retrieve the exception if (ex. length> 0) {system. out. print (") throws");} else {system. out. print (")") ;}for (Int J = 0; j <ex. length; j ++) {system. out. print (ex [J]. getname (); If (j <p. length-1) {system. out. print (",") ;}} system. out. println ();}}};
3.5. Obtain attributes in the class

Public field [] getfields () throws securityexception
Get all attributes in this class: public field [] getdeclaredfields () throws securityexception

Package Org. lxh. demo15.classinfodemo; import Java. lang. reflect. field; // import the constructor package import Java. lang. reflect. modifier; // import the constructor package public class getfielddemo {public static void main (string ARGs []) {class <?> C1 = NULL; // declare the class object try {C1 = Class. forname ("org. lxh. demo15.person "); // instantiated object} catch (classnotfoundexception e) {e. printstacktrace () ;}{// attribute field F [] = c1.getdeclaredfields (); // obtain the attribute for (INT I = 0; I <F. length; I ++) {class <?> R = f [I]. getType (); // get the attribute type int mo = f [I]. getmodifiers (); // The number of the modifier string priv = modifier. tostring (Mo); // restores the modifier system. out. print ("attributes of this class:"); system. out. print (priv + ""); system. out. print (R. getname () + ""); // obtain the property type system. out. print (F [I]. getname (); // name of the Output property system. out. println (";") ;}{// public attribute field F [] = c1.getfields (); // obtain the public attribute for (INT I = 0; I <F. length; I ++) {class <?> R = f [I]. getType (); // get the attribute type int mo = f [I]. getmodifiers (); // The number of the modifier string priv = modifier. tostring (Mo); // restores the modifier system. out. print ("Public attributes:"); system. out. print (priv + ""); system. out. print (R. getname () + ""); // obtain the property type system. out. print (F [I]. getname (); // name of the Output property system. out. println (";");}}}};

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.