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