For a complete structural class by reflecting
To get the properties of each section: Permissions modifier variable type variable names, etc.
Acquiring runtime class methods
annotation, permission modifier, return type, method name, parameter list, and abnormal
TestConstructor
package com.aff.reflection; import org.junit.Test; public class TestConstructor { @Test public void test1() throws Exception { Clazz class . = The Person class ; / * create objects corresponding runtime class using the newInstance (), is actually called empty argument runtime class constructor Want to be able to create success: ① required runtime class corresponding to free-argument constructor ② permission constructor modifier permission to be enough */ Object obj = clazz.newInstance(); Person p = (Person) obj; System.out.println(p);//Person [name=null, age=0] } }
TestField
package com.aff.reflection; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import org.junit.Test; public class TestField { @Test public void test1() { Clazz class = the Person. Class ; // getFields (): Get the only run-time class and parent class declared as public attributes Field, [] = fields1 clazz.getFields (); System.out.println(fields1); System.out.println(); // 2.getDeclaredFields (): Get all the run attribute class declaration itself Field, [] = fields2 clazz.getDeclaredFields (); for (Field, F: fields2) { System.out.println(fields2); } } // permission modifier variable type variable name // each section to get the properties @Test public void test2 () { Class clazz = Person.class; Field, [] fields2 = clazz.getDeclaredFields (); for (Field, F: fields2) { // 1. Each property rights acquired modifier int I = f.getModifiers (); String str = Modifier.toString(i); System.out.println(str); // 2. Get property variable type Class type = f.getType (); System.out.println(type.getName() + ""); // 3. Get the property name System.out.println (f.getName ()); System.out.println(); } } // Get Method runtime class // annotation, permission modifier, return type, method name, parameter list, abnormal @Test public void Test3 () { Clazz class . = The Person class ; // 1.getMethods (): Get all runtime class and its parent class declarations for methods public Method, [] = M1 clazz.getMethods (); for (Method, m: M1) { System.out.println(m); } System.out.println("----------------------"); // getDeclaredMethods (): Get the runtime class itself all methods declared Method, [] M2 = clazz.getDeclaredMethods (); for (Method, m: M2) { // 1. Annotations the Annotation [] = Ann m.getAnnotations () ; for (the Annotation A: Ann) { System.out.println(a + "--"); } // 2. permission modifier String str = Modifier.toString (m.getModifiers ()); System.out.print(str + "--"); // 3. Return Value Type Class = the returnType m.getReturnType (); System.out.print(returnType.getName() + "--"); // 4.方法名 System.out.print(m.getName() + "--"); // 5.形参列表 // System.out.print("("); Class[] param = m.getParameterTypes(); for (int i = 0; i < param.length; i++) { System.out.print("(" + param[i].getName() + " args-" + i + ")"); } // 6. Exception Type Class [] = EXPS m.getExceptionTypes (); IF (! Exps.length = 0 ) { System.out.println("throws"); } for (int i = 0; i < exps.length; i++) { System.out.print(exps[i].getName() + "--"); } System.out.println(); } } }