Q) Write
a java program to
obtain information about methods which are present in a class?
Answer:
import java.lang.reflect.*;
class MetInfo
{
public static void main (String [] args)
{
try
{
if (args.length==0)
{
System.out.println
("PLEASE PASS THE CLASS NAME..!");
}
else
{
Class c=Class.forName (args
[0]);
printMethods (c);
}
}
catch (ClassNotFoundException cnfe)
{
System.out.println
(args [0]+" DOES NOT EXISTS...");
}
}
static void printMethods
(Class c)
{
Method m []=c.getMethods ();
System.out.println
("NUMBER OF METHODS = "+m.length);
System.out.println
("NAME OF THE CLASS : "+c.getName ());
for (int i=0;
i<m.length; i++)
{
Class c1=m [i].getReturnType ();
String rtype=c1.getName ();
String mname=m [i].getName ();
System.out.print
(rtype+" "+mname+"(");
Class mp []=m [i].getParameterTypes ();
for (int j=0;
j<mp.length; j++)
{
String ptype=mp [i].getName ();
System.out.print
(ptype+",");
}
System.out.println
("\b"+")");
}
}
};