[JavaSE] basic reflection-Class operations, javase-class
Get the class name
How to obtain this class
Return Value Type of the method.
Get method name
Type of parameters for Obtaining Methods
Package com. tsh. reflect; import java. lang. reflect. method; public class ReflectDemo {public static void main (String [] args) {// int Class class c1 = int. class; // String Class type. The Byte Code of String is class c2 = String. class; Class c3 = double. class; Class c4 = void. class; System. out. println (c1.getName (); // output int System. out. println (c2.getName (); // output java. lang. string ReflectDemo reflectDemo = new ReflectDemo (); printClass (refle CtDemo);}/*** print Class information * @ param obj */public static void printClass (Object obj) {Class c = obj. getClass (); System. out. println ("Class Name:" + c. getName (); // obtain the public Method of the class. Method [] methods = c. getMethods (); for (Method method: methods) {// obtain the returned value type Class returnTypeClass = method. getReturnType (); String returnType = returnTypeClass. getSimpleName (); // obtain the method name String methodName = method. getName (); // obtain the parameter type. Ss [] paramClass = method. getParameterTypes (); String params = new String (); int I = 0; for (Class param: paramClass) {if (I! = 0) {params + = "," + param. getSimpleName ();} else {params + = param. getSimpleName ();} I ++;} System. out. println (returnType + "" + methodName + "(" + params + ")");}}}
Returned results
Class Name: com. tsh. reflect. reflectDemovoid main (String []) void printClass (Object) void wait (long, int) void wait (long) void wait () boolean equals (Object)