in src/main/java/org/apache/bcel/generic/ConstantPoolGen.java [104:214]
public ConstantPoolGen(final Constant[] cs) {
final StringBuilder sb = new StringBuilder(DEFAULT_BUFFER_SIZE);
size = Math.min(Math.max(DEFAULT_BUFFER_SIZE, cs.length + 64), Const.MAX_CP_ENTRIES + 1);
constants = Arrays.copyOf(cs, size);
if (cs.length > 0) {
index = cs.length;
}
for (int i = 1; i < index; i++) {
final Constant c = constants[i];
if (c instanceof ConstantString) {
final ConstantString s = (ConstantString) c;
final ConstantUtf8 u8 = (ConstantUtf8) constants[s.getStringIndex()];
final String key = u8.getBytes();
if (!stringTable.containsKey(key)) {
stringTable.put(key, Integer.valueOf(i));
}
} else if (c instanceof ConstantClass) {
final ConstantClass s = (ConstantClass) c;
final ConstantUtf8 u8 = (ConstantUtf8) constants[s.getNameIndex()];
final String key = u8.getBytes();
if (!classTable.containsKey(key)) {
classTable.put(key, Integer.valueOf(i));
}
} else if (c instanceof ConstantNameAndType) {
final ConstantNameAndType n = (ConstantNameAndType) c;
final ConstantUtf8 u8NameIdx = (ConstantUtf8) constants[n.getNameIndex()];
final ConstantUtf8 u8SigIdx = (ConstantUtf8) constants[n.getSignatureIndex()];
sb.append(u8NameIdx.getBytes());
sb.append(NAT_DELIM);
sb.append(u8SigIdx.getBytes());
final String key = sb.toString();
sb.delete(0, sb.length());
if (!natTable.containsKey(key)) {
natTable.put(key, Integer.valueOf(i));
}
} else if (c instanceof ConstantUtf8) {
final ConstantUtf8 u = (ConstantUtf8) c;
final String key = u.getBytes();
if (!utf8Table.containsKey(key)) {
utf8Table.put(key, Integer.valueOf(i));
}
} else if (c instanceof ConstantCP) {
final ConstantCP m = (ConstantCP) c;
String className;
ConstantUtf8 u8;
if (c instanceof ConstantInvokeDynamic) {
className = Integer.toString(((ConstantInvokeDynamic) m).getBootstrapMethodAttrIndex());
} else if (c instanceof ConstantDynamic) {
className = Integer.toString(((ConstantDynamic) m).getBootstrapMethodAttrIndex());
} else {
final ConstantClass clazz = (ConstantClass) constants[m.getClassIndex()];
u8 = (ConstantUtf8) constants[clazz.getNameIndex()];
className = Utility.pathToPackage(u8.getBytes());
}
final ConstantNameAndType n = (ConstantNameAndType) constants[m.getNameAndTypeIndex()];
u8 = (ConstantUtf8) constants[n.getNameIndex()];
final String methodName = u8.getBytes();
u8 = (ConstantUtf8) constants[n.getSignatureIndex()];
final String signature = u8.getBytes();
// Since name cannot begin with digit, we can use METHODREF_DELIM without fear of duplicates
String delim = METHODREF_DELIM;
if (c instanceof ConstantInterfaceMethodref) {
delim = IMETHODREF_DELIM;
} else if (c instanceof ConstantFieldref) {
delim = FIELDREF_DELIM;
}
sb.append(className);
sb.append(delim);
sb.append(methodName);
sb.append(delim);
sb.append(signature);
final String key = sb.toString();
sb.delete(0, sb.length());
if (!cpTable.containsKey(key)) {
cpTable.put(key, Integer.valueOf(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.bcel.classfile.ConstantMethodType) {
// // TODO should this be handled somehow?
// } else if (c instanceof org.apache.bcel.classfile.ConstantMethodHandle) {
// // TODO should this be handled somehow?
// } else if (c instanceof org.apache.bcel.classfile.ConstantModule) {
// // TODO should this be handled somehow?
// } else if (c instanceof org.apache.bcel.classfile.ConstantPackage) {
// // TODO should this be handled somehow?
// } else {
// // Not helpful, should throw an exception.
// assert false : "Unexpected constant type: " + c.getClass().getName();
// }
}
}