ConstantPool.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**********************************************************************/
  2. /* ConstantPool class for J2CPS */
  3. /* */
  4. /* (c) copyright QUT */
  5. /**********************************************************************/
  6. package J2CPS;
  7. import java.io.*;
  8. /* The constant pool from the ClassFile */
  9. public class ConstantPool {
  10. Object pool[]; /* the constant pool */
  11. /* Tags for constant pool entries */
  12. public final static int CONSTANT_Utf8 = 1;
  13. public static final int CONSTANT_Unicode = 2;
  14. public final static int CONSTANT_Integer = 3;
  15. public final static int CONSTANT_Float = 4;
  16. public final static int CONSTANT_Long = 5;
  17. public final static int CONSTANT_Double = 6;
  18. public final static int CONSTANT_Class = 7;
  19. public final static int CONSTANT_String = 8;
  20. public final static int CONSTANT_Fieldref = 9;
  21. public final static int CONSTANT_Methodref = 10;
  22. public final static int CONSTANT_InterfaceMethodref = 11;
  23. public final static int CONSTANT_NameAndType = 12;
  24. public final static int CONSTANT_Unknown = 13;
  25. /* access flags */
  26. public static final int ACC_PUBLIC = 0x0001;
  27. public static final int ACC_PRIVATE = 0x0002;
  28. public static final int ACC_PROTECTED = 0x0004;
  29. public static final int ACC_STATIC = 0x0008;
  30. public static final int ACC_FINAL = 0x0010;
  31. public static final int ACC_SYNCHRONIZED = 0x0020;
  32. public static final int ACC_VOLATILE = 0x0040;
  33. public static final int ACC_TRANSIENT = 0x0080;
  34. public static final int ACC_NATIVE = 0x0100;
  35. public static final int ACC_INTERFACE = 0x0200;
  36. public static final int ACC_ABSTRACT = 0x0400;
  37. public ConstantPool(DataInputStream stream) throws IOException {
  38. /* read the number of entries in the constant pool */
  39. int count = stream.readUnsignedShort();
  40. /* read in the constant pool */
  41. pool = new Object[count];
  42. for (int i = 1; i < count; i++) {
  43. Object c = ReadConstant(stream, i);
  44. pool[i] = c;
  45. /* note that Long and Double constant occupies two entries */
  46. if (c instanceof Long || c instanceof Double) { i++; }
  47. }
  48. for (int i = 1; i < pool.length; i++) {
  49. if (pool[i] instanceof Reference) {
  50. ((Reference)pool[i]).Resolve();
  51. } else if (pool[i] instanceof ClassRef) {
  52. ((ClassRef)pool[i]).Resolve();
  53. }
  54. }
  55. }
  56. public void EmptyConstantPool() {
  57. for (int i = 1; i < pool.length; i++) {
  58. pool[i] = null;
  59. }
  60. pool = null;
  61. }
  62. private Object ReadConstant(DataInputStream stream, int index)
  63. throws IOException {
  64. int tag = stream.readUnsignedByte();
  65. switch (tag) {
  66. case CONSTANT_Utf8:
  67. return new String(stream.readUTF());
  68. case CONSTANT_Integer:
  69. return new Integer(stream.readInt());
  70. case CONSTANT_Float:
  71. return new Float(stream.readFloat());
  72. case CONSTANT_Long:
  73. return new Long(stream.readLong());
  74. case CONSTANT_Double:
  75. return new Double(stream.readDouble());
  76. case CONSTANT_Class:
  77. return new ClassRef(this,stream.readUnsignedShort());
  78. case CONSTANT_String:
  79. return new StringRef(this,stream.readUnsignedShort());
  80. case CONSTANT_Fieldref:
  81. return new FieldRef(this,stream.readUnsignedShort(),
  82. stream.readUnsignedShort());
  83. case CONSTANT_Methodref:
  84. return new MethodRef(this,stream.readUnsignedShort(),
  85. stream.readUnsignedShort());
  86. case CONSTANT_InterfaceMethodref:
  87. return new InterfaceMethodRef(this,stream.readUnsignedShort(),
  88. stream.readUnsignedShort());
  89. case CONSTANT_NameAndType:
  90. return new NameAndType(this,stream.readUnsignedShort(),
  91. stream.readUnsignedShort());
  92. default:
  93. System.out.println("Unrecognized constant type: "+String.valueOf(tag));
  94. return null;
  95. }
  96. }
  97. public final Object Get(int index) {
  98. return pool[index];
  99. }
  100. public int GetNumEntries() {
  101. return pool.length;
  102. }
  103. /** Returns a String representing the Constant Pool */
  104. public void PrintConstantPool() {
  105. System.out.println(" CONSTANT POOL ENTRIES (" + pool.length + ")");
  106. for (int i = 1; i < pool.length; i++) {
  107. System.out.print(i + " ");
  108. if (pool[i] instanceof String) {
  109. System.out.println("<String> " + pool[i]);
  110. } else if (pool[i] instanceof Integer) {
  111. System.out.println("<Integer> " + pool[i].toString());
  112. } else if (pool[i] instanceof Float) {
  113. System.out.println("<Float > " + pool[i].toString());
  114. } else if (pool[i] instanceof Long) {
  115. System.out.println("<Long > " + pool[i].toString());
  116. } else if (pool[i] instanceof Double) {
  117. System.out.println("<Double> " + pool[i].toString());
  118. } else {
  119. System.out.println(pool[i].toString());
  120. }
  121. if (pool[i] instanceof Long || pool[i] instanceof Double) i++;
  122. }
  123. System.out.println();
  124. }
  125. /** Constructs a string from a set of access flags */
  126. public static String GetAccessString(int flags) {
  127. StringBuffer result = new StringBuffer();
  128. if ((flags & ACC_PUBLIC) != 0) result.append("public ");
  129. if ((flags & ACC_PRIVATE) != 0) result.append("private ");
  130. if ((flags & ACC_PROTECTED) != 0) result.append("protected ");
  131. if ((flags & ACC_STATIC) != 0) result.append("static ");
  132. if ((flags & ACC_FINAL) != 0) result.append("final ");
  133. if ((flags & ACC_SYNCHRONIZED) != 0) result.append("synchronized ");
  134. if ((flags & ACC_VOLATILE) != 0) result.append("volatile ");
  135. if ((flags & ACC_TRANSIENT) != 0) result.append("transient ");
  136. if ((flags & ACC_NATIVE) != 0) result.append("native ");
  137. if ((flags & ACC_INTERFACE) != 0) result.append("interface ");
  138. if ((flags & ACC_ABSTRACT) != 0) result.append("abstract ");
  139. return result.toString();
  140. }
  141. /** Check if a flag has the public bit set */
  142. public static boolean isPublic(int flags) {
  143. return (flags & ACC_PUBLIC) != 0;
  144. }
  145. /** Check if a flag has the private bit set */
  146. public static boolean isPrivate(int flags) {
  147. return (flags & ACC_PRIVATE) != 0;
  148. }
  149. /** Check if a flag has the protected bit set */
  150. public static boolean isProtected(int flags) {
  151. return (flags & ACC_PROTECTED) != 0;
  152. }
  153. /** Check if a flag has the final bit set */
  154. public static boolean isFinal(int flags) {
  155. return (flags & ACC_FINAL) != 0;
  156. }
  157. /** Check if a flag has the static bit set */
  158. public static boolean isStatic(int flags) {
  159. return (flags & ACC_STATIC) != 0;
  160. }
  161. /** Check if a flag has the native bit set */
  162. public static boolean isNative(int flags) {
  163. return (flags & ACC_NATIVE) != 0;
  164. }
  165. /** Check if a flag has the interface bit set */
  166. public static boolean isInterface(int flags) {
  167. return (flags & ACC_INTERFACE) != 0;
  168. }
  169. /** Check if a flag has the abstract bit set */
  170. public static boolean isAbstract(int flags) {
  171. return (flags & ACC_ABSTRACT) != 0;
  172. }
  173. /** Check if a flag has the synchronized bit set */
  174. public static boolean isSynchronized(int flags) {
  175. return (flags & ACC_SYNCHRONIZED) != 0;
  176. }
  177. /** Check if a flag has the volatile bit set */
  178. public static boolean isVolatile(int flags) {
  179. return (flags & ACC_VOLATILE) != 0;
  180. }
  181. /** Check if a flag has the transient bit set */
  182. public static boolean isTransient(int flags) {
  183. return (flags & ACC_TRANSIENT) != 0;
  184. }
  185. }