Tuesday 3 April 2012

Java Refection

This time I have come up with the concept that will help the developer to concentrate on business logic rather thinking about their object instantiation, its just like spoon feed to the developer that he/she won't think about  his/her own   
  • servlet instantiation(Servlets)
  • action class(Struts) instantiation 
  • controller bean(Spring) instantiation etc...
  Every one knows that In Servlet technology  service() method executed for each client request but to execute the service() method first our servlet needs to be instantiated. As you know that we are not creating object of our servlet and we are not calling service() method  like the following: 


               MyServlet  myservlet=new MyServlet();                                   
               myservlet.service(request, response);

then who is creating the object of our servlet and calling the service method?

the answer is container will do this.

how container will create the object?

based on the configuration given in web.xml file container will create the object of particular servlet by loading it using the class called Class.

servlet configuration in web.xml:-

<servlet>
      <servlet-name>myservlet</servlet-name>
      <servlet-class>org.servlets.MyServlet</servlet-class>
 </servlet>

step 1: loading the servlet 

Class clazz = Class.forName("org.servlets.MyServlet"); 

step 2: creating object of MyServlet 

Servlet myservlet = clazz.newInstance();
myservlet.service(request, response);

you can write step 1 and step 2 in a single step like the below

Servlet myservlet= Class.forName("org.servlets.MyServlet").newInstance();

after creating the object of our own servlet container will calls the life cycle methods (init ,service, destroy) 

the same thing will happen incase of Struts and other mvc frameworks             
Example of Struts Action class object creation :                                                         
Action action= Class.forName("MyAction class to be loaded").newInstance();
action.execute(....);                                                                                        

after loading any class if you want to know information about the loaded class such as information about the interface that the loaded class is implementing,information about its super classes, information about methods,information about the fields or variables of the loaded class then you can make use the classes of Java Reflection API.

If you want to find out information about java.lang.String  please see below:

  Class clazz= Class.forName("java.lang.String"); 
 
   //Identifying super classes 
  Class superClass = clazz.getSuperclass();
  while (superClass  != null)
  {
    String className = superClass.getName();
    System.out.println("Super class of String is "+ className);
  } 
    
  //Identifying  interfaces   
 Class interfaces = clazz.getInterfaces();
 for (int i = 0; i < interfaces.length; i++)
 {
    String interfaceName = interfaces[i].getName();
    System.out.println(interfaceName);
 } 
 
//Identifying  Constructors of the class


Constructor[] constructors =clazz.getConstructors();
for (int i = 0; i < constructors.length; i++)
{
  System.out.print("( ");
  Class[] parameterTypes =constructors[i].getParameterTypes();
  for (int k = 0; k < parameterTypes.length; k ++)
  {
    String parameterString = parameterTypes[k].getName();
    System.out.print(parameterString + " ");
  }
  System.out.println(")");
}
 
//Identifying  methods of the class 
 Method[] methods= clazz.getMethods();
 for (int i = 0; i < theMethods.length; i++)
 {
   String methodString = methods[i].getName();
   System.out.println("Name: " + methodString);
   String returnString =
   methods[i].getReturnType().getName();
   System.out.println("   Return Type: " + returnString);
   Class[] parameterTypes = methods[i].getParameterTypes();
   System.out.print("   Parameter Types:");
   for (int k = 0; k < parameterTypes.length; k ++)
   {
     String parameterString = parameterTypes[k].getName();
     System.out.print(" " + parameterString);
   }
   System.out.println();
 } 






//Identifying  fields of the class 
 Field[] fields = c.getFields();
 for (int i = 0; i < fields.length; i++) 
 {
   String fieldName = fields[i].getName();
   Class typeClass = fields[i].getType();
   String fieldType = typeClass.getName();
   System.out.println("Name: " + fieldName + ", Type: " + fieldType);
 }

 

No comments:

Post a Comment