Java 反射 - 利用反射分析类的能力
在 java.lang.reflect 包中有三个类: Field, Method, Constructor,
分别用于描述类的字段, 方法和构造器.
三个类都有 getName() 方法, 用来返回字段, 方法和构造器的名称.
- Field 类有一个 getType 方法
java.lang.reflect.Field
public Class<?> getType(); // 返回描述字段类型的一个对象
-
Method 类和 Constructor 类都有报告参数类型的方法.
-
Method 类还有一个报告返回类型的方法.
-
这三个类都有一个名为 getModifiers 方法, 他将返回一个整数, 用 0/1 位描述所使用的修饰符.
-
可以用 Modifier 类的静态方法分析 getModifier 方法返回的整数.
- Modifier.isPublic
- Modifier.isPrivate
- Modifier.isFinal
- Modifier.toString
-
-
Class 类中的 getFields, getMethods, getConstructors 方法将分别返回这个类支持的公共字段, 方法, 和构造器的数组, 其中包括超类的公共成员.
-
Class 类中的 getDeclareFields, getDeclareMethods, getDeclareConstructor 方法将会返回类中声明的全部字段, 方法, 和构造器的数组. 其中包括私有成员, 受保护成员, 但是不包括超类的成员.
API
java.lang.Class
Field[] getFields(); Field[] getDeclaredFields(); Method[] getMethods(); Method[] getDeclaredMethods(); Constructor[] getConstructors(); Constructor[] getDeclaredConstructors(); String getPackageName();
java.lang.reflect.Field
java.lang.reflect.Method
Java.lang.reflect.Constructor
Class getDeclaringClass(); Class[] getExceptionTypes(); int getModifiers(); String getName(); Class[] getParameterTypes(); // (Constructor 和 Method 类中) Class getReturnType(); // (在 Method 类中);
java.lang.reflect.Modifier
static String toString(int modifiers); static boolean isAbstract(int modifiers); static boolean isFinal(int modifiers); static boolean isInterface(int modifiers); static boolean isNative(int modifiers); static boolean isPrivate(int modifiers); static boolean isProtected(int modifiers); static boolean isPublic(int modifiers); static boolean isStatic(int modifiers); static boolean isStrict(int modifiers);; static boolean isSynchronized(int modifiers); static boolean isVolatile(int modifiers);
使用实例
package july14;
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 ReflectionTest {
public static void main(String[] args) throws ReflectiveOperationException {
String name;
if (args.length > 0)
name = args[0];
else {
var in = new Scanner(System.in);
System.out.println("Enter class name (e.g. java.util.Date): ");
name = in.next();
}
Class cl = Class.forName(name);
Class superCl = cl.getSuperclass();
String modifiers = Modifier.toString(cl.getModifiers());
if (modifiers.length() > 0)
System.out.print(modifiers+" ");
System.out.print("class "+name);
if (superCl!=null && superCl != Object.class)
System.out.print("extends "+superCl.getName());
System.out.print(" {\n");
printConstructors(cl);
System.out.println();
printMethods(cl);
System.out.println();
printFields(cl);
System.out.println("}");
}
private static void printMethods(Class cl) {
Method[] methods = cl.getMethods();
for (Method method : methods) {
Class retType = method.getReturnType();
String name = method.getName();
System.out.print(" ");
String modifiers = Modifier.toString(method.getModifiers());
if (modifiers.length()>0)
System.out.print(modifiers+" ");
System.out.print(retType.getName()+" "+name+"(");
Class[] paramTypes = method.getParameterTypes();
for (int j=0; j<paramTypes.length; j++) {
if (j>0) System.out.print(", ");
System.out.print(paramTypes[j].getName());
}
System.out.println(");");
}
}
private static void printFields(Class cl) {
Field[] fields = cl.getFields();
for (Field f : fields) {
Class type = f.getType();
String name = f.getName();
System.out.print(" ");
String modifiers = Modifier.toString(f.getModifiers());
if (modifiers.length()>0) System.out.print(modifiers+" ");
System.out.println(type.getName()+" "+name+";");
}
}
private static void printConstructors(Class cl) {
Constructor[] constructors = cl.getConstructors();
for (Constructor constructor : constructors) {
String name = cl.getName();
System.out.print(" ");
String modifiers = Modifier.toString(cl.getModifiers());
if (modifiers.length()>0)
System.out.print(modifiers+" ");
System.out.print(name+"(");
Class[] paramTypes = constructor.getParameterTypes();
for (int i = 0; i < paramTypes.length; i++) {
if (i>0)
System.out.print(", ");
System.out.print(paramTypes[i].getName());
}
System.out.println(");");
}
}
}
输入 java.lang.Double
打印输出:
public final class java.lang.Doubleextends java.lang.Number { public final java.lang.Double(double); public final java.lang.Double(java.lang.String); public boolean equals(java.lang.Object); public static java.lang.String toString(double); public java.lang.String toString(); public int hashCode(); public static int hashCode(double); public static double min(double, double); public static double max(double, double); public static native long doubleToRawLongBits(double); public static long doubleToLongBits(double); public static native double longBitsToDouble(long); public volatile int compareTo(java.lang.Object); public int compareTo(java.lang.Double); public byte byteValue(); public short shortValue(); public int intValue(); public long longValue(); public float floatValue(); public double doubleValue(); public static java.lang.Double valueOf(java.lang.String); public static java.lang.Double valueOf(double); public static java.lang.String toHexString(double); public static int compare(double, double); public static boolean isNaN(double); public boolean isNaN(); public static boolean isInfinite(double); public boolean isInfinite(); public static boolean isFinite(double); public static double sum(double, double); public static double parseDouble(java.lang.String); public final native void wait(long); public final void wait(long, int); public final void wait(); public final native java.lang.Class getClass(); public final native void notify(); public final native void notifyAll(); public static final double POSITIVE_INFINITY; public static final double NEGATIVE_INFINITY; public static final double NaN; public static final double MAX_VALUE; public static final double MIN_NORMAL; public static final double MIN_VALUE; public static final int MAX_EXPONENT; public static final int MIN_EXPONENT; public static final int SIZE; public static final int BYTES; public static final java.lang.Class TYPE; }