Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. import java.util.*;
  2. import java.lang.reflect.*;
  3.  
  4. /**
  5. * Get basic information about a class by reflection.
  6. */
  7. public class Jook {
  8. public static void main(String[] args) {
  9. if (args.length == 0) {
  10. System.out.println("Usage: java Jook classname");
  11. System.exit(0);
  12. }
  13.  
  14. try {
  15. Class<?> clazz = Class.forName(args[0]);
  16. Class<?> superClazz = clazz.getSuperclass();
  17. String modifiers = Modifier.toString(clazz.getModifiers());
  18.  
  19. if (superClazz != null && superClazz != Object.class) {
  20. System.out.println(String.format("%s class %s extends %s {", modifiers, clazz.getName(),
  21. superClazz.getName()));
  22. } else {
  23. System.out.println(String.format("%s class %s {", modifiers, clazz.getName(),
  24. superClazz.getName()));
  25. }
  26.  
  27. System.out.println(" Constructor:");
  28. printConstructors(clazz);
  29.  
  30. System.out.println(" Method:");
  31. printMethods(clazz);
  32.  
  33. System.out.println(" Field:");
  34. printFields(clazz);
  35.  
  36. System.out.println("}");
  37. } catch (ClassNotFoundException e) {
  38. System.out.println("Not found class!");
  39. }
  40. }
  41.  
  42. public static void printConstructors(Class<?> clazz) {
  43. Constructor[] constructors = clazz.getDeclaredConstructors();
  44.  
  45. for (Constructor constructor : constructors) {
  46. String modifiers = Modifier.toString(constructor.getModifiers());
  47.  
  48. Class<?>[] paramTypes = constructor.getParameterTypes();
  49. String[] paramTypesName = new String[paramTypes.length];
  50. for (int i = 0; i < paramTypes.length; ++i) {
  51. paramTypesName[i] = paramTypes[i].getName();
  52. }
  53.  
  54. System.out.println(String.format("\t%s %s(%s);", modifiers, constructor.getName(),
  55. String.join(", ", paramTypesName)));
  56. }
  57. }
  58.  
  59. public static void printMethods(Class<?> clazz) {
  60. Method[] methods = clazz.getDeclaredMethods();
  61.  
  62. for (Method method : methods) {
  63. String modifiers = Modifier.toString(method.getModifiers());
  64. Class<?> returnType = method.getReturnType();
  65.  
  66. Class<?>[] paramTypes = method.getParameterTypes();
  67. String[] paramTypesName = new String[paramTypes.length];
  68. for (int i = 0; i < paramTypes.length; ++i) {
  69. paramTypesName[i] = paramTypes[i].getName();
  70. }
  71.  
  72. System.out.println(String.format("\t%s %s %s(%s);", modifiers, returnType.getName(),
  73. method.getName(), String.join(", ", paramTypesName)));
  74. }
  75. }
  76.  
  77. public static void printFields(Class<?> clazz) {
  78. Field[] fields = clazz.getDeclaredFields();
  79.  
  80. for (Field field : fields) {
  81. String modifiers = Modifier.toString(field.getModifiers());
  82. System.out.println(String.format("\t%s %s %s", modifiers, field.getType().getName(),
  83. field.getName()));
  84. }
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement