2. Java's Reflection API's makes it possible to inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time It is also possible to instantiate new objects, invoke methods and get/set field values using reflection What is reflection?
3. Using Java Reflection you can inspect Java classes at runtime From the classes you can obtain information about Class Name Class Modifies (public, private, synchronized etc.) Package Info Superclass Implemented Interfaces Constructors Methods Fields Annotations Classes
4. First step in reflection is to get hold of the Class object When you know the name at compile time Class myObjectClass = MyObject.class If you don't know the name at compile time, but have the class name as a string at runtime or from some configuration file String className = Class.forName(className); className = fully qualified class name ClassNotFoundException Class object
5. From a Class object you can obtain its name in two versions Fully qualified String className = aClass.getName(); Simple name String simpleClassName = aClass.getSimpleName(); Class Name
6. You can access the modifiers of a class via the Class object The class modifiers are the keywords "public", "private", "static" etc int modifiers = aClass.getModifiers(); You can check the modifiers using these methods in the class java.lang.reflect.Modifiers: Modifiers.isAbstract(int modifiers) Modifiers.isFinal(intmodifiers) Modifiers.isInterface(intmodifiers) Modifiers.isNative(intmodifiers) Modifiers.isPrivate(intmodifiers) Modifiers.isProtected(intmodifiers) Modifiers.isPublic(intmodifiers) Modifiers.isSynchronized(intmodifiers) Modifiers.isTransient(intmodifiers) Modifiers
8. Class superclass = aClass.getSuperclass(); The superclass class object is a Class object like any other, so you can continue doing class reflection on that too. Superclass
9. Class[] interfaces = aClass.getInterfaces(); A class can implement many interfaces. Therefore an array of Class is returned. Interfaces are also represented by Class objects in Java Reflection. To get a complete list of the interfaces implemented by a given class you will have to consult both the class and its superclasses recursively. Implemented interfaces
10. Constructor[] constructors = aClass.getConstructors(); Method[] method = aClass.getMethods(); Method[] method = aClass.getFields(); Annotation[] annotations = aClass.getAnnotations(); Other information
11. Gettting specific constructor Constructor constructor = aClass.getConstructor(new Class[]{String.class}); If no constructor matches the given constructor arguments, in this case String.class, a NoSuchMethodException is thrown. You can read what parameters a given constructor takes like this: Class[] parameterTypes = constructor.getParameterTypes(); Constructor
12. Constructor constructor = MyObject.class.getConstructor(String.class); MyObjectmyObject = (MyObject) constructor.newInstance("constructor-arg1"); Instantiate object sans new
13. Class aClass = ...//obtain class object Field[] fields = aClass.getFields(); The Field[] array will have one Field instance for each public field declared in the class If you know the name of the field you want to access, you can access it like this Class aClass = MyObject.class Field field = aClass.getField("someField"); Fields
14. String fieldName = field.getName(); Object fieldType = field.getType(); Getting and Setting Field Values Class aClass = MyObject.class Field field = aClass.getField("someField"); MyObjectobjectInstance = new MyObject(); Object value = field.get(objectInstance); field.set(objetInstance, value); Fields
15. Class aClass = ...//obtain class object Method[] methods = aClass.getMethods(); The Method[] array will have one Method instance for each public method declared in the class. Get the public method named "doSomething“ Method method = aClass.getMethod("doSomething", new Class[]{String.class}); NoSuchMethodException Get no argument method Method method = aClass.getMethod("doSomething", null); Methods