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 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2003 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(Modifier.toString(mod) + " class "
  44. + cf.getName() + " extends " + cf.getSuperclass());
  45. out.println();
  46. ConstPool cp = cf.getConstPool();
  47. list = cf.getFields();
  48. n = list.size();
  49. for (int i = 0; i < n; ++i) {
  50. FieldInfo finfo = (FieldInfo)list.get(i);
  51. int acc = finfo.getAccessFlags();
  52. out.println(Modifier.toString(AccessFlag.toModifier(acc))
  53. + " " + finfo.getName() + "\t"
  54. + finfo.getDescriptor());
  55. printAttributes(finfo.getAttributes(), out);
  56. }
  57. out.println();
  58. list = cf.getMethods();
  59. n = list.size();
  60. for (int i = 0; i < n; ++i) {
  61. MethodInfo minfo = (MethodInfo)list.get(i);
  62. int acc = minfo.getAccessFlags();
  63. out.println(Modifier.toString(AccessFlag.toModifier(acc))
  64. + " " + minfo.getName() + "\t"
  65. + minfo.getDescriptor());
  66. printAttributes(minfo.getAttributes(), out);
  67. out.println();
  68. }
  69. out.println();
  70. printAttributes(cf.getAttributes(), out);
  71. }
  72. static void printAttributes(List list, PrintWriter out) {
  73. if (list == null)
  74. return;
  75. int n = list.size();
  76. for (int i = 0; i < n; ++i) {
  77. AttributeInfo ai = (AttributeInfo)list.get(i);
  78. if (ai instanceof CodeAttribute) {
  79. CodeAttribute ca = (CodeAttribute)ai;
  80. out.println("attribute: " + ai.getName() + ": "
  81. + ai.getClass().getName());
  82. out.println("max stack " + ca.getMaxStack()
  83. + ", max locals " + ca.getMaxLocals()
  84. + ", " + ca.getExceptionTable().size()
  85. + " catch blocks");
  86. out.println("<code attribute begin>");
  87. printAttributes(ca.getAttributes(), out);
  88. out.println("<code attribute end>");
  89. }
  90. else
  91. out.println("attribute: " + ai.getName()
  92. + " (" + ai.get().length + " byte): "
  93. + ai.getClass().getName());
  94. }
  95. }
  96. }