Monday 3 December 2012

Reflection

Posted by Naveen Katiyar On 09:53 1 comment
Reflection provides the facility to analyze a class  at run-time such as obtaining the name of class,no. of methods in the class,etc.
Class Loading:-It is a process of storing class information into the 'Class object' by JVM.Class loading is of two types-
a)Static class loading
b)Dynamic class loading

In case of static class loading,the compiler knows about the loaded  class and in case of dynamic class loading,compiler doesn't know about loaded class.


  • Ways of static class loading:

i) By using new keyword to make the object of class
ii)Static member of class is invoked.
iii)Through inheritance

Note : A class can be loaded only once in the JVM.

  • Ways of dynamic class loading:

forName() method of class named Class is used to dynamically load a class as well as returns Class object of loaded class.

syntax:- public static Class forName(String className) throws ClassNotFoundException

Note:In java ,all classes are represented by JVM as an object of a class named 'Class'.This object encapsulates all the information regarding class.e.g. class name,static method,non-static method,constructors etc.  

For e.g.:
class Test
{
public static void main(String arg[])
{
Class c = Class.forName(arg[0]);
------
------
}
}

The 'Class' object of given Test class contains following information:
  • class name
  • super class
  • super interface
  • methods
  • constructors
  • etc.
Note: The class named 'Class' itself is represented as an object of 'Class' by JVM.

In 'Object' class ,there is a method-
                       public Class getClass();

Test t  = new Test();
Class c =  t.getClass();

If we do not have the object of a class and that class is already loaded by inheritance ,or by calling of 
static method,etc.,then we can get 'Class' object as:-  

Class c = Test.class;      //It is a syntax

Commonly used methods of class named 'Class':
i)public String getName()
ii)public Method[] getDeclaredMethods()
iii)public Method[] getMethods()
iv)public Field[] getDeclaredFields()
v)public Field[] getFields()
vi)public Constructor[] getDeclaredConstructors()
vii)public Constructor[] getConstructors()
viii)public Object newInstance()
and many more---

JVM represents every method of a class by making object of class named 'Method'.Constructor,Field, and
Method classes are defined in java.lang.reflect package.

The below class will explain the above concept:-

class InfoTest
{
public static void main(String arg[])
{
try
{
Class c = Class.forName(arg[0]);
System.out.println(c.getName());
System.out.println(c.getSuperClass());
System.out.println(c.isInterface);
System.out.println(c.getModifiers());
}catch(Exception e){}
}
}

Note:-getMethods() method only return public methods of current class and immediate super class's public method,while getDeclaredMethods() only returns methods of current class(public,private,protected).
For each modifier or any valid combination of modifier ,java has assigned a numeric value which is returned 
by getModifiersMethod().e.g.
public-1
abstract-2
public final-17

Making our own javap tool

import java.lang.reflect.*;
class MyJavap
{
public static void main(String arg[])
{
try
{
Class c = Class.forName(arg[0]);
Method m[] = c.getDeclaredMethods();
System.out.println("****methods are****");
for(Method mm : m)
     System.out.println(mm);

Field f[] = c.getDeclaredFields();
System.out.println("****fields are****");
for(Field ff: f)
     System.out.println(ff);

Constructor ctr[] = c.getDeclaredConstructors();
System.out.println("****constructors are****");
for(Constructor cc: ctr)
     System.out.println(cc);
 
}catch(Exception e){}
}
}

 


     
 

1 comments: