Java Reflection API - Overview
18 April 2007Concept of Java reflection is very powerful. It gives you access to internal information about the classes (sub classes, methods, attributes) loaded into the JVM. This concept is not available in other programming languages like C and C++.
Consider the following example. I have displayed the complete signatures of “ MyClass ” methods using reflection. First of all, you should obtain a java.lang.Class object for the class that you want to manipulate. “ java.lang.Class ” is used to represent classes in a running Java program. Then we will use getDeclaredMethods() to get the list of methods in an array.
import java.lang.reflect.*; public class Test { public static void main(String[] args) { try { Class c = Class.forName("MyClass"); Method m[] = c.getDeclaredMethods(); for (int i = 0; i < m.length; i++) System.out.println(m[i].toString()); } catch (Throwable e) { System.err.println(e); } } } public class MyClass { int getAge(String name){ return 25; } String getLoc(String name){ return "Paris"; } } Output: int MyClass.getAge(java.lang.String) java.lang.String MyClass.getLoc(java.lang.String)
You may also get the names and types of attributes from a class. getType() and getName() methods are used for this purpose. But before using these, you have to get the class using Class.forName() and after that use getDeclaredFields() to get the list of fields/attributes.
Review the following code:
public class BaseClass { private String company_name; private int company_code; } public class MyClass extends BaseClass { private int age; private String name; } import java.lang.reflect.*; public class Test { public static void main(String[] args) { try { Class c = Class.forName("MyClass"); Field list[] = c.getDeclaredFields(); for (int i = 0; i < list.length; i++) { Field fld = list[i]; System.out.println("Class: " +fld.getDeclaringClass()); System.out.println("Super Class: " +fld.getDeclaringClass().getSuperclass()); System.out.println("Attribute name: " + fld.getName()); System.out.println("Data type: " + fld.getType()); int mod = fld.getModifiers(); System.out.println("Modifiers: " +Modifier.toString(mod)); System.out.println("======================================"); } } catch (Throwable e) { System.err.println(e); } } } Output: Class: class MyClass Super Class: class BaseClass Attribute name: age Data type: int Modifiers: private ====================================== Class: class MyClass Super Class: class BaseClass Attribute name: name Data type: class java.lang.String Modifiers: private ======================================
The “ getSuperclass() ” method retrieves the name of super class. In the example discussed above, super class was BaseClass. If it has no explicit super class then it will return java.lang.Object as super class as every class in java is inherited from it.
Reflection also allows you to get the list of constructors.You can get information about a class’s constructors by invoking the getConstructors() method. It will return an array of Constructor objects. Now you can use the methods provided by the Constructor class to get the constructor’s name, set of modifiers, parameter types, and set of throwable exceptions.
You may also call methods of a class using reflection and you may also change values of attributes using reflection.
Related Posts:
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: Java Reflection API - Overview
I really liked your blog postings. I put a link to your blog on my blog: http://afbarnes.blogspot.com I am in the process of adding new articles. Tell me what you think….
Thanks for the link and appreciation, Andre.
Your blog looks very useful! Keep up the good work.
Thanks man!! It was really helpful. I m takin a seminar on the topic. Its really kewl!!