Reflection refers to ability of a program to create objects and invoke methods of objects at runtime.
Why to use Reflection: It lets you write programs that you do not have to "know" everything at compile time, making them more dynamic, since they can be tied together at runtime !!
Lots of modern frameworks uses reflection extensively eg: Hibernate, Spring, Junit.
The api is useful in Product development like compilers, jvms, frameworks, servers development. These products reads the declarative part of the java class using Reflection API.
eg: where jvm needs to read declaration details(zero arg constructor) for a CLASS and uses Reflection API
Class A{
A(int i){
}
}
Class Test{
public static void main(String args[]){
Class c=Class.forName("A"); // to load A class byte code to jvm memory
A a= (A)c.newInstance(); //Creates instance with NO argument & non private constructor.
}
}
3 ways to prepare Class Object for a Class definition.
Employee.java
public abstract class Employee implements java.io.Serailizable,Cloneable {
public int eno;
public static String ename;
public final String eaddress;
public Employee(){
}
public Employee(int i, String name, String address){
eno=i; ename=name;eaddress=address;
}
}
Test.java
import java.lang.reflect.*;
public class Test throws Exception {
public static void main(String args[]){ Class c=Class.forName("Employee");
System.out.println("Name :"+c.getName());
System.out.println("Super Class :"+c.getSuperclass.getName());
Class[] ci=getInterfaces();
System.out.println("Interfaces :");
for (Class cls:ci) System.out.println(cls.getName()+ " ");
int i=c.getModifiers();
System.out.println("access modifiers"+Modifier.toString(i));
}
}
//Meta data for variables.
import java.lang.reflect.*;
public class Test throws Exception {
public static void main(String args[]) throws Exception{
Employee e=new Employee();
Class c=e.getClass();
// Field[] flds1=c.getFields(); //public variables only of the class and super class
Field[] flds2=c.getDeclaredFields(); //All the variables meta data of the class alone
for (Field f:flds2){
System.out.println("Field Name: "+f.getName());
System.out.println("Data Type: "+f.getType().getName());
int i=f.getModifiers();
System.out.println("access modifiers"+Modifier.toString(i));
}
}
}
//Meta data for methods.
import java.lang.reflect.*;
public class Test throws Exception {
public static void main(String args[]) throws Exception{
Class c= Employee.class();
// Field[] mthd1=c.getMethods(); //public methods only of the class and super class
Method[] mthd2=c.getDeclaredMethods(); //All the methods of the class alone
for (Method m:mthd2){
System.out.println("Method Name: "+m.getName());
System.out.println("Return Type: "+m.getReturnType().getName());
int i=m.getModifiers();
System.out.println("access modifiers"+Modifier.toString(i));
Class[] cls1=m.getParameterTypes();
System.out.println("Parameter Types");
for(Class c1:cls1){
System.out.println(c1.getName());
}
Class[] cls2=m.getExceptionTypes();
System.out.println("Exception Types");
for(Class c2:cls2){
System.out.println(c2.getName() + " ");
}
}
}
}
Why to use Reflection: It lets you write programs that you do not have to "know" everything at compile time, making them more dynamic, since they can be tied together at runtime !!
Lots of modern frameworks uses reflection extensively eg: Hibernate, Spring, Junit.
The api is useful in Product development like compilers, jvms, frameworks, servers development. These products reads the declarative part of the java class using Reflection API.
eg: where jvm needs to read declaration details(zero arg constructor) for a CLASS and uses Reflection API
Class A{
A(int i){
}
}
Class Test{
public static void main(String args[]){
Class c=Class.forName("A"); // to load A class byte code to jvm memory
A a= (A)c.newInstance(); //Creates instance with NO argument & non private constructor.
}
}
3 ways to prepare Class Object for a Class definition.
- Class c=Class.forName("Employee");
- It will search for Employee.class file in current location, java pre defined library & locations referred by classpath environment variable. It will load byte class code into memory. JVM will collect meta data of the class and creates Class Object.
- Argument is Fully Qualified Class name
- Doesn't work with Primitives
- Employee e=new Employee(); //loads class to memory and jvm creates Class Object implicitly
Class c=e.getClass(); // assigns Class Object reference to c - Class liternals
- Works with classes, interfaces, arrays, and primitives
- Class c=Employee.class //When a class is compiled, internally a class public static final variable will be appended to class which refers to Class object
- eg: String.class, boolean.class, int[][][].class, void.class
- public String getName(); //returns name of the respective class
- public Class getSuperClass(); // returns Super Class Object
- public Class[] getInterfaces();
- public int getModifiers() -> public static String toString(int modifier)
Employee.java
public abstract class Employee implements java.io.Serailizable,Cloneable {
public int eno;
public static String ename;
public final String eaddress;
public Employee(){
}
public Employee(int i, String name, String address){
eno=i; ename=name;eaddress=address;
}
}
Test.java
import java.lang.reflect.*;
public class Test throws Exception {
public static void main(String args[]){ Class c=Class.forName("Employee");
System.out.println("Name :"+c.getName());
System.out.println("Super Class :"+c.getSuperclass.getName());
Class[] ci=getInterfaces();
System.out.println("Interfaces :");
for (Class cls:ci) System.out.println(cls.getName()+ " ");
int i=c.getModifiers();
System.out.println("access modifiers"+Modifier.toString(i));
}
}
//Meta data for variables.
import java.lang.reflect.*;
public class Test throws Exception {
public static void main(String args[]) throws Exception{
Employee e=new Employee();
Class c=e.getClass();
// Field[] flds1=c.getFields(); //public variables only of the class and super class
Field[] flds2=c.getDeclaredFields(); //All the variables meta data of the class alone
for (Field f:flds2){
System.out.println("Field Name: "+f.getName());
System.out.println("Data Type: "+f.getType().getName());
int i=f.getModifiers();
System.out.println("access modifiers"+Modifier.toString(i));
}
}
}
//Meta data for methods.
import java.lang.reflect.*;
public class Test throws Exception {
public static void main(String args[]) throws Exception{
Class c= Employee.class();
// Field[] mthd1=c.getMethods(); //public methods only of the class and super class
Method[] mthd2=c.getDeclaredMethods(); //All the methods of the class alone
for (Method m:mthd2){
System.out.println("Method Name: "+m.getName());
System.out.println("Return Type: "+m.getReturnType().getName());
int i=m.getModifiers();
System.out.println("access modifiers"+Modifier.toString(i));
Class[] cls1=m.getParameterTypes();
System.out.println("Parameter Types");
for(Class c1:cls1){
System.out.println(c1.getName());
}
Class[] cls2=m.getExceptionTypes();
System.out.println("Exception Types");
for(Class c2:cls2){
System.out.println(c2.getName() + " ");
}
}
}
}


No comments:
Post a Comment