use of org.apache.bcel.classfile.ConstantDouble in project jop by jop-devel.
the class ConstantPoolRebuilder method createNewConstantPool.
public ConstantPoolGen createNewConstantPool(ConstantPoolGen oldPool, Set<Integer> usedIndices) {
// We add all used entries to the new pool in the same order as in the old pool
// to avoid indices getting larger than before
Integer[] ids = new ArrayList<Integer>(usedIndices).toArray(new Integer[usedIndices.size()]);
Arrays.sort(ids);
// First thing we need is a map, since we need to relink the constantpool entries
idMap.clear();
// Note that index 0 is not valid, so skip it
List<Constant> constants = new ArrayList<Constant>(usedIndices.size() + 1);
constants.add(null);
int newPos = 1;
for (int id : ids) {
Constant c = oldPool.getConstant(id);
// we cannot use newPool.addConstant here, because this would add all referenced constants too,
// and we do not want that..
constants.add(c);
idMap.put(id, newPos++);
if (c instanceof ConstantLong || c instanceof ConstantDouble) {
// reserve an additional slot for those
constants.add(null);
newPos++;
}
}
// now we create new constants and map the references
Constant[] newConstants = new Constant[constants.size()];
for (int i = 1; i < constants.size(); i++) {
Constant c = constants.get(i);
if (c == null)
continue;
newConstants[i] = copyConstant(idMap, c);
}
newPool = new ConstantPoolGen(newConstants);
return newPool;
}
use of org.apache.bcel.classfile.ConstantDouble in project jop by jop-devel.
the class ConstantPoolRebuilder method copyConstant.
public static Constant copyConstant(Map<Integer, Integer> idMap, Constant c) {
if (c instanceof ConstantClass) {
int index = ((ConstantClass) c).getNameIndex();
return new ConstantClass(idMap.get(index));
} else if (c instanceof ConstantFieldref) {
int clsIdx = ((ConstantFieldref) c).getClassIndex();
int nameIdx = ((ConstantFieldref) c).getNameAndTypeIndex();
return new ConstantFieldref(idMap.get(clsIdx), idMap.get(nameIdx));
} else if (c instanceof ConstantMethodref) {
int clsIdx = ((ConstantMethodref) c).getClassIndex();
int nameIdx = ((ConstantMethodref) c).getNameAndTypeIndex();
return new ConstantMethodref(idMap.get(clsIdx), idMap.get(nameIdx));
} else if (c instanceof ConstantInterfaceMethodref) {
int clsIdx = ((ConstantInterfaceMethodref) c).getClassIndex();
int nameIdx = ((ConstantInterfaceMethodref) c).getNameAndTypeIndex();
return new ConstantInterfaceMethodref(idMap.get(clsIdx), idMap.get(nameIdx));
} else if (c instanceof ConstantString) {
int index = ((ConstantString) c).getStringIndex();
return new ConstantString(idMap.get(index));
} else if (c instanceof ConstantInteger) {
return new ConstantInteger((ConstantInteger) c);
} else if (c instanceof ConstantFloat) {
return new ConstantFloat((ConstantFloat) c);
} else if (c instanceof ConstantLong) {
return new ConstantLong((ConstantLong) c);
} else if (c instanceof ConstantDouble) {
return new ConstantDouble((ConstantDouble) c);
} else if (c instanceof ConstantNameAndType) {
int nameIdx = ((ConstantNameAndType) c).getNameIndex();
int sigIdx = ((ConstantNameAndType) c).getSignatureIndex();
return new ConstantNameAndType(idMap.get(nameIdx), idMap.get(sigIdx));
} else if (c instanceof ConstantUtf8) {
return new ConstantUtf8((ConstantUtf8) c);
}
throw new JavaClassFormatError("Unknown constant type " + c);
}
Aggregations