Reflection

It is a process of examining or modifying the run time behavior of a class at run time. Packages used for reflection in java is java.lang and java.lang.reflect

Methods of Reflection,

  • getName( )
  • Class.forName( )
  • isInterface( )
  • isArray()
  • isPrimitive()
  • getSuperclass()
  • getDeclaredFields()
  • getDeclaredMethods()
  • getDeclaredConstructors()
  • newInstance()

Sample Program

package viki;

class Simple{
public void getmsg()
{
System.out.println("Hi this class is called by newInstance() method!!!!!");
}
 }

interface My{ }  

class Reflection{  
 public static void main(String args[]) throws ClassNotFoundException,  InstantiationException, IllegalAccessException{ 
 
 
  @SuppressWarnings("rawtypes")
  Class c=Class.forName("viki.Simple");
  Simple s=(Simple)c.newInstance();  
  s.getmsg();  
  System.out.println(c.getName());
  
  Class c3=int.class;
  System.out.println(c3.isPrimitive());
  System.out.println(c3.isArray());
  
  Simple s1=new Simple();
  System.out.println(s1.getClass());
  
  
  Class c2=Class.forName("viki.My");  
  System.out.println(c2.isInterface());

  
  Class c1=Reflection.class;
  System.out.println(c1.getName());
  
 }  
 }   

Output:

Hi this class is called by newInstance() method!!!!!
viki.Simple
true
false
class viki.Simple
true
viki.Reflection

Javap using Reflection:

Javap command displays information about methods,fields and constructors for the given class.This can be acheived in reflection through, 
  • getDeclaredFields()
  • getDeclaredMethods()
  • getDeclaredConstructors()
these methods. The sample code follows,

   package viki;

   import java.lang.reflect.*;  

   public class Javaptool{  
   public static void main(String[] args)throws Exception {  
    Class c=Class.forName("viki.Simple");
      
    System.out.println("Fields........");  
    Field f[]=c.getDeclaredFields(); 
    if(f.length>0)
    {
    for(int i=0;i<f.length;i++)  
        System.out.println(f[i]);  }
    else
    {
    System.out.println("There is no fields");  
    }
      
    System.out.println("Constructors........");  
    Constructor con[]=c.getDeclaredConstructors();  
    if(con.length>0)
    {
    for(int i=0;i<con.length;i++)  
        System.out.println(con[i]);  
    }
    else
    {
    System.out.println("There is no constructors");  
    }
        System.out.println("Methods........");  
    Method m[]=c.getDeclaredMethods();  
    if(m.length>0)
    {
    for(int i=0;i<m.length;i++)  
        System.out.println(m[i]);  
    
   }
   else{
  System.out.println("There is no methods");  
   }
   }  
   }

Output:

Fields........
There is no fields
Constructors........
viki.Simple()
Methods........
public void viki.Simple.getmsg()

AppletViewer using Reflection:

Using reflection it is possible to create our own applet frame.The sample code as follows,

    package viki;
    import java.applet.Applet;  
    import java.awt.Frame;  
    import java.awt.Graphics;  
  
    public class MyViewer extends Frame{  
    public static void main(String[] args) throws Exception{  
    Class c=Class.forName("viki.MyViewer");  
          
    MyViewer v=new MyViewer();  
    v.setSize(400,400);  
    v.setLayout(null);  
    v.setVisible(true);  
          
    Applet a=(Applet)c.newInstance();  
    a.start();  
    Graphics g=v.getGraphics();  
    a.paint(g);  
    a.stop();  
          
    }  
  
    }  

Calling private method using reflection:

Normally ,It is not possible to call a java class which is declared as private from outside the class .But it can be achieved through reflection and example below,

package viki;
import java.lang.reflect.*;  

class Test{  
private void cube(int n){System.out.println(n*n*n);} 
private void sayHello(){System.out.println("Hi.......Hello.....");}
}


class CallingPrivateMethod{  
public static void main(String args[])throws Exception{  
Class c=Test.class;  
Object obj=c.newInstance();  
  
Method m=c.getDeclaredMethod("cube",new Class[]{int.class});  
m.setAccessible(true);  
m.invoke(obj,4);  

Method m1=c.getDeclaredMethod("sayHello");
m1.setAccessible(true);
m1.invoke(obj, null);
}}  

Output:

64
Hi.......Hello.....

  





No comments:

Post a Comment