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.

ClassFileWriter.java 5.2KB

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