Blog Image

Reflection API  

This blog i am explaining about reflection API and what are the drawbacks and using this API how we can get the meta data of  class and which classes are providing for getting the meta data of class and methods

Reflection API:

Reflection API is a process of examining or modifying the behavior of a class at run time.

Reflection is powerful but we should not use in normal programming because of fallowing reasons,

  • Performance impact because it scanning the all the class path to find the classes to load ,   performance will degrated.
  • Security threat also there because of at run time it will accessible, means if any code changes entire application will impacted.
Reflection in java little use in normal programming but is the backbone of most of the java and j2ee frameworks .
If you want to know the information related to the classes java.lang.reflect package provides many classes to implement reflection java.Methods of the java.lang.Class class is used to gather the complete metadata of a particular class.
To know the information about the variables ,method and constructor first we want object of a class,
so we can create a object of class by using,
  • new keyword ,  
  • class.forName()
  • .class.
see below example for creating the object,

  Class c1=Class.forName("com.sjg.reflection.ReflectionApiExample");
        System.out.println(c1);
       
        ReflectionApiExample reflectionApi = new ReflectionApiExample();
        Class c2 = reflectionApi.getClass();
        System.out.println(c2);

        Class c3 = ReflectionApiExample.class;
        System.out.println(c3);

Now we have objects so we can get the meta data.
 See below example for getting the meta data of a class ,variable ,method and constructor


public class ReflectionApiExample implements Serializable {

    /* declaring variables */
    static int value = 1234567;
    static String nameOftheclass = "ReflectionApiExample";
    public static int value2 = 98765;
    private static int value3 = 98765;

    /* declaring the methods */
    public void gettingMetadata(String name) {
        System.out.println("getting meta data of method example method");
    }

    public String gettingMetaInfo() {
        System.out.println("getting meta data of method example method");
        return null;
    }

    /*declaring contractor*/
    public ReflectionApiExample(int no) {
    }

    public ReflectionApiExample() {
    }

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

        Class c3 = ReflectionApiExample.class;
        System.out.println(c3);

        System.out.println("***** getting meta data of class***** ");
        /*getting the meta data of a class*/
        System.out.println("getting meta data of class");
        System.out.println(c3.getName());

        System.out.println(c3.getSuperclass().getName());

        Class[] ListOfInterfaces = c3.getInterfaces();

        for (Class c : ListOfInterfaces) {
            System.out.println(c.getName());
        }

        /*getting the meta data of a class*/
        System.out.println("***** getting meta data of variables*****");
        int modifier = c3.getModifiers();
        System.out.println(Modifier.toString(modifier));
        Field[] variables = c3.getDeclaredFields();

        for (Field field : variables) {
            System.out.println(field.getName());
            System.out.println(field.getType());
            System.out.println(field.get(field));

            System.out.println();
            int modifier1 = field.getModifiers();
            System.out.println(Modifier.toString(modifier1));

        }

        /* getting meat date of method*/
        System.out.println("***** getting meta data of methods*****");
        Method[] methods = c3.getDeclaredMethods();
        for (Method method : methods) {

            System.out.println(method.getName());
            System.out.println(method.getReturnType());

            System.out.println();
            int method1 = method.getModifiers();
            System.out.println(Modifier.toString(method1));
        }

        /*getting meta data of constructor*/
        System.out.println("***** getting meta data of constructor *****");
        Constructor[] constructors = c3.getDeclaredConstructors();
        for (Constructor constructor : constructors) {

            System.out.println(constructor.getName());

            System.out.println();
            int constructor1 = constructor.getModifiers();

            System.out.println(Modifier.toString(constructor1));

            Class[] parameter = constructor.getParameterTypes();
            System.out.print("Constrctor parameter types :");
            for (Class parameter1 : parameter) {
                System.out.println(parameter1.getName() + " ");
            }
        }

    }

You can down load the above program from attachment given below.

About author

User Image
sagarreddy

Seeking a challenging position, where I will be a valuable team member, contributing quality ideas and work for an organization where there is an ample scope to grow for an individual as well as organization growth in Software Development.

0

-Comments

Be the first person to write a comment for this Blog
Load More

No More Comments

Leave a Comment

Your comment has been posted and will appear soon.