Wednesday, October 24, 2012

Retrieve Class Information using Java Reflection API

Reflection API should be used by developers who has strong understanding of language fundamentals.

Using Reflection APIs , you can

1. Retrieve Class Information
2. Retrieve Implemented Interfaces
3. Retrieve Methods information used in Provided Class
4. Retrieve Constructors information used in provided class
5. Retrieve Fields information
6. Retrieve SuperClass information of provided class
7. Retrieve Modifier Information
8. and Many more :)

Here, i am just showing small java code for how you can retrieve class information. Please refer to this blogs for more examples of Java Reflections API explanation and java codes.

Java Program :
List list = new ArrayList();

Class originalClass = list.getClass();        
System.out.println("Original Class Information");
System.out.println("ClassName - " + originalClass.getName());
System.out.println("SimpleName -  " + originalClass.getSimpleName());
System.out.println("PackageName - " + originalClass.getPackage());
Output :
Original Class Information
ClassName - java.util.ArrayList
SimpleName -  ArrayList
PackageName - package java.util, Java Platform API Specification, version 1.7

Author : Anuj Patel
Blog : http://goldenpackagebyanuj.blogspot.in/

No comments:

Post a Comment