Tuesday, August 2, 2016

Java Reflection

Java Reflection is used to examine or modify the run time behavior of class at run time. Java Reflection is quite powerful and useful. Reflection enables us to:
  • Examine an object's class at runtime
  • Construct an object for a class at runtime
  • Examine a class's field and method at runtime
  • Invoke any method of an object at runtime
The most common usage of Java reflection is Junit annotations. Its looks through methods and if @Test annotation appears JUnit will execute those methods as test methods.   java.lang and java.lang.reflect packages are provides the classes for reflection. Using java.lang.Class class we can get metadata of a class at run time. To start, as first step we have to obtain the java.lang.Class object of the class that  need to inspect. We can get it in three ways. 
  1. Using .class -  If know the class name at compile time can use.
    • Class classObject= ReflectionSample.class
  2. Using forName() - static method of Class class. To use this, we need to provide fully qualified class name (Ex: com.javaReflection.ReflectionSample). It loads a class of a given name, using the current class loader.  It throws a ClassNotFoundException if the class cannot be found on the classpath at runtime. 
    • Class classObject = Class.forName(“ReflectionSample”); // ReflectionSample=class name
  3. Using getClass() – it’s a method of object class, It returns the instance of Class class.               
    • ReflectionSample sample= new ReflectionSample                              Class classObject= sample.getClass();
Using the class object, we created we can get general information about the class. 
  • To get the name 
    • String name= classObject.getSimpleName(); //it will return the class name 
    • String name= classObject.getCanonicalName(); //return the canonical name of the underlying class as defined by the Java Language Specification 
    • String name= classObject.getName(); //returns fully qualified class name (including package name)
  • To get the constructors
    • Constructor [] constructors = classObject.getConstructors(); // return public, protected, private constructors.
    • Constructor [] constructors = classObject.getDeclaredConstructors(); //return public constructors 
  • To get the methods
    • Method [] methods = classObject.getMethods(); // return public and inherited from supperclass 
    • Method [] methods = classObject.getDeclaredMethods(); // return public, protected, and private methods, but excludes inherited methods.
  • To get the class fields 
    • Field [] fields= classObject.getfields(); // return public and inherited from supperclass 
    • Field [] fields= classObject.getDeclaredfields(); // return public, protected, and private methods, but excludes inherited Fields.
  • To get annotations 
    • Annotation [] annotations= classObject.getAnnotations(); //return public and inherited from supperclass 
    • Annotation [] annotations= classObject.getDeclaredAnnotations(); // return public, protected, and private methods, but excludes inherited Fields.
  • To get class interfaces
    • Class [] interfaces = classObject.getInterfaces(); // return the class object interface implemented by the class, If a superclass of the class implements an interface, but the class doesn't specifically state that it also implements that interface, that interface will not be returned in the array. Even if the class in practice implements that interface, because the superclass does.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. if classObject is an interface object it will return extended once also.
  • To get supper class 
    • Class superclassObj= classObject.getSuperclass(); //it’s a Class object like any other, so can continue doing class reflection on that too.
  • To get package Info
    • Package package= classObject.getPackage(); // returns the package info if need name of package using getName() can return.  
  • To get class modifiers
    • int modifiers= classObject.getModifiers(); // returns the int representation of the class modifiers, can use java.lang.reflect.toString() to get it in the string format or can use java.lang.reflect.Modifier to check