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