反射机制最重要的内容:检查类的结构。
下面是一些方法:
Field[ ] getFields();
Field[ ] getDeclareFields();
//返回一个包含Field对象的数组,这些对象记录了这个类或其超类的公有域。getDeclareFields方法也将返回包含Field对象的数组,这些对象记录了这个类的全部域。如果类中没有域,或者Class对象描述的是基本类型或数组类型,这些方法将返回一个长度为0的数组。
Method[ ] getMethods();
Method[ ] getDeclareMethods();
//返回包含Methods对象的数组:getMethods方法返回所有的公有方法,包括从超类继承来的公有方法;getDeclareMethods方法返回这个类或接口的全部方法,但不包括从超类继承来的方法。
Constructor[ ] getConstructor();
Constructor[ ] getDeclaredConstructor();
//返回包含Constructor对象的数组,其中包含了Class对象所描述的类的所有公有构造器或所有构造器。
Class getDeclaringClass();
//返回一个用于描述类中定义的构造器、方法或域的Class对象。
Class[ ] getExceptionTypes();(在Constructor和Method类中)
//返回一个用于描述方法抛出的异常类型的Class对象数组。
int getModifiers();
//返回一个用于描述构造器、方法或域的修饰符的整型数值。(在Modifier类中)
String getName();
//返回一个描述构造器、方法或域的字符串。
Class[ ] getParameterTypes();(在Constructor和Method类中)
//返回一个用于描述参数类型的Class对象数组。
Class getReturnTypr();
//返回一个用于描述返回类型的Class对象。
还有一些判断修饰符的方法,就不列出来了。。。
接下来是一个小例子,功能:输入类名,然后输出类中的所有构造器、方法和域:
/**
* This program uses reflection to print all features of a class
* @version 1.8 2019/2/21
* @author Linging
*
*/
public class ReflectionTest {
private static Scanner sc;
public static void main(String[] args) {
// TODO Auto-generated method stub
String name;
if(args.length > 0)
name = args[0];
else {
sc = new Scanner(System.in);
System.out.println("Please input the class name(e.g.java.util.Date):");
name = sc.next();
}
try {
//获取Class对象
Class c1 = Class.forName(name);
//获取该类的父类的Class对象
Class superc1 = c1.getSuperclass();
//获取该父类的修饰符
String type = Modifier.toString(superc1.getModifiers());
if(type.length() > 0)
System.out.print(type+" ");
System.out.print("class "+name);
if(superc1 != null && superc1 != Object.class)
System.out.println(" extends "+superc1.getName());
System.out.println("{");
ReflectionTest.printConstructors(c1);
ReflectionTest.printMethods(c1);
ReflectionTest.printFields(c1);
System.out.print("}");
}catch(Exception e) {
e.printStackTrace();
}
}
/**
* print all Constructors of a class
*/
public static void printConstructors(Class c1) {
// System.out.println("Constructors:");
//返回Constructor对象数组
Constructor[] cons = c1.getDeclaredConstructors();
for(Constructor c : cons) {
System.out.print(" ");
String s = Modifier.toString(c.getModifiers());
if(s.length() > 0) System.out.print(s+" ");
System.out.print(c.getName()+"(");
Class[] CType = c.getParameterTypes();
for(int i = 0; i < CType.length; i++) {
if(i > 0)System.out.print(", ");
System.out.print(CType[i].getName());
}
System.out.println(");");
}
System.out.println();
}
/**
* print all Methods of a class
*/
public static void printMethods(Class c1) {
//System.out.println("Methods:");
//返回Method对象数组
Method[] m = c1.getDeclaredMethods();
for(Method ms : m) {
System.out.print(" ");
String s = Modifier.toString(ms.getModifiers()); //获取修饰符
if(s.length() > 0) System.out.print(s+" ");
Class returnType = ms.getReturnType(); //获取返回类型
System.out.print(returnType.getName()+" ");
System.out.print(ms.getName()+"("); //打印方法名
Class paramType[] = ms.getParameterTypes(); //获取参数
for(int i = 0; i < paramType.length; i++) {
if(i > 0) System.out.print(", ");
System.out.print(paramType[i].getName());
}
System.out.println(");");
}
System.out.println();
}
/**
* print all Fields of a class
*/
public static void printFields(Class c1) {
// System.out.println("Fields:");
//返回Field对象数组
Field[] field = c1.getDeclaredFields();
for(Field f : field) {
System.out.print(" ");
String s = Modifier.toString(f.getModifiers());
if(s.length() > 0) System.out.print(s+" ");
Class returnType = f.getType();
System.out.print(returnType+" "+f.getName());
System.out.println(";");
}
System.out.println();
}
}
以 java.lang.Double 为例结果为:
学习自:《Java核心技术 卷1》。