finding the methods in java using reflection finding the methods in java with example how can we find the methods in java using reflection how can we find the methods in java how can we find the methods in java using reflection with example finding the methods in java using reflection with example finding the methods of a class in java using reflection finding the methods of a class in java using reflection with example how can we find the methods of a class in java using reflection with example how can we find the methods of a class in java using reflection
Finding Methods:
When a class is
loaded into JVM, JVM creates method object to store META DATA
of a method to
get the method objects of a class, we need to call getMethods(). When we
call
getMethods () then internally an array of type method will be created and
stores all
methods objects finally JVM returns that method array.
Syntax:
Method
m[]=c.getMethods();
Method is a
class given in java.lang.reflect package. If we call getMethods then it returns
including inherited methods from base class. If we want only current class
methods excluding
inherited methods of base class, we need to call a method
getDeclaredMethods().
Finding
method is similar to finding constructor but only difference is there is no
return type for constructor there is return type for method.
Example:
//ReflectDemo5.java
import
java.lang.reflect.*;
public class
ReflectDemo5{
public
static void main(String args[]){
try{
Class
c=Class.forName(args[0]);
Method
m[]=c.getMethods();
for(int
i=0;i<m.length;m++) {
String
s1=m[i].getName();
String
rt=m[i].getReturnType().getName();
System.out.println(rt+”
“+s1+”(“);
Class
p[]=m[i].getParameterTypes();
for(int
j=0;j,p.length;j++)
String
s2=p[j].getName();
System.out.println(s2+”)”);
}
}
}
Catch(ClassNotFoundException cnfe){
System.out.println(cnfe);
}
}
}
No comments:
Post a Comment