Thursday, August 8, 2013
Today I will show you how to retrieve all the declaration components of a class using Java Reflection. The declaration components means the modifiers, the generic type parameters, implemented interfaces, classes inherited i.e. inheritance path and all the annotations declared. These are all the meta informations related with a class. We can get all modifiers using method getModufiers(). The different modifiers include private, protected, public, static, final, abstract etc. We can get annotations using getAnnotations(). We can get type parameters using getTypeParameters() while interfaces using getGenericInterfaces() and super-class using getSuperClass().
           Here in the example we will take input from user the fully qualified class name and then print all the declaration components of that class using the methods mentioned above and thus sniff class declaration. If the class name eneterd is not found then it will throw ClassNotFoundException.
--------------------------------------------------------------------------------------------------------------------------
Java Source Code
--------------------------------------------------------------------------------------------------------------------------
import java.io.Console;
import java.lang.annotation.Annotation;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.List;

import static java.lang.System.out;

public class ClassDeclarationSniffer {
    public static void main(String[] args) {
Console console=System.console();
System.out.print("Enter Class Name : ");
String cname=console.readLine();  //reading class name
try {
Class<?> c=Class.forName(cname);  //loading class
out.println("\n------OUTPUT------");
out.println("Class : "+c.getCanonicalName());
//getting all modifiers
out.println("\nModifiers : "+Modifier.toString(c.getModifiers()));
//getting generic type parameters
out.print("\nType Parameters : ");
TypeVariable[] tv = c.getTypeParameters();
   if (tv.length != 0)
   for (TypeVariable t : tv)
       out.print(t.getName()+" ");
   else
       out.print("  -- No Type Parameters --");
   //getting all implemented interfaces
   out.println("\n\nImplemented Interfaces :");
   Type[] intfs = c.getGenericInterfaces();
   if (intfs.length != 0)
   for (Type intf : intfs)
       out.println("  "+intf.toString());
   else
       out.println("  -- No Implemented Interfaces --");
   //getting inheritance hierarchy
   out.println("\nInheritance Path : ");
   List<Class> lst = new ArrayList<Class>();
   printParent(c, lst);
   if (lst.size() != 0)
   for (Class<?> parent : lst)
       out.println("  "+parent.getCanonicalName());
   else
       out.println("  -- No Super Classes --");
   //getting all annotations
   out.println("\nAnnotations : ");
   Annotation[] anns = c.getAnnotations();
   if (anns.length != 0)
   for (Annotation a : anns)
       out.println("  "+a.toString());
   else
out.println("  -- No Annotations --");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
    
    private static void printParent(Class<?> c, List<Class> l) {
    Class<?> parent = c.getSuperclass();  //getting superclass
      if (parent != null) {
       l.add(parent);  //adding to hierarchy list
       printParent(parent, l);  //recursive calling for another parent
      }
    }
}
--------------------------------------------------------------------------------------------------------------------------
Output
--------------------------------------------------------------------------------------------------------------------------
Enter Class Name : java.util.ArrayList

------OUTPUT------
Class : java.util.ArrayList

Modifiers : public

Type Parameters : E

Implemented Interfaces :
  java.util.List<E>
  interface java.util.RandomAccess
  interface java.lang.Cloneable
  interface java.io.Serializable

Inheritance Path :
  java.util.AbstractList
  java.util.AbstractCollection
  java.lang.Object

Annotations :
  -- No Annotations --
--------------------------------------------------------------------------------------------------------------------------
Download Links
--------------------------------------------------------------------------------------------------------------------------

0 comments:

Post a Comment

Total Pageviews

Followers


Labels

Popular Posts

free counters