Refletion API

Hope that everybody having problems with the reflection API will get some help from this tutorial.

This is what the sun java tutorials say about this API.

Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique and can enable applications to perform operations which would otherwise be impossible.

you can get the full sun tutorial Here.

So sun says "This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language" so i don't know why they added this to CSE syllabus in the third lecture?????.

What to do now we must learn it so i thought to make a simple tutorial so everybody can understand.

Let's start

  • To look into java classes and find out what they are made of we must first be able to catch the class itself to do this there are three ways but we will only need one.
1.the simplest way
Class c=Class.forName("fully-qualified name of a class");

Here the fully qualified name means class with packages for example if we have class "foo" in package "packfoo"
the fully-qualified class name would be "packfoo.foo";.

Then we have to find out the properties of the class like the name of it's super class,modifiers to do this you can use methods given in the Class class(look at all the methods Here).

Here are two methods from the Class class
1.to get the super class-c.getSuperclass()
2.to get the modifiers-c.getModifiers();
there a load of methods in the Class class which you can use similarly
  • Then we have to be able to get properties of methods in the class.
First we must get all the methods in the class to do this there are two methods in the Class class.

1.to get all the public methods and inherited methods-
Method m[ ]=c.getMethods();
Her "Method" is a variable type that can keep objects passed by the "getMethods();" and here we have used a array because this method return a array of "Method's".

2.to get all the other methods like private methods(includes public methodes but does not include inherited methods) -
Method m1[ ]=c.getDeclaredMethods();

Then just like the Class methods the 'Method' class also has a lot of useful methods like. m[0].getName(),m[0].getReturnType()

Now we have to get the variables for this it's same as the methods just two deferent methods to get the values.

1. to get all the public variables and inherited variables-
Field f[]=c.getFields();
Her "Field" is a variable type that can keep objects passed by the "getFields();" and here we have used a array because this method return a array of "Field's".

2.to get all the other variables like private variables(includes public variables but does not include inherited variables) -
Field f1[]=c.getDeclaredFields();

Then again 'Field' class also has a lot of useful methods like f[0].getName(),f[0].getModifiers();


Here is a simple example

The Main class

package javaapplication1;
public class Main {

public static void main(String[] args) {

Inspector insp = new Inspector("javaapplication1.NewClass");
System.out.println(insp);
}

}

The NewClass



package javaapplication1;


public class NewClass extends NewClass1 {
public int i=20;
public String name="supun";
int k;
private int y;
String nothing;
public int edan1(){
return 34;
}
int edan2(){
return 34;
}
private int edan3(){
return 34;
}
}

The NewClass1




package javaapplication1;


public class NewClass1 {
public int superr=21;

public String supermethod(){
return "super";
}
}
The inspector class (this does all the work)






package javaapplication1;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class Inspector {
private Class thisClass;
private Class superClass;
public Inspector (String className) {
try {
System.out.println("Loading class : " + className);

thisClass = Class.forName(className);
System.out.println("Class load successful");

System.out.println("Prointing super class and modifier");
System.out.println(Modifier.toString(thisClass.getModifiers()));
superClass = thisClass.getSuperclass();
System.out.println("Super class : "+ superClass);



Field[] f=thisClass.getFields();
Field[] f1=thisClass.getDeclaredFields();
System.out.println("get public feilds ");
for(Field x:f){
System.out.println("name--"+x.getName());
}
System.out.println("get other feilds ");
for(Field x:f1){
System.out.println("name--"+x.getName());
}


Method[] m1=thisClass.getDeclaredMethods();
Method[] m=thisClass.getMethods();
System.out.println("get public methods ");
for(Method x:m){
System.out.println("name--"+x.getName());
System.out.println("return type--"+x.getReturnType());
}
System.out.println("get other methods ");
for(Method x:m1){
System.out.println("name--"+x.getName());
System.out.println("return type--"+x.getReturnType());
}

} catch (Exception e) {
System.out.println("Error loading ");

}
}
}


Please feel free to comment on the tutorial

Comments

Popular posts from this blog

Setting up Heron Cluster with Apache Aurora Locally

Reading and Writing Binary files in java - Converting Endianness ( between Big-endian byte order and Little-endian byte order)

Writing Unit Tests to test JMS Queue listener code with ActiveMQ