3. Introduction What does “Reflection” mean ? What’s Java Reflection API ? Java Reflection API Usage “Hello Reflection” Demo 3
4. Introduction… What does “Reflection” mean ? In computer science, reflection is the process by which a computer program can observe and modify its own structure and behavior at runtime[1]. 4
5. Introduction… 5 What’s Java Reflection API? An Java program contains many objects. Java Reflection API includes classes, interfaces support developer can retrieve and manipulate its object at runtime.
22. java.lang.Class class Kind of Java types Class object How to get a Class object How to get Class’ members object Instantiate a new object from its Class 11
23. java.lang.Class class … Kind of Java types Primitive type e.g : int, double, float, short, long, byte, char, void Reference type e.g : class, interface, array, enum, annotation 12
24. java.lang.Class class… Class object Instances of the class Class represent classes and interfaces in a running Java application. Has no public constructor. Are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader. 13
25. java.lang.Class class… How to get a Class object getClass() method .class syntax forName() static method .TYPE syntax Other methods 14
26. java.lang.Class class… How to get Class’ members object 15 Get Constructors Constructor<T> getConstructor(Class<T>… parameterTypes) Constructor<?> [] getConstructors() Constructor<T> getDeclaredConstructor(Class<T>… parameterTypes) Constructor<?> [] getDeclaredConstructors() GetFields Field getField(String fieldName) Field [] getFields() Field getDeclaredField(String fieldName) Field [] getDeclaredFields()
27. java.lang.Class class… How to get Class’ members object 16 Get Methods Method getMethod(String name, Class<T>… parameterTypes) Method [] getMethods() Method getDeclaredMethod(String name, Class<T>… parameterTypes) Method [] getDeclaredMethods(); GetModifiers int getModifiers()
28. java.lang.Class class… How to get Class’ members object 17 Get Package Package getPackage() Other methods Ref : JDK Document
41. Class member… Getting Field’s Value Example 22 Class c = Integer.class; //get a private instant field Field field = c.getDeclaredField("value"); field.setAccessible(true); System.out.format(field.get(new Integer(10))); //get a public static field field = c.getField("MAX_VALUE"); System.out.format(field.get(null));
42. Class member… Setting Field’s Value Example 23 Class c = Integer.class; Integer i = new Integer(10); //get a private instant field Field field = c.getDeclaredField("value"); field.setAccessible(true); System.out.format(field.set(i, new Integer(20))); //get a public static field field = c.getField("MAX_VALUE"); System.out.format(field.set(null, new Integer(20)));
43. Class member… Method Get information Class<T> getDeclaringClass() Class<T> [] getParameterTypes() int getModifiers() Class<T> getReturnType() Invoke method Object invoke(Object obj, Object … initArgs) 24
44. Class member… Invoke a method example 25 Method f = Math.class.getMethod("sqrt", new Class[]{double.class}); Object[] args = {new Double(10)}; //invoke a static method Double d = (Double) f.invoke(null, args); double y = d.doubleValue(); System.out.println(y);