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.
--------------------------------------------------------------------------------------------------------------------------
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
}
}
}
--------------------------------------------------------------------------------------------------------------------------
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 --
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
--------------------------------------------------------------------------------------------------------------------------
Labels:Reflection
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
0 comments:
Post a Comment