How does Java obtain the attributes and types of a class?

Source: Internet
Author: User

Import java. Io. bufferedreader;
Import java. Io. inputstreamreader;
Import java. Lang. Reflect. method;
Import java. Lang. Reflect. modifier;

Public class test {

Public static void main (string [] ARGs) throws exception {
// Bufferedreader BB = new bufferedreader (New inputstreamreader (system. In ));
// String classname = BB. Readline ();
Class C = Class. forname ("arraylisttest. Java ");
// ------------------- Obtain detailed information about the method
Method M [] = C. getdeclaredmethods ();
For (INT I = 0; I <M. length; I ++)
{
// -------------------- Obtain the method name
System. Out. println ("method name:" + M [I]. getname ());
// -------------------- Obtain the type of the method parameter and several parameters
Class B [] = m [I]. getparametertypes (); // obtain all the parameters and store them in array B.
For (Int J = 0; j <B. length; j ++)
{
System. Out. println ("the parameter type is" + B [J]);
}
// -------------------- Obtain the type of the method Return Value
System. Out. println (M [I]. getreturntype (); // obtain the return value type of the method.
// -------------------- Get the modifier OF THE METHOD
Int mod = m [I]. getmodifiers ();
System. Out. println ("method modifiers include" + modifier. tostring (MOD ));
// -------------------- Obtain the exception TYPE OF THE METHOD
Class E [] = m [I]. getexceptiontypes (); // obtain all Exception types and store them in array E.
For (int K = 0; k <E. length; k ++)
{
System. Out. println ("the exception type of the method is:" + E [k]);
}
System. Out. println ("-------------------------------------------------------------------");
}
// ------------------------------ Obtain the attribute details

}

}
Package class // test. com. tc. Test. Class's forname method to obtain the attribute information;

Import java. Lang. Reflect. field;
Import java. Lang. Reflect. modifier;

Public class test {

Public static void main (string [] ARGs) throws classnotfoundexception {
Class C = Class. forname ("class test. com. TC. test. the forname method of class obtains the attribute information. student "); // load the class to be used into the memory, and store all information about this class in Object C
Field F [] = C. getdeclaredfields (); // extract the attribute information and store it in the object of the field class. Because each Field object can only store the information of one attribute, you must use an array to receive the information.
For (INT I = 0; I <F. length; I ++)
{
System. Out. println ("the attribute name is:" + F [I]. getname (); // obtain the attribute name
System. Out. println ("the attribute type is:" + F [I]. GetType (); // obtain the attribute type
Int mod = f [I]. getmodifiers (); // get the total number of modifiers.
System. Out. println (MOD );
System. Out. println ("modifier of the attribute:" + modifier. tostring (MOD); // The modifier class can calculate the modifiers based on the total number of constants.
System. Out. println ("-------------------------------------------------------");
}

}

}

Article Source: http://blog.csdn.net/vipliyaohua/article/details/6619919

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.