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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 java.util.List;
  19. import javassist.Modifier;
  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. /* 0x0020 (SYNCHRONIZED) means ACC_SUPER if the modifiers
  37. * are of a class.
  38. */
  39. int mod
  40. = AccessFlag.toModifier(cf.getAccessFlags()
  41. & ~AccessFlag.SYNCHRONIZED);
  42. out.println("major: " + cf.major + ", minor: " + cf.minor
  43. + " modifiers: " + Integer.toHexString(cf.getAccessFlags()));
  44. out.println(Modifier.toString(mod) + " class "
  45. + cf.getName() + " extends " + cf.getSuperclass());
  46. String[] infs = cf.getInterfaces();
  47. if (infs != null && infs.length > 0) {
  48. out.print(" implements ");
  49. out.print(infs[0]);
  50. for (int i = 1; i < infs.length; ++i)
  51. out.print(", " + infs[i]);
  52. out.println();
  53. }
  54. out.println();
  55. List<FieldInfo> fields = cf.getFields();
  56. for (FieldInfo finfo:fields) {
  57. int acc = finfo.getAccessFlags();
  58. out.println(Modifier.toString(AccessFlag.toModifier(acc))
  59. + " " + finfo.getName() + "\t"
  60. + finfo.getDescriptor());
  61. printAttributes(finfo.getAttributes(), out, 'f');
  62. }
  63. out.println();
  64. List<MethodInfo> methods = cf.getMethods();
  65. for (MethodInfo minfo:methods) {
  66. int acc = minfo.getAccessFlags();
  67. out.println(Modifier.toString(AccessFlag.toModifier(acc))
  68. + " " + minfo.getName() + "\t"
  69. + minfo.getDescriptor());
  70. printAttributes(minfo.getAttributes(), out, 'm');
  71. out.println();
  72. }
  73. out.println();
  74. printAttributes(cf.getAttributes(), out, 'c');
  75. }
  76. static void printAttributes(List<AttributeInfo> list, PrintWriter out, char kind) {
  77. if (list == null)
  78. return;
  79. for (AttributeInfo ai:list) {
  80. if (ai instanceof CodeAttribute) {
  81. CodeAttribute ca = (CodeAttribute)ai;
  82. out.println("attribute: " + ai.getName() + ": "
  83. + ai.getClass().getName());
  84. out.println("max stack " + ca.getMaxStack()
  85. + ", max locals " + ca.getMaxLocals()
  86. + ", " + ca.getExceptionTable().size()
  87. + " catch blocks");
  88. out.println("<code attribute begin>");
  89. printAttributes(ca.getAttributes(), out, kind);
  90. out.println("<code attribute end>");
  91. }
  92. else if (ai instanceof AnnotationsAttribute) {
  93. out.println("annnotation: " + ai.toString());
  94. }
  95. else if (ai instanceof ParameterAnnotationsAttribute) {
  96. out.println("parameter annnotations: " + ai.toString());
  97. }
  98. else if (ai instanceof StackMapTable) {
  99. out.println("<stack map table begin>");
  100. StackMapTable.Printer.print((StackMapTable)ai, out);
  101. out.println("<stack map table end>");
  102. }
  103. else if (ai instanceof StackMap) {
  104. out.println("<stack map begin>");
  105. ((StackMap)ai).print(out);
  106. out.println("<stack map end>");
  107. }
  108. else if (ai instanceof SignatureAttribute) {
  109. SignatureAttribute sa = (SignatureAttribute)ai;
  110. String sig = sa.getSignature();
  111. out.println("signature: " + sig);
  112. try {
  113. String s;
  114. if (kind == 'c')
  115. s = SignatureAttribute.toClassSignature(sig).toString();
  116. else if (kind == 'm')
  117. s = SignatureAttribute.toMethodSignature(sig).toString();
  118. else
  119. s = SignatureAttribute.toFieldSignature(sig).toString();
  120. out.println(" " + s);
  121. }
  122. catch (BadBytecode e) {
  123. out.println(" syntax error");
  124. }
  125. }
  126. else
  127. out.println("attribute: " + ai.getName()
  128. + " (" + ai.get().length + " byte): "
  129. + ai.getClass().getName());
  130. }
  131. }
  132. }