Your SlideShare is downloading. ×
19   reflection
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

19 reflection

833
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
833
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
32
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Reflection
    DhrubojyotiKayal
  • 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
  • 7. Package package = aClass.getPackage();
    Package Information
  • 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
  • 16. Method parameters
    parameterTypes= method.getParameterTypes();
    Return type
    Class returnType = method.getReturnType();
    Methods
  • 17. //get method that takes a String as argument Method method = MyObject.class.getMethod("doSomething", String.class);
    Object returnValue = method.invoke(myInstance, "parameter-value1");
    Execute a static method?
    Invoking methods
  • 18. Dynamic code addition at runtime
    Building frameworks.
    Why is reflection important?
  • 19. Q&A