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.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("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()+" ");
       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());
       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());
       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());
out.println("  -- No Annotations --");
} catch (ClassNotFoundException e) {
    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
Enter Class Name : java.util.ArrayList

Class : java.util.ArrayList

Modifiers : public

Type Parameters : E

Implemented Interfaces :
  interface java.util.RandomAccess
  interface java.lang.Cloneable

Inheritance Path :

Annotations :
  -- No Annotations --
Download Links


Post a Comment

Total Pageviews

Subscribe via Email


Popular Posts

About Me

My photo

I am a student of BTech Computer Science Engineering from RCCIIT,Kolkata. I am a crazy lover of Java and wants to settle as a Java developer. I have a seven years Java experience with an application developer experience for 2 years. Recently from March 2012 I am a registered S40 app developer for Nokia and has corrected an app of them. I am currently writing blogs to encourage and grow interest in all those who don't know or learning Java.