Reflection
API provide the facility find out the details of a class at execution time as
well as facility to load a class, to create an instance of a class to invoke a
constructor or method on a class object to get and set value of a field etc.
java.lang.reflect
package provide classes of reflection API. Commonly use class of Reflection API
Method:
A Method
provides information about, and access to, a single method on a class or
interface. The reflected method may be a class method or an instance method (including
an abstract method).
Field:
A Field
provides information about, and dynamic access to, a single field of a class or
an interface. The reflected field may be a class (static) field or an instance
field.
Constructor:
Constructor
provides information about and access to, a single constructor for a class.
Class Loading
Static
class loading: If the class to be loaded is known at the compilation time, then
this class is loaded using static class loading.
Dynamic
Class loading: A class to be loaded become known at execution time then it is
loaded by dynamic class loading.
Static
class loading is implicitly performed by the JVM in the following cases:
a. When an object of a class is created.
b. When a static member or static method
of a class is refer.
In static
class loading if class not found then NoClassDefFoundError is occurred.
Dynamic
class loading has to be explicitly requested:
Java.lang.Class
class provides method to dynamically load a class to create an instance of a
class or find out the details of the class.
forName()
: This method of this class is used to dynamically load a class into memory.
public
static Class forName(String ClassName)
If class
not found then ClassNotFoundException is occurred.
In java for each class an object of
java.lang.Class is created when the class is loaded into the memory.
Methods of java.lang.Class: It is a final class.
getName():Returns
the name of the entity (class, interface, array class, primitive type, or void)
represented by this Class object, as a String.
public
String getName()
getMethods():Returns
an array containing Method objects reflecting all the public member methods of
the class or interface represented by this Class object, including those
declared by the class or interface and those inherited from superclasses and
superinterfaces.
public
Method[] getMethods()
getDeclaredMethods():Returns
an array of Method objects reflecting all the methods declared by the class or
interface represented by this Class object. This includes public, protected,
default (package) access, and private methods, but excludes inherited methods.
public
Method[] getDeclaredMethods()
getConstructors():Returns
an array containing Constructor objects reflecting all the public constructors
of the class represented by this Class object
public
Constructor[] getConstructors()
getDeclaredConstructors():Returns
an array of Constructor objects reflecting all the constructors declared by the
class represented by this Class object. These are public, protected, default
(package) access, and private constructors.
public
Constructor[] getDeclaredConstructors()
getFields():Returns
an array containing Field objects reflecting all the accessible public fields
of the class or interface represented by this Class object
public
Field[] getFields()
getDeclaredFields():Returns
an array of Field objects reflecting all the fields declared by the class or
interface represented by this Class object.
public
Field[] getDeclaredFields()
newInstance():Creates
a new instance of the class represented by this Class object.
Example to load class dynamically
and get all details of class like fields, constructors and methods:
- This is a class Student, their details are
getting through Person class
package nkpack;
/**
*
* @author Navindra Jha
*/
public class Student {
private String name;
protected String state;
public int marks;
String hno;
public Student() {
}
public Student(String name) {
this.name = name;
}
public String getHno() {
return hno;
}
public void setHno(String hno) {
this.hno = hno;
}
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
this.marks = marks;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
- Person.java
package nkpack;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Navindra Jha
*/
public class Person {
public static void main(String args[]) {
try {
Class c = Class.forName("nkpack.Student");
System.out.println("Constructors in " + c.getSimpleName() +
" class are:");
Constructor constructor[] =
c.getDeclaredConstructors();
for (Constructor constructor1 :
constructor) {
System.out.println(constructor1);
}
System.out.println("Fields in
" + c.getSimpleName() + " class are:");
Field field[] =
c.getDeclaredFields();
for (Field field1 : field) {
System.out.println(field1.getType().getSimpleName() + ":" +
field1.getName());
}
System.out.println("Methods in
" + c.getSimpleName() + " class are:");
Method method[] =
c.getDeclaredMethods();
for (Method method1 : method) {
System.out.println(method1);
}
} catch (ClassNotFoundException ex) {
Logger.getLogger(Person.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Output:
Constructors in Student class
are:
public
applettest.Student(java.lang.String)
public applettest.Student()
Fields in Student class are:
String:name
String:state
int:marks
String:hno
Methods in Student class are:
public java.lang.String
applettest.Student.getHno()
public void
applettest.Student.setHno(java.lang.String)
public int
applettest.Student.getMarks()
public void
applettest.Student.setMarks(int)
public java.lang.String
applettest.Student.getName()
public java.lang.String
applettest.Student.getState()
public void
applettest.Student.setName(java.lang.String)
public void
applettest.Student.setState(java.lang.String)
No comments:
Post a Comment