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

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