| Code with Finding: |
class ConstantPoolGen {
/**
* Initialize with given array of constants.
*
* @param cs array of given constants, new ones will be appended
*/
public ConstantPoolGen(Constant[] cs) {
StringBuilder sb = new StringBuilder(DEFAULT_BUFFER_SIZE);
size = Math.max(DEFAULT_BUFFER_SIZE, cs.length + 64);
constants = new Constant[size];
System.arraycopy(cs, 0, constants, 0, cs.length);
if (cs.length > 0) {
index = cs.length;
}
for (int i = 1; i < index; i++) {
Constant c = constants[i];
if (c instanceof ConstantString) {
ConstantString s = (ConstantString) c;
ConstantUtf8 u8 = (ConstantUtf8) constants[s.getStringIndex()];
String key = u8.getBytes();
if (!string_table.containsKey(key)) {
string_table.put(key, new Index(i));
}
} else if (c instanceof ConstantClass) {
ConstantClass s = (ConstantClass) c;
ConstantUtf8 u8 = (ConstantUtf8) constants[s.getNameIndex()];
String key = u8.getBytes();
if (!class_table.containsKey(key)) {
class_table.put(key, new Index(i));
}
} else if (c instanceof ConstantNameAndType) {
ConstantNameAndType n = (ConstantNameAndType) c;
ConstantUtf8 u8 = (ConstantUtf8) constants[n.getNameIndex()];
ConstantUtf8 u8_2 = (ConstantUtf8) constants[n.getSignatureIndex()];
sb.append(u8.getBytes());
sb.append(NAT_DELIM);
sb.append(u8_2.getBytes());
String key = sb.toString();
sb.delete(0, sb.length());
if (!n_a_t_table.containsKey(key)) {
n_a_t_table.put(key, new Index(i));
}
} else if (c instanceof ConstantUtf8) {
ConstantUtf8 u = (ConstantUtf8) c;
String key = u.getBytes();
if (!utf8_table.containsKey(key)) {
utf8_table.put(key, new Index(i));
}
} else if (c instanceof ConstantCP) {
ConstantCP m = (ConstantCP) c;
String class_name;
ConstantUtf8 u8;
if (c instanceof ConstantInvokeDynamic) {
class_name = Integer.toString(((ConstantInvokeDynamic) m).getBootstrapMethodAttrIndex());
// since name can't begin with digit, can use
// METHODREF_DELIM with out fear of duplicates.
} else {
ConstantClass clazz = (ConstantClass) constants[m.getClassIndex()];
u8 = (ConstantUtf8) constants[clazz.getNameIndex()];
class_name = u8.getBytes().replace('/', '.');
}
ConstantNameAndType n = (ConstantNameAndType) constants[m.getNameAndTypeIndex()];
u8 = (ConstantUtf8) constants[n.getNameIndex()];
String method_name = u8.getBytes();
u8 = (ConstantUtf8) constants[n.getSignatureIndex()];
String signature = u8.getBytes();
String delim = METHODREF_DELIM;
if (c instanceof ConstantInterfaceMethodref) {
delim = IMETHODREF_DELIM;
} else if (c instanceof ConstantFieldref) {
delim = FIELDREF_DELIM;
}
sb.append(class_name);
sb.append(delim);
sb.append(method_name);
sb.append(delim);
sb.append(signature);
String key = sb.toString();
sb.delete(0, sb.length());
if (!cp_table.containsKey(key)) {
cp_table.put(key, new Index(i));
}
} else if (c == null) { // entries may be null
// nothing to do
} else if (c instanceof ConstantInteger) {
// nothing to do
} else if (c instanceof ConstantLong) {
// nothing to do
} else if (c instanceof ConstantFloat) {
// nothing to do
} else if (c instanceof ConstantDouble) {
// nothing to do
} else if (c instanceof org.apache.commons.bcel6.classfile.ConstantMethodType) {
// TODO should this be handled somehow?
} else if (c instanceof org.apache.commons.bcel6.classfile.ConstantMethodHandle) {
// TODO should this be handled somehow?
} else {
assert false : "Unexpected constant type: " + c.getClass().getName();
}
}
}
}
|