ConstantPool.java 8.4 KB

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