1 /**********************************************************************/
2 /* ConstantPool class for J2CPS */
4 /* (c) copyright QUT */
5 /**********************************************************************/
8 import java
.io
.DataInputStream
;
9 import java
.io
.IOException
;
11 /* The constant pool from the ClassFile */
13 public class ConstantPool
{
15 Object pool
[]; /* the constant pool */
17 /* Tags for constant pool entries */
18 public final static int CONSTANT_Utf8
= 1;
19 public static final int CONSTANT_Unicode
= 2;
20 public final static int CONSTANT_Integer
= 3;
21 public final static int CONSTANT_Float
= 4;
22 public final static int CONSTANT_Long
= 5;
23 public final static int CONSTANT_Double
= 6;
24 public final static int CONSTANT_Class
= 7;
25 public final static int CONSTANT_String
= 8;
26 public final static int CONSTANT_Fieldref
= 9;
27 public final static int CONSTANT_Methodref
= 10;
28 public final static int CONSTANT_InterfaceMethodref
= 11;
29 public final static int CONSTANT_NameAndType
= 12;
30 public final static int CONSTANT_Unknown
= 13;
33 public static final int ACC_PUBLIC
= 0x0001;
34 public static final int ACC_PRIVATE
= 0x0002;
35 public static final int ACC_PROTECTED
= 0x0004;
36 public static final int ACC_STATIC
= 0x0008;
37 public static final int ACC_FINAL
= 0x0010;
38 public static final int ACC_SYNCHRONIZED
= 0x0020;
39 public static final int ACC_VOLATILE
= 0x0040;
40 public static final int ACC_TRANSIENT
= 0x0080;
41 public static final int ACC_NATIVE
= 0x0100;
42 public static final int ACC_INTERFACE
= 0x0200;
43 public static final int ACC_ABSTRACT
= 0x0400;
45 public ConstantPool(DataInputStream stream
) throws IOException
{
46 /* read the number of entries in the constant pool */
47 int count
= stream
.readUnsignedShort();
48 /* read in the constant pool */
49 pool
= new Object
[count
];
50 for (int i
= 1; i
< count
; i
++) {
51 Object c
= ReadConstant(stream
);
53 /* note that Long and Double constant occupies two entries */
54 if (c
instanceof Long
|| c
instanceof Double
) { i
++; }
56 for (int i
= 1; i
< pool
.length
; i
++) {
57 if (pool
[i
] instanceof Reference
) {
58 ((Reference
)pool
[i
]).Resolve();
59 } else if (pool
[i
] instanceof ClassRef
) {
60 ((ClassRef
)pool
[i
]).Resolve();
65 public void EmptyConstantPool() {
66 for (int i
= 1; i
< pool
.length
; i
++) {
72 private Object
ReadConstant(DataInputStream stream
)
74 int tag
= stream
.readUnsignedByte();
77 return stream
.readUTF();
78 case CONSTANT_Integer
:
79 return new Integer(stream
.readInt());
81 return new Float(stream
.readFloat());
83 return new Long(stream
.readLong());
85 return new Double(stream
.readDouble());
87 return new ClassRef(this,stream
.readUnsignedShort());
89 return new StringRef(this,stream
.readUnsignedShort());
90 case CONSTANT_Fieldref
:
91 return new FieldRef(this,stream
.readUnsignedShort(),
92 stream
.readUnsignedShort());
93 case CONSTANT_Methodref
:
94 return new MethodRef(this,stream
.readUnsignedShort(),
95 stream
.readUnsignedShort());
96 case CONSTANT_InterfaceMethodref
:
97 return new InterfaceMethodRef(this,stream
.readUnsignedShort(),
98 stream
.readUnsignedShort());
99 case CONSTANT_NameAndType
:
100 return new NameAndType(this,stream
.readUnsignedShort(),
101 stream
.readUnsignedShort());
103 System
.out
.println("Unrecognized constant type: "+String
.valueOf(tag
));
108 public final Object
Get(int index
) {
112 public int GetNumEntries() {
116 /** Returns a String representing the Constant Pool */
117 public void PrintConstantPool() {
118 System
.out
.println(" CONSTANT POOL ENTRIES (" + pool
.length
+ ")");
119 for (int i
= 1; i
< pool
.length
; i
++) {
120 System
.out
.print(i
+ " ");
121 if (pool
[i
] instanceof String
) {
122 System
.out
.println("<String> " + pool
[i
]);
123 } else if (pool
[i
] instanceof Integer
) {
124 System
.out
.println("<Integer> " + pool
[i
].toString());
125 } else if (pool
[i
] instanceof Float
) {
126 System
.out
.println("<Float > " + pool
[i
].toString());
127 } else if (pool
[i
] instanceof Long
) {
128 System
.out
.println("<Long > " + pool
[i
].toString());
129 } else if (pool
[i
] instanceof Double
) {
130 System
.out
.println("<Double> " + pool
[i
].toString());
132 System
.out
.println(pool
[i
].toString());
134 if (pool
[i
] instanceof Long
|| pool
[i
] instanceof Double
) i
++;
136 System
.out
.println();
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();
156 /** Check if a flag has the public bit set */
157 public static boolean isPublic(int flags
) {
158 return (flags
& ACC_PUBLIC
) != 0;
161 /** Check if a flag has the private bit set */
162 public static boolean isPrivate(int flags
) {
163 return (flags
& ACC_PRIVATE
) != 0;
166 /** Check if a flag has the protected bit set */
167 public static boolean isProtected(int flags
) {
168 return (flags
& ACC_PROTECTED
) != 0;
171 /** Check if a flag has the final bit set */
172 public static boolean isFinal(int flags
) {
173 return (flags
& ACC_FINAL
) != 0;
176 /** Check if a flag has the static bit set */
177 public static boolean isStatic(int flags
) {
178 return (flags
& ACC_STATIC
) != 0;
181 /** Check if a flag has the native bit set */
182 public static boolean isNative(int flags
) {
183 return (flags
& ACC_NATIVE
) != 0;
186 /** Check if a flag has the interface bit set */
187 public static boolean isInterface(int flags
) {
188 return (flags
& ACC_INTERFACE
) != 0;
191 /** Check if a flag has the abstract bit set */
192 public static boolean isAbstract(int flags
) {
193 return (flags
& ACC_ABSTRACT
) != 0;
196 /** Check if a flag has the synchronized bit set */
197 public static boolean isSynchronized(int flags
) {
198 return (flags
& ACC_SYNCHRONIZED
) != 0;
201 /** Check if a flag has the volatile bit set */
202 public static boolean isVolatile(int flags
) {
203 return (flags
& ACC_VOLATILE
) != 0;
206 /** Check if a flag has the transient bit set */
207 public static boolean isTransient(int flags
) {
208 return (flags
& ACC_TRANSIENT
) != 0;