Search in sources :

Example 1 with ConstantLong

use of org.apache.bcel.classfile.ConstantLong 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;
}
Also used : ConstantInteger(org.apache.bcel.classfile.ConstantInteger) ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) ConstantLong(org.apache.bcel.classfile.ConstantLong) Constant(org.apache.bcel.classfile.Constant) ConstantDouble(org.apache.bcel.classfile.ConstantDouble) ArrayList(java.util.ArrayList)

Example 2 with ConstantLong

use of org.apache.bcel.classfile.ConstantLong 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);
}
Also used : ConstantString(org.apache.bcel.classfile.ConstantString) ConstantDouble(org.apache.bcel.classfile.ConstantDouble) ConstantUtf8(org.apache.bcel.classfile.ConstantUtf8) ConstantFloat(org.apache.bcel.classfile.ConstantFloat) ConstantNameAndType(org.apache.bcel.classfile.ConstantNameAndType) ConstantMethodref(org.apache.bcel.classfile.ConstantMethodref) ConstantLong(org.apache.bcel.classfile.ConstantLong) ConstantFieldref(org.apache.bcel.classfile.ConstantFieldref) JavaClassFormatError(com.jopdesign.common.misc.JavaClassFormatError) ConstantInterfaceMethodref(org.apache.bcel.classfile.ConstantInterfaceMethodref) ConstantClass(org.apache.bcel.classfile.ConstantClass) ConstantInteger(org.apache.bcel.classfile.ConstantInteger)

Aggregations

ConstantDouble (org.apache.bcel.classfile.ConstantDouble)2 ConstantInteger (org.apache.bcel.classfile.ConstantInteger)2 ConstantLong (org.apache.bcel.classfile.ConstantLong)2 JavaClassFormatError (com.jopdesign.common.misc.JavaClassFormatError)1 ArrayList (java.util.ArrayList)1 Constant (org.apache.bcel.classfile.Constant)1 ConstantClass (org.apache.bcel.classfile.ConstantClass)1 ConstantFieldref (org.apache.bcel.classfile.ConstantFieldref)1 ConstantFloat (org.apache.bcel.classfile.ConstantFloat)1 ConstantInterfaceMethodref (org.apache.bcel.classfile.ConstantInterfaceMethodref)1 ConstantMethodref (org.apache.bcel.classfile.ConstantMethodref)1 ConstantNameAndType (org.apache.bcel.classfile.ConstantNameAndType)1 ConstantString (org.apache.bcel.classfile.ConstantString)1 ConstantUtf8 (org.apache.bcel.classfile.ConstantUtf8)1 ConstantPoolGen (org.apache.bcel.generic.ConstantPoolGen)1