package test;
public class DemoClass {
private String privateField;
String field;
protected String protectedField;
public String publicField;
public DemoClass() {
}
public DemoClass(String arg) {
}
public void publicMethod() throws java.io.IOException, Exception {
}
public String publicMethod(String s, int i) {
return "s=" + s + " i=" + i;
}
protected void protectedMethod() {
}
private void privateMethod() {
}
void method() {
}
public String publicRetMethod() {
return null;
}
public InnerClass getInnerClass() {
return new InnerClass();
}
public class InnerClass {
}
}
2. DemoTest
package test;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class DemoTest {
public static void main(String[] args) {
DemoClass demoClass = new DemoClass();
DemoTest demoTest = new DemoTest();
demoTest.getClassInfos(demoClass);
}
public void getClassInfos(Object clazz) {
Class demoClass = clazz.getClass();
getClassInfo(demoClass);
getFieldInfos(demoClass);
getMethodInfos(demoClass);
}
public void getClassInfo(Class demoClass) {
String className = demoClass.getName();
String classCanonicalName = demoClass.getCanonicalName();
String classSimpleName = demoClass.getSimpleName();
String packageName = demoClass.getPackage().getName();
String toString = demoClass.toString();
System.out.println("Class Name: " + className);
System.out.println("Class Canonical Name: " + classCanonicalName);
System.out.println("Class Simple Name: " + classSimpleName);
System.out.println("Package Name: " + packageName);
System.out.println("toString: " + toString);
}
public void getFieldInfos(Class demoClass) {
Field[] declaredFields = demoClass.getDeclaredFields();
Field[] fields = demoClass.getFields();
System.out.println("==========");
System.out.println("Number of Declared Fields: "
+ declaredFields.length);
System.out.println("Number of Fields: " + fields.length);
for (Field field : declaredFields) {
getFieldInfo(field);
}
System.out.println("----------");
for (Field field : fields) {
getFieldInfo(field);
}
}
public void getFieldInfo(Field field) {
String fieldName = field.getName();
int modifier = field.getModifiers();
String modifierStr = Modifier.toString(modifier);
String type = field.getType().getSimpleName();
System.out.println(modifierStr + " " + type + " " + fieldName);
}
public void getMethodInfos(Class demoClass) {
Method[] declaredMethods = demoClass.getDeclaredMethods();
Method[] methods = demoClass.getMethods();
System.out.println("==========");
System.out.println("Number of Declared Methods: "
+ declaredMethods.length);
System.out.println("Number of Methods: " + methods.length);
for (Method method : declaredMethods) {
getMethodInfo(method);
}
System.out.println("----------");
for (Method method : methods) {
getMethodInfo(method);
}
}
public void getMethodInfo(Method method) {
String methodName = method.getName();
int modifier = method.getModifiers();
String modifierStr = Modifier.toString(modifier);
String returnType = method.getReturnType().getSimpleName();
Class params[] = method.getParameterTypes();
StringBuilder paramStr = new StringBuilder();
int paramLen = params.length;
if (paramLen != 0) {
paramStr.append("(").append(params[0].getSimpleName())
.append(" arg");
for (int i = 1; i < paramLen; i++) {
paramStr.append(", ").append(params[i].getName())
.append(" arg").append(i);
}
paramStr.append(")");
} else {
paramStr.append("()");
}
Class exceptions[] = method.getExceptionTypes();
StringBuilder exceptionStr = new StringBuilder();
int exceptionLen = exceptions.length;
if (exceptionLen != 0) {
exceptionStr.append("throws ")
.append(exceptions[0].getSimpleName());
for (int i = 1; i < exceptionLen; i++) {
exceptionStr.append(", ").append(exceptions[i].getSimpleName());
}
}
System.out.println(modifierStr + " " + returnType + " " + methodName
+ paramStr + " " + exceptionStr);
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.