<pre>import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Scanner;
public class TestReflection {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
String className = null;
Scanner scan = null;
if(args.length > 0)
className = args[0];
else {
scan = new Scanner(System.in);
className = scan.next();
Class<?> cl = Class.forName(className);
Class<?> su = cl.getSuperclass();
String modifier = Modifier.toString(cl.getModifiers());
if(modifier.length() > 0)
System.out.print(modifier + " ");
System.out.print("class " + cl.getName());
if(su != null && su != Object.class)
System.out.print(" extends " + su.getName());
System.out.println(" {");
printFields(Double.class);
System.out.println();
printConstructor(Double.class);
System.out.println();
printMethods(Double.class);
System.out.println("}");
}
}
public static void printConstructor(Class<?> cl) {
Constructor<?>[] constructors = cl.getDeclaredConstructors();
for(Constructor<?> constructor : constructors) {
String modifier = Modifier.toString(constructor.getModifiers());
if(modifier.length() > 0)
System.out.print(" " + modifier + " ");
System.out.print(cl.getName() + "(");
Class<?>[] parameterTypes = constructor.getParameterTypes();
for(int i = 0; i < parameterTypes.length; i++) {
if(i > 0)
System.out.print(", ");
System.out.print(parameterTypes[i].getName());
}
System.out.println(");");
}
}
public static void printMethods(Class<?> cl) {
Method[] methods = cl.getDeclaredMethods();
for (Method method : methods) {
String modifier = Modifier.toString(method.getModifiers());
if(modifier.length() > 0)
System.out.print(" " + modifier + " ");
Class<?> returnType = method.getReturnType();
System.out.print(returnType.getName() + " " + method.getName() + "(");
Class<?>[] parameterTypes = method.getParameterTypes();
for (int i = 0; i < parameterTypes.length; i++) {
if(i > 0)
System.out.print(", ");
System.out.print(parameterTypes[i].getName());
}
System.out.println(");");
}
}
public static void printFields(Class<?> cl) {
Field[] fields = cl.getFields();
for (Field field : fields) {
String modifier = Modifier.toString(field.getModifiers());
if(modifier.length() > 0)
System.out.print(" " + modifier + " ");
Class<?> type = field.getType();
String name = field.getName();
System.out.println(type + " " + name + ";");
}
}
}
</pre>
代码片段: javase-gistfile1.txt
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Scanner;
public cl...
- 发表于 2018-07-07 22:00
- 阅读 ( 5234 )
- 分类:代码片段
你可能感兴趣的文章
- Java 添加条码、二维码到PDF文档 133 浏览
- Java 在Excel中添加筛选器并执行筛选 90 浏览
- JAVA + VUE + 简洁UI:分离式软件平台形成之旅 264 浏览
- Java 设置Word中的表格自适应的3种方式 112 浏览
- LeaRun快速开发平台,.net/java项目开发工具简析 141 浏览
- JVM学习笔记之类装载器-ClassLoader 202 浏览
随机文章
相关问题
- java基础知识提问 1 回答
条评论
请先 登录 后评论
发送私信
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!