import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* Make a class which lists all the constructors, methods and fields of the
* specified class.
*
*
*/
public class ClassPropertyTest1 {
/**
* execution begins from here
*
* @param args
* commandline arguments
*/
public static void main(String[] args) {
// get the data of MyClass
new ClassPropertyTest1().getSourceClassData();
}
/**
* gets all the data of class SourceClass
*
*/
public void getSourceClassData() {
System.out.println("SourceClass Details : ");
System.out.println("---------------------");
SourceClass srcObj = new SourceClass();
System.out.println("\tAvailable Fields Are ::");
System.out.println("\t-----------------------");
int i = 0;
for (Field fldObj : srcObj.getClass().getDeclaredFields()) {
System.out.println("\tField (" + (i++) + ") : " + fldObj);
}
System.out.println("\n\n\tAvailable Constructors Are ::");
System.out.println("\t-----------------------------");
i = 0;
for (Constructor cObj : srcObj.getClass().getDeclaredConstructors()) {
System.out.println("\tConstructor (" + (i++) + ") : " + cObj);
}
System.out.println("\n\n\tAvailable Methods Are ::");
System.out.println("\t------------------------");
i = 0;
for (Method cObj : srcObj.getClass().getDeclaredMethods()) {
System.out.println("\tMethod (" + (i++) + ") : " + cObj);
}
}
}
/**
* source class for getting data of its memebers
*
*/
class SourceClass {
// member variables
private int intVar;
private static final float PI = 3.14f;
// default constructor
SourceClass() {
intVar = 0;
}
// parameeterized constructor
SourceClass(int intV) {
intVar = intV;
}
// displays value of intVar
public void display() {
System.out.println("Value of intVar : " + intVar);
}
}
No comments:
Post a Comment