Last active
March 31, 2016 11:59
-
-
Save ishitcno1/d92106ad45693a3358ce764e0d85dc05 to your computer and use it in GitHub Desktop.
Print all features of a class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.*; | |
import java.util.Scanner; | |
/** | |
* Print all features of a class | |
* Created by albert on 2016/3/31. | |
*/ | |
public class ClassChecker { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
System.out.print("Enter class name: "); | |
String className = scanner.nextLine(); | |
try { | |
Class cls = Class.forName(className); | |
printClassName(cls); | |
printConstructors(cls); | |
System.out.println(); | |
printFields(cls); | |
System.out.println(); | |
printMethods(cls); | |
System.out.println("}"); | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} | |
System.exit(0); | |
} | |
/** | |
* Print class name | |
* @param cls | |
*/ | |
public static void printClassName(Class cls) { | |
String name = cls.getName(); | |
String modifiers = Modifier.toString(cls.getModifiers()); | |
if (modifiers.length() > 0) { | |
System.out.print(modifiers + " "); | |
} | |
System.out.print("class " + name); | |
Class superCls = cls.getSuperclass(); | |
if (superCls != null && superCls != Object.class) { | |
System.out.print(" extends " + superCls.getName()); | |
} | |
Class[] interfaces = cls.getInterfaces(); | |
for (int i = 0; i < interfaces.length; i++) { | |
if (i == 0) { | |
System.out.print(" implements "); | |
} else { | |
System.out.print(", "); | |
} | |
System.out.print(interfaces[i].getName()); | |
} | |
System.out.println(" {"); | |
} | |
/** | |
* Print all constructors of a class | |
* @param cls | |
*/ | |
public static void printConstructors(Class cls) { | |
Constructor[] constructors = cls.getDeclaredConstructors(); | |
for (Constructor c : constructors) { | |
System.out.print(" "); | |
String name = c.getName(); | |
String modifiers = Modifier.toString(c.getModifiers()); | |
if (modifiers.length() > 0) { | |
System.out.print(modifiers + " "); | |
} | |
System.out.print(name + "("); | |
Class[] paramTypes = c.getParameterTypes(); | |
for (int i = 0; i < paramTypes.length; i++) { | |
if (i > 0) { | |
System.out.print(", "); | |
} | |
System.out.print(paramTypes[i].getName()); | |
} | |
System.out.println(");"); | |
} | |
} | |
/** | |
* Print all fields of a class | |
* @param cls | |
*/ | |
public static void printFields(Class cls) { | |
Field[] fields = cls.getDeclaredFields(); | |
for (Field field : fields) { | |
System.out.print(" "); | |
Type type = field.getType(); | |
String name = field.getName(); | |
String modifiers = Modifier.toString(field.getModifiers()); | |
if (modifiers.length() > 0) { | |
System.out.print(modifiers + " "); | |
} | |
System.out.print(type.getTypeName() + " " + name); | |
if (Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers())) { | |
try { | |
System.out.print(" = " + field.get(null)); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
} | |
System.out.println(";"); | |
} | |
} | |
/** | |
* Print all methods of a class | |
* @param cls | |
*/ | |
public static void printMethods(Class cls) { | |
Method[] methods = cls.getMethods(); | |
for (Method method : methods) { | |
System.out.print(" "); | |
Type type = method.getReturnType(); | |
String name = method.getName(); | |
String modifiers = Modifier.toString(method.getModifiers()); | |
if (modifiers.length() > 0) { | |
System.out.print(modifiers + " "); | |
} | |
System.out.print(type.getTypeName() + " " + name + "("); | |
Class[] paramTypes = method.getParameterTypes(); | |
for (int i = 0; i < paramTypes.length; i++) { | |
if (i > 0) { | |
System.out.print(", "); | |
} | |
System.out.print(paramTypes[i].getTypeName()); | |
} | |
System.out.println(");"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment