分析Java解释器能够加载的类

public class ReflectionTest {
    public static void main(String[] args) {
        String name;
        if (args.length > 0) {
            name = args[0];
        } else {
            Scanner sc = new Scanner(System.in);
            System.out.println("输入类名:(e.g:java.util.Date):");
            name = sc.next();
        }
        try {
            Class c1 = Class.forName(name);
            Class supperc1 = c1.getSuperclass();

            String modifiers = Modifier.toString(c1.getModifiers());

            if (modifiers.length() > 0) {
                System.out.print(modifiers + " ");
            }
            System.out.print("class" + name);

            if (supperc1 != null && supperc1 != Object.class) {
                System.out.print("extends" + supperc1.getName());
            }

            System.out.print("\n{\n");
            printConstructors(c1);

            System.out.println();
            printMethods(c1);
            System.out.println();
            printFilds(c1);
            System.out.println("\n}\n");

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        System.exit(0);

    }

    private static void printConstructors(Class c1) {
        Constructor[] constructors = c1.getDeclaredConstructors();
        for (Constructor c : constructors) {
            String name = c.getName();
            System.out.println("   ");
            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(")");
        }
    }

    private static void printMethods(Class c1) {
        Method[] methods = c1.getDeclaredMethods();
        for (Method m : methods) {
            Class retType = m.getReturnType();
            String name = m.getName();
            System.out.println("   ");
            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(")");
        }
    }

    private static void printFilds(Class c1) {
        Field[] fields = c1.getDeclaredFields();
        for (Field f : fields) {
            Class retType = f.getType();
            String name = f.getName();
            System.out.println("   ");
            String modifiers = Modifier.toString(f.getModifiers());
            if (modifiers.length() > 0) {
                System.out.print(modifiers + "");
            }
            System.out.print(retType.getName() + "" + name + "(");
        }
    }
}

结果示例:

输入类名:(e.g:java.util.Date):
java.lang.Object
public classjava.lang.Object
{

publicjava.lang.Object()


protectedvoidfinalize()
   
public finalvoidwait()
   
public finalvoidwait(long,int)
   
public final nativevoidwait(long)
   
publicbooleanequals(java.lang.Object)
   
publicjava.lang.StringtoString()
   
public nativeinthashCode()
   
public final nativejava.lang.ClassgetClass()
   
protected nativejava.lang.Objectclone()
   
public final nativevoidnotify()
   
public final nativevoidnotifyAll()
   
private static nativevoidregisterNatives()


}


 

 

你可能感兴趣的:(java)