You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ClassFilePrinter.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later,
  9. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist.bytecode;
  17. import java.io.PrintWriter;
  18. import javassist.Modifier;
  19. import java.util.List;
  20. /**
  21. * A utility class for priting the contents of a class file.
  22. * It prints a constant pool table, fields, and methods in a
  23. * human readable representation.
  24. */
  25. public class ClassFilePrinter {
  26. /**
  27. * Prints the contents of a class file to the standard output stream.
  28. */
  29. public static void print(ClassFile cf) {
  30. print(cf, new PrintWriter(System.out, true));
  31. }
  32. /**
  33. * Prints the contents of a class file.
  34. */
  35. public static void print(ClassFile cf, PrintWriter out) {
  36. List list;
  37. int n;
  38. /* 0x0020 (SYNCHRONIZED) means ACC_SUPER if the modifiers
  39. * are of a class.
  40. */
  41. int mod
  42. = AccessFlag.toModifier(cf.getAccessFlags()
  43. & ~AccessFlag.SYNCHRONIZED);
  44. out.println("major: " + cf.major + ", minor: " + cf.minor
  45. + " modifiers: " + Integer.toHexString(cf.getAccessFlags()));
  46. out.println(Modifier.toString(mod) + " class "
  47. + cf.getName() + " extends " + cf.getSuperclass());
  48. String[] infs = cf.getInterfaces();
  49. if (infs != null && infs.length > 0) {
  50. out.print(" implements ");
  51. out.print(infs[0]);
  52. for (int i = 1; i < infs.length; ++i)
  53. out.print(", " + infs[i]);
  54. out.println();
  55. }
  56. out.println();
  57. list = cf.getFields();
  58. n = list.size();
  59. for (int i = 0; i < n; ++i) {
  60. FieldInfo finfo = (FieldInfo)list.get(i);
  61. int acc = finfo.getAccessFlags();
  62. out.println(Modifier.toString(AccessFlag.toModifier(acc))
  63. + " " + finfo.getName() + "\t"
  64. + finfo.getDescriptor());
  65. printAttributes(finfo.getAttributes(), out, 'f');
  66. }
  67. out.println();
  68. list = cf.getMethods();
  69. n = list.size();
  70. for (int i = 0; i < n; ++i) {
  71. MethodInfo minfo = (MethodInfo)list.get(i);
  72. int acc = minfo.getAccessFlags();
  73. out.println(Modifier.toString(AccessFlag.toModifier(acc))
  74. + " " + minfo.getName() + "\t"
  75. + minfo.getDescriptor());
  76. printAttributes(minfo.getAttributes(), out, 'm');
  77. out.println();
  78. }
  79. out.println();
  80. printAttributes(cf.getAttributes(), out, 'c');
  81. }
  82. static void printAttributes(List list, PrintWriter out, char kind) {
  83. if (list == null)
  84. return;
  85. int n = list.size();
  86. for (int i = 0; i < n; ++i) {
  87. AttributeInfo ai = (AttributeInfo)list.get(i);
  88. if (ai instanceof CodeAttribute) {
  89. CodeAttribute ca = (CodeAttribute)ai;
  90. out.println("attribute: " + ai.getName() + ": "
  91. + ai.getClass().getName());
  92. out.println("max stack " + ca.getMaxStack()
  93. + ", max locals " + ca.getMaxLocals()
  94. + ", " + ca.getExceptionTable().size()
  95. + " catch blocks");
  96. out.println("<code attribute begin>");
  97. printAttributes(ca.getAttributes(), out, kind);
  98. out.println("<code attribute end>");
  99. }
  100. else if (ai instanceof AnnotationsAttribute) {
  101. out.println("annnotation: " + ai.toString());
  102. }
  103. else if (ai instanceof ParameterAnnotationsAttribute) {
  104. out.println("parameter annnotations: " + ai.toString());
  105. }
  106. else if (ai instanceof StackMapTable) {
  107. out.println("<stack map table begin>");
  108. StackMapTable.Printer.print((StackMapTable)ai, out);
  109. out.println("<stack map table end>");
  110. }
  111. else if (ai instanceof StackMap) {
  112. out.println("<stack map begin>");
  113. ((StackMap)ai).print(out);
  114. out.println("<stack map end>");
  115. }
  116. else if (ai instanceof SignatureAttribute) {
  117. SignatureAttribute sa = (SignatureAttribute)ai;
  118. String sig = sa.getSignature();
  119. out.println("signature: " + sig);
  120. try {
  121. String s;
  122. if (kind == 'c')
  123. s = SignatureAttribute.toClassSignature(sig).toString();
  124. else if (kind == 'm')
  125. s = SignatureAttribute.toMethodSignature(sig).toString();
  126. else
  127. s = SignatureAttribute.toFieldSignature(sig).toString();
  128. out.println(" " + s);
  129. }
  130. catch (BadBytecode e) {
  131. out.println(" syntax error");
  132. }
  133. }
  134. else
  135. out.println("attribute: " + ai.getName()
  136. + " (" + ai.get().length + " byte): "
  137. + ai.getClass().getName());
  138. }
  139. }
  140. }