This class is mainly used to inspect specific class in java. It will help you to know about classes, methods, interfaces ect. about class. This class is present at java.lang.Class present from JDK 1.0.
Here I am giving simple example of it because main intention
is to know about class. After that you can explore that class. I am giving explanation in code itself so read comments carefully.
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
public class Main {
public static void main(String[] args) {
try {
// Checking whether method returns void ro not.
System.out.print("\nReturn type of method\n");
Class c1 = Test1.class.getMethod("testing1",null).getReturnType();
System.out.println(c1 == Void.TYPE);
/* Obtaining all methods of class.
* It will return you all methods of java class including methods
* it is obtaining from its super class. For example Methods from
* object class. But here it will give you private methods.
* For private methods you hava to use getDeclaredMethods()*/
System.out.print("\n\nAll methods");
Method[] m = Test1.class.getMethods();
for(int i=0; i < m.length; i++) {
System.out.print("\nMethod : " + m[i].toString());
}
/* Obtaining declared methods of class.
* It will return you all methods declared in specific class
* including private methods. It will not return superclass methods.*/
System.out.print("\n\nDeclared methods");
Method[] m2 = Test1.class.getDeclaredMethods();
for(int i=0; i < m2.length; i++) {
System.out.print("\nDeclared Methods : " + m2[i].toString());
}
/* Here you will get all declared constructors
* including private ones.
*
*/
System.out.print("\n\nAll constructors");
Constructor[] c2 = Test1.class.getDeclaredConstructors();
for(int i=0; i < c2.length; i++) {
System.out.print("\nConstructors : " + c2[i].toString());
}
/* Here you will get all declared constructors
* excluding private ones.
*
*/
System.out.print("\n\nPublic constructors");
Constructor[] c3 = Test1.class.getConstructors();
for(int i=0; i < c3.length; i++) {
System.out.print("\nConstructors : " + c3[i].toString());
}
/* It will return you all public member classes of specific class
* including member classes of super class.
* It will not return any private class.
*/
System.out.print("\n\nAll public Classes");
Class[] cls = Test1.class.getClasses();
for(int i=0; i < cls.length; i++) {
System.out.print("\nClasses : " + cls[i].toString());
}
/* It will return you all declared member classes including
* private member classes.
*/
/*
* In the same way you can analyze interfaces.
*/
System.out.print("\n\nAll declared Classes");
Class[] cls1 = Test1.class.getDeclaredClasses();
for(int i=0; i < cls1.length; i++) {
System.out.print("\nClasses : " + cls1[i].toString());
}
} catch (Exception e) {
System.out.print("\nError : " + e);
}
}
}
class Test2 {
public Test2() {
}
public void superClassMethod() {
}
public class innerTest2 {
}
private class innerPrivateTest2 {
}
}
class Test1 extends Test2{
public Test1() {
}
private Test1(String str) {
}
public void testing1(){
}
public String testing2() {
return null;
}
private void testing3() {
}
public class innerTest1 {
}
private class innerPrivateTest1 {
}
}
No comments:
Post a Comment