Professional Documents
Culture Documents
15 Reflection
15 Reflection
Know
• What is Reflection
• Where we use reflection
• Use and Misuse of reflection
• Steps in using reflection classes
• Simulating an instanceof operator
• Getting detailed info about a class
• Invoking methods of a class
Be Able To
• Write a reflection class
What is Reflection ?
• Reflection is the feature of Java
programming language through which we
can inspect an executing java program
and get information about a class and its
members.
• This is the technique that JavaBeans
“introspection” mechanism uses to
determine the properties, events and
methods that are supported by the bean.
Where we use Reflection
• Many IDE provide ‘code-assist’ and they use
reflection to get information about the class.
• Many framework API rely reflection.
• Used by application that create documentation
• Used by the application servers, testing tools
etc.
Misuse of reflection
• Don’t use reflections when there are better
alternative.
• This is because
– It makes the application slow
– It is difficult to code read and maintain
code that uses reflection extensively.
import java.util.reflect.*;
public class CheckMethod {
}
catch(Exception e)
{
System.out.println(e);
}
}
Execute the above program as
java CheckMethod java.util.Stack
Output:
public synchronized java.lang.Object
java.util.Stack.pop()
public java.lang.Object
java.util.Stack.push(java.lang.Object)
public boolean java.util.Stack.empty()
public synchronized java.lang.Object
java.util.Stack.peek()
public synchronized int
java.util.Stack.search(java.lang.Ob
ject)
Steps in using reflection classes
• Step 1:
obtain a java.lang.Class object for the
class that you want to manipulate.
• Step 2:
to call a method such as
getDeclaredMethods
• Step 3:
use the reflection API to manipulate the
information
Simulating instanceof operator
public class CheckInstance {
public static void main(String args[])
{
try {
Class cls = Class.forName(“java.lang.Thread");
boolean b1= cls.isInstance(new B());
System.out.println(b1);
boolean b2 = cls.isInstance(new Thread())
System.out.println(b2);
}
catch (Exception e) {
System.out.println(e);
}
}
}
Getting detailed info about a class
package person;
public class Student {
private String name; Bean class
private int regNo;
private String degreeName;
private int currentSemester;
private static int gRegNo;
Class cl=Class.forName("person.Student");
Method m_list[]=cl.getDeclaredMethods();
Constructor
c_list[]=cl.getDeclaredConstructors();
Field f_list[]=cl.getDeclaredFields();
/*method details*/
System.out.println("########### Method Details
###########################");
for(int i=0;i<m_list.length;i++)
{
Method m_list1=m_list[i];
System.out.println("Method--"+(i+1)+" details");
System.out.println("-------------------");
System.out.println("Method
name="+m_list1.getName());
System.out.println("Method declared in
class="+m_list1.getDeclaringClass());
System.out.println(" ");
System.out.println("Parameter List");
Class param[]=m_list1.getParameterTypes();
for(int j=0;j<param.length;j++)
{
System.out.println("Method parameter #" + j
+ " "+param[j].getCanonicalName());
}
System.out.println(" ");
System.out.println("Exception List");
Class exce[]=m_list1.getExceptionTypes();
for(int j=0;j<exce.length;j++)
{
System.out.println("Method exception #" + j + "
"+exce[j]);
}
System.out.println(" ");
System.out.println("Method return type = "+
m_list1.getReturnType());
System.out.println("----End of method-----
"+(i+1));
System.out.println(" ");
}
/*for constructor*/
System.out.println("###########Constructor
details###########################");
for (int i = 0; i < c_list.length; i++) {
Constructor ct = c_list[i];
System.out.println("Constructor name= " +
ct.getName());
System.out.println("Constructor declared in
class = " +ct.getDeclaringClass());
Class param[] = ct.getParameterTypes();
for (int j = 0; j < param.length; j++)
{ System.out.println("Constr
uctor parameter #" + j + " " + param[j])
}
System.out.println("-----------------");
}
System.out.println(" ");
/*field details*/
System.out.println("############ Field details
####################");
for (int i= 0; i < f_list.length; i++) {
Field fld = f_list[i];
System.out.println("Field name= "
+fld.getName());
System.out.println("Field declared in class = "
+fld.getDeclaringClass());
System.out.println("Field type = " +
fld.getType());
int mod = fld.getModifiers();
System.out.println("Field modifiers = "
+Modifier.toString(mod));
System.out.println("---------------------");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output of the program
##################### Method
details###################
Method--1 details
--------------------
Method name=generateRegno
Method declared in class=class
person.Student
Parameter List
Exception List
Method return type
Method return type = int
----End of method-----1
Method--2 details
--------------------
Method name=getGRegNo
Method declared in class=class
person.Student
Parameter List
Exception List
Parameter List
Method parameter #0 java.lang.String
Exception List
package invokemethod;
public class Add {
Object
retreive=m.invoke(cl.newInstance(),arg_list);
System.out.println(retreive);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Out put is : 133