19 reflection

1,177 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,177
On SlideShare
0
From Embeds
0
Number of Embeds
15
Actions
Shares
0
Downloads
34
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

19 reflection

  1. 1. Reflection<br />DhrubojyotiKayal<br />
  2. 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<br />It is also possible to instantiate new objects, invoke methods and get/set field values using reflection<br />What is reflection?<br />
  3. 3. Using Java Reflection you can inspect Java classes at runtime<br />From the classes you can obtain information about <br />Class Name<br />Class Modifies (public, private, synchronized etc.)<br />Package Info<br />Superclass<br />Implemented Interfaces<br />Constructors<br />Methods<br />Fields<br />Annotations<br />Classes<br />
  4. 4. First step in reflection is to get hold of the Class object<br />When you know the name at compile time<br />Class myObjectClass = MyObject.class<br />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<br />String className = Class.forName(className); <br />className = fully qualified class name<br />ClassNotFoundException<br />Class object<br />
  5. 5. From a Class object you can obtain its name in two versions<br />Fully qualified<br />String className = aClass.getName(); <br />Simple name<br />String simpleClassName = aClass.getSimpleName(); <br />Class Name<br />
  6. 6. You can access the modifiers of a class via the Class object<br />The class modifiers are the keywords "public", "private", "static" etc<br />int modifiers = aClass.getModifiers(); <br />You can check the modifiers using these methods in the class java.lang.reflect.Modifiers: <br />Modifiers.isAbstract(int modifiers) <br />Modifiers.isFinal(intmodifiers) <br />Modifiers.isInterface(intmodifiers) <br />Modifiers.isNative(intmodifiers) <br />Modifiers.isPrivate(intmodifiers) <br />Modifiers.isProtected(intmodifiers) <br />Modifiers.isPublic(intmodifiers) <br />Modifiers.isSynchronized(intmodifiers) <br />Modifiers.isTransient(intmodifiers) <br />Modifiers<br />
  7. 7. Package package = aClass.getPackage();<br />Package Information<br />
  8. 8. Class superclass = aClass.getSuperclass(); <br />The superclass class object is a Class object like any other, so you can continue doing class reflection on that too. <br />Superclass<br />
  9. 9. Class[] interfaces = aClass.getInterfaces(); <br />A class can implement many interfaces. Therefore an array of Class is returned. <br />Interfaces are also represented by Class objects in Java Reflection. <br />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. <br />Implemented interfaces<br />
  10. 10. Constructor[] constructors = aClass.getConstructors(); <br />Method[] method = aClass.getMethods(); <br />Method[] method = aClass.getFields(); <br />Annotation[] annotations = aClass.getAnnotations(); <br />Other information<br />
  11. 11. Gettting specific constructor<br />Constructor constructor = aClass.getConstructor(new Class[]{String.class}); <br />If no constructor matches the given constructor arguments, in this case String.class, a NoSuchMethodException is thrown. <br />You can read what parameters a given constructor takes like this: <br />Class[] parameterTypes = constructor.getParameterTypes(); <br />Constructor<br />
  12. 12. Constructor constructor = MyObject.class.getConstructor(String.class); <br />MyObjectmyObject = (MyObject) constructor.newInstance("constructor-arg1"); <br />Instantiate object sans new<br />
  13. 13. Class aClass = ...//obtain class object <br />Field[] fields = aClass.getFields(); <br />The Field[] array will have one Field instance for each public field declared in the class<br />If you know the name of the field you want to access, you can access it like this<br />Class aClass = MyObject.class<br />Field field = aClass.getField("someField"); <br />Fields<br />
  14. 14. String fieldName = field.getName(); <br />Object fieldType = field.getType();<br />Getting and Setting Field Values<br />Class aClass = MyObject.class Field field = aClass.getField("someField"); MyObjectobjectInstance = new MyObject(); Object value = field.get(objectInstance); field.set(objetInstance, value); <br />Fields<br />
  15. 15. Class aClass = ...//obtain class object Method[] methods = aClass.getMethods(); <br />The Method[] array will have one Method instance for each public method declared in the class. <br />Get the public method named "doSomething“<br />Method method = aClass.getMethod("doSomething", new Class[]{String.class}); <br />NoSuchMethodException<br />Get no argument method<br />Method method = aClass.getMethod("doSomething", null); <br />Methods<br />
  16. 16. Method parameters<br />parameterTypes= method.getParameterTypes(); <br />Return type<br />Class returnType = method.getReturnType(); <br />Methods<br />
  17. 17. //get method that takes a String as argument Method method = MyObject.class.getMethod("doSomething", String.class); <br />Object returnValue = method.invoke(myInstance, "parameter-value1"); <br />Execute a static method?<br />Invoking methods<br />
  18. 18. Dynamic code addition at runtime<br />Building frameworks.<br />Why is reflection important?<br />
  19. 19. Q&A<br />

×