1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| public class ReflectTest {
private String className = null;
private Class personClass = null;
@Before public void init() throws ClassNotFoundException { className = "com.jinzl.reflect.Person"; personClass = Class.forName(className); }
@Test public void getClassName(){ System.out.println(personClass); System.out.println(Person.class); }
@Test public void getNewInstance() throws IllegalAccessException, InstantiationException { System.out.println(personClass.newInstance()); }
@Test public void getPublicConstructorTest() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { Constructor constructor = personClass.getConstructor(String.class, Integer.class); Person person = (Person) constructor.newInstance("jinzili", 18); System.out.println(person); }
@Test public void getPrivateConstructorTest() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { Constructor constructor = personClass.getDeclaredConstructor(String.class); constructor.setAccessible(true); Person person = (Person) constructor.newInstance("jinzili"); System.out.println(person); }
@Test public void getPublicField() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException { Constructor constructor = personClass.getConstructor(String.class, Integer.class); Person person = (Person) constructor.newInstance("jinzili", 18);
Field field = personClass.getField("phone"); field.set(person, "18721317074"); System.out.println(person); }
@Test public void getPrivateField() throws Exception { Constructor constructor = personClass.getConstructor(String.class, Integer.class); Person person = (Person) constructor.newInstance("jinzili", 18);
Field field = personClass.getDeclaredField("name"); field.setAccessible(true); field.set(person, "jinzili"); System.out.println(person); }
@Test public void getPublicMethod() throws Exception{ Object obj = personClass.newInstance(); Object object = personClass.getMethod("toStringCustom").invoke(obj); System.out.println(object); }
@Test public void getPrivateMethod() throws Exception{ Constructor constructor = personClass.getConstructor(String.class, Integer.class); Object obj = constructor.newInstance("jinzili", 18); Method method = personClass.getDeclaredMethod("getName"); method.setAccessible(true); Object value = method.invoke(obj); System.out.println(value); }
@Test public void getOtherMethod() throws Exception{ System.out.println(personClass.getClassLoader()); Class[] interfaces = personClass.getInterfaces(); for(Class _interface : interfaces){ System.out.println(_interface); } System.out.println(personClass.getGenericSuperclass()); System.out.println(personClass.isArray()); System.out.println(new String[3].getClass().isArray()); System.out.println(personClass.isEnum()); System.out.println(personClass.isInterface()); }
}
|