Thursday, 8 March 2018

Reflection API in JAVA

We can modify the run time behavior of a class with Reflection process. With this reflection we can inspect and get the metadata information of class, interface or method etc at runtime and can modify the run time behavior of a class.

We can also create objects for a class using Reflection and also we can call the methods and can access the fields.

java.lang.class provides methods to get metadata for a class and  to change the run time behavior.

Below are the most commonly used method of Class.

public static Class forName(String className)  --- it will load the class return the reference of class.

public T newInstance() -- it will create new object for class and returns it.

public String getName() ----- -It returns name of class

public boolean isArray() -- to check if it is array

public boolean isPrimitive() -- to check if it is primitive

public boolean isInterface() -- to check if it is interface

public Class getSuperClass() --- to get super c lass refence

public Field[] getDeclaredFileds -- to get array of fields

public Method[] getDeclaredMethods --- to get array of Methods

public Constructor[] getDeclaredConstructors --- to get array of Constructors


package com.sample.java.testing;

public class ReflectionTest1 {

    public static void main(String[] args) throws ClassNotFoundException {
    }
}


package com.sample.java.testing;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ReflectionTest extends ReflectionTest1{
   
    int i = 10;
   
    public ReflectionTest() {

    }
   
    public ReflectionTest(int k) {
       
    }
   
   package com.sample.java.testing;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ReflectionTest extends ReflectionTest1{
   
    int i = 10;
   
    public ReflectionTest() {

    }
   
    public ReflectionTest(int k) {
       
    }
   
    public void displayClassNameUsingClassUsingGetclass(Object object){
        Class class1 = object.getClass();// get the class ref using .class as well
        System.out.println(class1.getName());//it will print class name
        System.out.println(class1.getModifiers());//return access modifiers of class.
        Field[] fields = class1.getDeclaredFields();
        for (Field field : fields) {
            System.out.println("field name is " + field.getName());//print field name
            System.out.println("field class is " + field.getClass());//print field class
        }
       
        Method[] methods = class1.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println("Method name is " + method.getName());//print method name
            System.out.println("Method class is "  + method.getClass());//print method class
        }
       
        Constructor[] constructors = class1.getDeclaredConstructors();
        for (Constructor constructor : constructors) {
            System.out.println("constructor name is " + constructor.getName());//print constructor name
            System.out.println("constructor class is "  + constructor.getClass());//print constructor class
        }
       
        System.out.println("Super " + class1.getSuperclass());// it will print super class ref
    }
   
    public void displayClassNameUsingClassUsingDotclass(){
        Class class2 = ReflectionTest.class;// get the class ref using .class as well
        System.out.println(class2.getName());//it will print class name
    }
   
    public void displayClassNameUsingClassUsingForname() throws ClassNotFoundException{
        Class class2 = Class.forName("java.lang.ArrayList");
        System.out.println(class2.getName());//it will print class name
//returns true if ReflectionTest1 is interface otherwise false
        System.out.println(class2.isInterface());
       
        Class class1 = Class.forName("ReflectionTest1");
        System.out.println(class1.getName());//it will print class name
//returns true if ReflectionTest1 is interface otherwise false
        System.out.println(class1.isInterface());
       
        Class class11 = Class.forName("ReflectionInterface");
        System.out.println(class11.getName());//it will print class name
//returns true if ReflectionInterface is interface otherwise false
        System.out.println(class1.isInterface());
       
    }
   

    public static void main(String[] args) throws ClassNotFoundException {

        ReflectionTest  object = new ReflectionTest(4);
        object.displayClassNameUsingClassUsingGetclass(object);
        object.displayClassNameUsingClassUsingDotclass();
        object.displayClassNameUsingClassUsingForname();
    
       
    }

}

  • IDE like eclipse, my eclipse   and net beans will use reflection API for auto completion of methods by using getDeclaredMethods().
  • Spring dependency injection also uses Reflection API. 
  • Junit also will use Reflection API and it parse @test annotation and get all method of test class and execute it.



No comments:

Post a Comment