简介
java 中的反射机制 -- 检查类的结构 (猜测主要作用于未运行的类)
可以作为调试手段
code
package com;
import java.util.*;
import java.lang.reflect.*;
public class ReflectionTest {
public static void main(String[] args) {
String name;
if (args.length > 0) name = args[0];
else{
Scanner in = new Scanner(System.in);
System.out.println("Enter class name (e.g. java.util.Date): ");
name = in.next();
}
try{
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{\n");
printConstructors(cl);
System.out.println();
printMethods(cl);
System.out.println();
printFields(cl);
System.out.println("}");
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
}
/**
* Prints all constructors of a class
* @param cl a class
* */
public static void printConstructors(Class cl) {
Constructor[] constructors = cl.getDeclaredConstructors();
for(Constructor c: constructors){
String name = c.getName();
System.out.print(" ");
String modifiers = Modifier.toString(c.getModifiers());
if(modifiers.length() > 0) System.out.print(modifiers + " ");
System.out.print(name + "(");
Class[] paramTypes = c.getParameterTypes();
for(int j=0; j<paramTypes.length; j++){
if(j>0) System.out.print(", ");
System.out.print(paramTypes[j].getName());
}
System.out.println(");");
}
}
/**
* Prints all methods of a class
* @param cl a class
* */
public static void printMethods(Class cl){
Method[] methods = cl.getDeclaredMethods();
for(Method m : methods) {
Class retType = m.getReturnType();
String name = m.getName();
System.out.print(" ");
String modifiers = Modifier.toString(m.getModifiers());
if (modifiers.length() > 0) System.out.print(modifiers + " ");
System.out.print(retType.getName() + " " + name + "(");
Class[] paramTypes = m.getParameterTypes();
for(int j = 0; j<paramTypes.length; j++) {
if (j > 0) System.out.print(", ");
System.out.print(paramTypes[j].getName());
}
System.out.println(");");
}
}
/**
* Prints all fields of a class
* @param cl a class
* */
public static void printFields(Class cl) {
Field[] fields = cl.getDeclaredFields();
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] " + modifiers + " [modifiers] ");
System.out.println(type.getName() + " " + name + ";");
}
}
}
Answer
Enter class name (e.g. java.util.Date):
java.lang.Double
public final class java.lang.Double extends java.lang.Number
{
public java.lang.Double(double);
public 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 isFinite(double);
public static boolean isInfinite(double);
public boolean isInfinite();
public static double sum(double, double);
public static double parseDouble(java.lang.String);
[modifiers] public static final [modifiers] double POSITIVE_INFINITY;
[modifiers] public static final [modifiers] double NEGATIVE_INFINITY;
[modifiers] public static final [modifiers] double NaN;
[modifiers] public static final [modifiers] double MAX_VALUE;
[modifiers] public static final [modifiers] double MIN_NORMAL;
[modifiers] public static final [modifiers] double MIN_VALUE;
[modifiers] public static final [modifiers] int MAX_EXPONENT;
[modifiers] public static final [modifiers] int MIN_EXPONENT;
[modifiers] public static final [modifiers] int SIZE;
[modifiers] public static final [modifiers] int BYTES;
[modifiers] public static final [modifiers] java.lang.Class TYPE;
[modifiers] private final [modifiers] double value;
[modifiers] private static final [modifiers] long serialVersionUID;
}
---------------------------我的天空里没有太阳,总是黑夜,但并不暗,因为有东西代替了太阳。虽然没有太阳那么明亮,但对我来说已经足够。凭借着这份光,我便能把黑夜当成白天。我从来就没有太阳,所以不怕失去。
--------《白夜行》