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 123 124 125
| import java.lang.reflect.*; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.List;
public class Main { public static void main(String[] args){ try { Class<?> clazz = Class.forName("Person"); Constructor[] constructors = clazz.getConstructors(); System.out.println("\nPublic constructors\n"); printIterable(Arrays.asList(constructors)); Constructor constructor = clazz.getConstructor(); Person person = (Person) constructor.newInstance(); System.out.println(person);
Constructor[] allConstructors = clazz.getDeclaredConstructors(); System.out.println("\nAll constructors:\n"); printIterable(Arrays.asList(allConstructors)); Constructor constructor1 = clazz.getDeclaredConstructor(String.class, String.class, String.class); constructor1.setAccessible(true); person = (Person) constructor1.newInstance("China", "GuangDong", "Peng-YM"); System.out.println(constructor1.getModifiers()); System.out.println(person);
System.out.println("\nPublic fields\n"); Field[] fields = clazz.getFields(); printIterable(Arrays.asList(fields));
System.out.println("\nAll fields\n"); Field[] allFields = clazz.getDeclaredFields(); printIterable(Arrays.asList(allFields));
Field age = clazz.getDeclaredField("age"); age.setAccessible(true);
person.setAge(-1); System.out.println(person);
age.setInt(person, -1); System.out.println(person);
Field finalField = clazz.getDeclaredField("finalValue"); finalField.setAccessible(true);
finalField.set(person, "modified-final"); System.out.println(person);
Field modifierField = Field.class.getDeclaredField("modifiers"); modifierField.setAccessible(true); modifierField.setInt(finalField, finalField.getModifiers() & ~Modifier.FINAL);
finalField.set(person, "modified-final"); System.out.println(person);
modifierField.setInt(finalField, Modifier.PUBLIC);
Field inlineFinal = clazz.getDeclaredField("inlineFinalValue"); inlineFinal.setAccessible(true); modifierField.setInt(inlineFinal, inlineFinal.getModifiers() & ~Modifier.FINAL); inlineFinal.set(person, "modified-inline-final"); System.out.println(person);
Field staticField = clazz.getDeclaredField("staticValue"); staticField.setAccessible(true); staticField.set(person, "modified-static"); System.out.println(person);
System.out.println("\nPublic methods\n"); Method[] methods = clazz.getMethods(); printIterable(Arrays.asList(methods));
System.out.println("\nAll Methods\n"); Method[] allMethods = clazz.getMethods(); printIterable(Arrays.asList(allMethods));
Method getMobile = clazz.getDeclaredMethod("getMobile", int.class); getMobile.setAccessible(true); getMobile.invoke(person, 123);
Method classMethod = clazz.getDeclaredMethod("getStaticValue"); System.out.println("\nThe static value is: " + classMethod.invoke(person));
Method genericMethod = clazz.getDeclaredMethod("genericTypeTest", HashMap.class, List.class); Type[] genericParameterTypes = genericMethod.getGenericParameterTypes(); System.out.println("\nGeneric Types"); printIterable(Arrays.asList(genericParameterTypes)); ParameterizedType parameterizedType = (ParameterizedType) genericParameterTypes[0]; System.out.println("\nRaw Type: " + parameterizedType.getRawType()); Type[] actualTypes = parameterizedType.getActualTypeArguments(); System.out.println("\nActual Types:\n"); printIterable(Arrays.asList(actualTypes));
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException | NoSuchFieldException e) { e.printStackTrace(); } }
private static void printIterable(Collection<?> iterable){ for (Object i: iterable){ System.out.println(i); } } }
|