Showing posts with label Reflection. Show all posts
Showing posts with label Reflection. Show all posts

Java Reflection with Example . Accessing of Class Fields , Methods & Others

Reflection is a feature enabling a program can access and manipulate itself - It consists of metadata plus operations to manipulate the metadata. Meta means self-referential - So metadata is data (information) about oneself.
Reflection in Java is an feature which allows an executing Java program to access itself, and manipulate internal properties of the program. For example, it is possible for a Java class to obtain the names of all its members (Fields & Methods) and display them. Reflection can be usesd by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. The Reflection API represents or reflects the classes, interfaces, and objects in the Java virtual machine.


Some Uses of Reflection :
-----------------------
1) to determine which data fields are defined in a given class
2) to determine which Methods are defined in a given class
3) to see which objects are instances of a given class
4) to display which constructors are defined in a given class
5) to change the value of data fields in objects
6) to invoke a method by its name
Example 1 :
The following example illustrates to display of all methods of a Classs which is given as a input.
import java.lang.reflect.*;
public class classMethods {
public static void main(String args[])
{
try {
Class cl = Class.forName(args[0]);
Method met[] = cl.getDeclaredMethods();
for (int i = 0; i < met.length; i++)
System.out.println(met[i].toString());
}
catch (Throwable e) {
System.err.println(e);
}
}
}

Usage : Compile the java program by : javac classMethods.java
and run the program by :
1) java classMethods java.util.Date > kk.out
the kk.out file will have the following outputs which are the methods of java.util.Date
public int java.util.Date.hashCode()
public java.lang.Object java.util.Date.clone()
public boolean java.util.Date.equals(java.lang.Object)
public int java.util.Date.compareTo(java.lang.Object)
public int java.util.Date.compareTo(java.util.Date)
public java.lang.String java.util.Date.toString()
private void java.util.Date.writeObject(java.io.ObjectOutputStream) throws java.io.IOException
private void java.util.Date.readObject(java.io.ObjectInputStream) throws java.io.IOException,java.lang.ClassNotFoundException
private final sun.util.calendar.BaseCalendar$Date java.util.Date.normalize()
private final sun.util.calendar.BaseCalendar$Date java.util.Date.normalize(sun.util.calendar.BaseCalendar$Date)
public boolean java.util.Date.after(java.util.Date)
public boolean java.util.Date.before(java.util.Date)
public static long java.util.Date.parse(java.lang.String)
public int java.util.Date.getDate()
public long java.util.Date.getTime()
public void java.util.Date.setTime(long)
public int java.util.Date.getHours()
public int java.util.Date.getMinutes()
public int java.util.Date.getMonth()
public int java.util.Date.getSeconds()
public int java.util.Date.getYear()
public static long java.util.Date.UTC(int,int,int,int,int,int)
private static final java.lang.StringBuilder java.util.Date.convertToAbbr(java.lang.StringBuilder,java.lang.String)
private final sun.util.calendar.BaseCalendar$Date java.util.Date.getCalendarDate()
private static final sun.util.calendar.BaseCalendar java.util.Date.getCalendarSystem(sun.util.calendar.BaseCalendar$Date)
private static final sun.util.calendar.BaseCalendar java.util.Date.getCalendarSystem(long)
private static final sun.util.calendar.BaseCalendar java.util.Date.getCalendarSystem(int)
public int java.util.Date.getDay()
private static final synchronized sun.util.calendar.BaseCalendar java.util.Date.getJulianCalendar()
static final long java.util.Date.getMillisOf(java.util.Date)
private final long java.util.Date.getTimeImpl()
public int java.util.Date.getTimezoneOffset()
public void java.util.Date.setDate(int)
public void java.util.Date.setHours(int)
public void java.util.Date.setMinutes(int)
public void java.util.Date.setMonth(int)
public void java.util.Date.setSeconds(int)
public void java.util.Date.setYear(int)
public java.lang.String java.util.Date.toGMTString()
public java.lang.String java.util.Date.toLocaleString()

2) java classMethods classMethods
Now input is the own class which has only one method.
Output is : public static void classMethods.main(java.lang.String[])

Example 2:
The following example illustrates how to access the fields of a class .

import java.lang.reflect.*;

public class fields {
private double fld1;
public static final int fld2 = 50;
String s = "field testing";

public static void main(String args[])
{
try {
Class cls = Class.forName("fields");

Field fldlist[] = cls.getDeclaredFields();
for (int i
= 0; i < fldlist.length; i++) {
Field fld = fldlist[i];
System.out.println("name = " + fld.getName());
System.out.println("declared class = " + fld.getDeclaringClass());
System.out.println("type = " + fld.getType());
int mod = fld.getModifiers();
System.out.println("modifiers = " + Modifier.toString(mod));

}
}
catch (Throwable e) {
System.err.println(e);
}
}
}

Output :
name = fld1
declared class = class fields
type = double
modifiers = private
name = fld2
declared class = class fields
type = int
modifiers = public static final
name = s
declared class = class fields
type = class java.lang.String
modifiers =
Read more...

 
Copyright 2011 Java Online