Reflection API

  • An API is already developed set of classes which can be reused to enhance/add a functionality.
  • We can have API for different concepts for example we have Collection API, JDBC API, Servlet API, Reflection API.
  • In collection API we have classes like
    • ArrayList Class.
    • Hashmap class
    • Hashset class
    • TreeSet class
    • Priorityqueue class
  • In JDBC API we have classes like 
    • Driver
    • Connection
    • Statement
    • Prepared statement
    • Callable statement
    • Results set
    • Results set metadata
    • BLOB
    • CLOB
    • Save Point
    • RawSet
  • A set of classes meant for performing reflection operations is called as reflection API.
  • Reflection API is used for reading and executing a class information/class meta-data at execution time.
  • Reflection API is needed so that compiler, interpreter and decompiler can read a class information.
  • Where ever in our project we need class information dynamically we should use reflection API.
  • Reflection API is used for accessing the class members and executing member function dynamically.
    • For example tomcat server reads our servlets defined in web.XML using reflection API and execute innate, service, destroy methods accordingly.
  • For example a class cannot be declared as private/protected. Compiler throws error for this using Reflection API.
  • Reflection API is used to read declarative information of a class.
  • If we use expressions such as
    • Class C = Class.fromName(“A”)
    • A a = (A) C.newInstance();
  • Here JVM will read metadata of class A using reflection API And it’s bite code will be loaded to the memory.
  • The attribute will be stored in form of a glass object referred by C variable.
  • Primary class of Java reflection API is class in java.lang.class which gathers the meta-data of a class.
  • This class object in Java reflection API Consists of objects of classes that are defined in java.lang.reflect package. Some of the classes are as follows
    • Field
      • Gathers meta-data off variable
    • Method
      • Gathers meta-data of methods
    • Constructor
      • Gathers meta-data of constructor
    • Modifier
      • Gathers modifiers of class
  • There are three ways to prepare class object in Java
    • Class.forName(“employee”)
    • Employee e = new Employee(); Class C = e.getClass();
      • Method GetClass is in object class.
    • Class C = Employee.class
      • Class is a final variable define in “Class” class which holds class meta-data.
  • Some methods defined in “Class” class are
    • Public String getName()
      • Gets name of the class
    • Public class getSuperClass()
      • Gets name of SuperClass
    • Public class getInterfaces()
      • Gets interfaces of class
    • Public int getModifiers(); 
      • Gets modifier ID
      • Public Static String toString(int i) Of Modifier class gets Modifier.
  • To get Fields we use two methods
    • GetFields()
      • Can be used to get all the public fields.
    • GetDeclaredFields()
      • Used to get all the fields of current class irrespective of acess modifier public.
  • Once we have the field array we can use following methods for meta-data.
    • Public string getName
    • Public class getType
    • Public vartype get (Field F)
      • Gets the value of the field
    • Public int getModifier(); Modifier.toString(i);
  • To get methods we use
    • Public Method[] getMethods();
    • Public Method[] getDeclaredMethods();
    • Exclamation of above is same as for Fields.
  • Once we have Method array we can get method meta-data using
    • Public string getName
    • public class getReturnType
    • Public int getModifiers
    • Class[] getParameterTypes
    • Class[] getExceptionTypes
  • To getConstructors we use getConstructors and get DeclaredConstructors methods.
    • This returns array of Constructors
  • Once we have constructors array we can get method made a data using
    • Public String getName
    • Public int getModifiers
    • Public Class[] getParameterTypes
    • Class[] getExceptionTypes

No comments:

Post a Comment