Search in sources :

Example 1 with ConstantFieldref

use of org.apache.bcel.classfile.ConstantFieldref in project jop by jop-devel.

the class ReplaceNativeAndCPIdx method getFieldOffset.

/**
 * Get field offset: relative offset for object fields, absolute
 * addresses for static fields.
 *
 * TODO: remove the dead code in constant pool replacement
 * TODO: remove not used constants
 * @param cp
 * @param index
 * @return
 */
private int getFieldOffset(ConstantPool cp, int index) {
    // from ClassInfo.resolveCPool
    Constant co = cp.getConstant(index);
    int fidx = ((ConstantFieldref) co).getClassIndex();
    ConstantClass fcl = (ConstantClass) cp.getConstant(fidx);
    String fclname = fcl.getBytes(cp).replace('/', '.');
    // got the class name
    int sigidx = ((ConstantFieldref) co).getNameAndTypeIndex();
    ConstantNameAndType signt = (ConstantNameAndType) cp.getConstant(sigidx);
    String sigstr = signt.getName(cp) + signt.getSignature(cp);
    JopClassInfo clinf = (JopClassInfo) ai.cliMap.get(fclname);
    int j;
    boolean found = false;
    while (!found) {
        for (j = 0; j < clinf.clft.len; ++j) {
            if (clinf.clft.key[j].equals(sigstr)) {
                found = true;
                return clinf.clft.idx[j];
            }
        }
        if (!found) {
            clinf = (JopClassInfo) clinf.superClass;
            if (clinf == null) {
                System.out.println("Error: field " + fclname + "." + sigstr + " not found!");
                break;
            }
        }
    }
    System.out.println("Error in getFieldOffset()");
    System.exit(-1);
    return 0;
}
Also used : Constant(org.apache.bcel.classfile.Constant) ConstantFieldref(org.apache.bcel.classfile.ConstantFieldref) ConstantClass(org.apache.bcel.classfile.ConstantClass) ConstantNameAndType(org.apache.bcel.classfile.ConstantNameAndType)

Example 2 with ConstantFieldref

use of org.apache.bcel.classfile.ConstantFieldref 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)

Example 3 with ConstantFieldref

use of org.apache.bcel.classfile.ConstantFieldref in project jop by jop-devel.

the class ConstantInfo method createFromConstant.

/**
 * Create a new constantInfo from a BCEL constant.
 * If the constantpool contains invalid data, a {@link JavaClassFormatError} is thrown.
 *
 * @param cp the constantpool used to resolve the index references.
 * @param constant the BCEL constant to convert
 * @return a new ConstantInfo containing the constant value.
 */
public static ConstantInfo createFromConstant(ConstantPool cp, Constant constant) {
    MemberID sig;
    MethodRef methodRef;
    ConstantNameAndType nRef;
    AppInfo appInfo = AppInfo.getSingleton();
    byte tag = constant.getTag();
    switch(tag) {
        case Constants.CONSTANT_Class:
            ClassRef classRef = appInfo.getClassRef(((ConstantClass) constant).getBytes(cp).replace('/', '.'));
            return new ConstantClassInfo(classRef);
        case Constants.CONSTANT_Fieldref:
            ConstantFieldref fRef = (ConstantFieldref) constant;
            nRef = (ConstantNameAndType) cp.getConstant(fRef.getNameAndTypeIndex());
            sig = new MemberID(fRef.getClass(cp), nRef.getName(cp), nRef.getSignature(cp));
            FieldRef fieldRef = appInfo.getFieldRef(sig);
            return new ConstantFieldInfo(fieldRef);
        case Constants.CONSTANT_Methodref:
            ConstantMethodref mRef = (ConstantMethodref) constant;
            nRef = (ConstantNameAndType) cp.getConstant(mRef.getNameAndTypeIndex());
            sig = new MemberID(mRef.getClass(cp), nRef.getName(cp), nRef.getSignature(cp));
            methodRef = appInfo.getMethodRef(sig, false);
            return new ConstantMethodInfo(methodRef);
        case Constants.CONSTANT_InterfaceMethodref:
            ConstantInterfaceMethodref imRef = (ConstantInterfaceMethodref) constant;
            nRef = (ConstantNameAndType) cp.getConstant(imRef.getNameAndTypeIndex());
            sig = new MemberID(imRef.getClass(cp), nRef.getName(cp), nRef.getSignature(cp));
            methodRef = appInfo.getMethodRef(sig, true);
            return new ConstantMethodInfo(methodRef);
        case Constants.CONSTANT_String:
            return new ConstantStringInfo(((ConstantString) constant).getBytes(cp), false);
        case Constants.CONSTANT_Integer:
            return new ConstantIntegerInfo(((ConstantInteger) constant).getBytes());
        case Constants.CONSTANT_Float:
            return new ConstantFloatInfo(((ConstantFloat) constant).getBytes());
        case Constants.CONSTANT_Long:
            return new ConstantLongInfo(((ConstantLong) constant).getBytes());
        case Constants.CONSTANT_Double:
            return new ConstantDoubleInfo(((ConstantDouble) constant).getBytes());
        case Constants.CONSTANT_NameAndType:
            String name = ((ConstantNameAndType) constant).getName(cp);
            String signature = ((ConstantNameAndType) constant).getSignature(cp);
            return new ConstantNameAndTypeInfo(new MemberID(name, signature));
        case Constants.CONSTANT_Utf8:
            return new ConstantStringInfo(((ConstantUtf8) constant).getBytes(), true);
        default:
            throw new JavaClassFormatError("Invalid byte tag in constant pool: " + tag);
    }
}
Also used : ConstantString(org.apache.bcel.classfile.ConstantString) ConstantNameAndType(org.apache.bcel.classfile.ConstantNameAndType) AppInfo(com.jopdesign.common.AppInfo) ConstantMethodref(org.apache.bcel.classfile.ConstantMethodref) ConstantFieldref(org.apache.bcel.classfile.ConstantFieldref) JavaClassFormatError(com.jopdesign.common.misc.JavaClassFormatError) ConstantInterfaceMethodref(org.apache.bcel.classfile.ConstantInterfaceMethodref) ConstantClass(org.apache.bcel.classfile.ConstantClass)

Example 4 with ConstantFieldref

use of org.apache.bcel.classfile.ConstantFieldref in project fb-contrib by mebigfatguy.

the class SyncCollectionIterators method sawOpcodeAfterNothing.

private void sawOpcodeAfterNothing(int seen) {
    if ((seen == Const.INVOKESTATIC) && "java/util/Collections".equals(getClassConstantOperand())) {
        if (synchCollectionNames.contains(getNameConstantOperand())) {
            state = State.SEEN_SYNC;
        }
    } else if (OpcodeUtils.isALoad(seen)) {
        int reg = RegisterUtils.getALoadReg(this, seen);
        if (localCollections.get(reg)) {
            collectionInfo = Integer.valueOf(reg);
            state = State.SEEN_LOAD;
        }
    } else if (seen == Const.GETFIELD) {
        ConstantFieldref ref = (ConstantFieldref) getConstantRefOperand();
        ConstantNameAndType nandt = (ConstantNameAndType) getConstantPool().getConstant(ref.getNameAndTypeIndex());
        String fieldName = nandt.getName(getConstantPool());
        if (memberCollections.contains(fieldName)) {
            collectionInfo = fieldName;
            state = State.SEEN_LOAD;
        }
    }
}
Also used : ConstantFieldref(org.apache.bcel.classfile.ConstantFieldref) ConstantNameAndType(org.apache.bcel.classfile.ConstantNameAndType)

Aggregations

ConstantFieldref (org.apache.bcel.classfile.ConstantFieldref)4 ConstantNameAndType (org.apache.bcel.classfile.ConstantNameAndType)4 ConstantClass (org.apache.bcel.classfile.ConstantClass)3 JavaClassFormatError (com.jopdesign.common.misc.JavaClassFormatError)2 ConstantInterfaceMethodref (org.apache.bcel.classfile.ConstantInterfaceMethodref)2 ConstantMethodref (org.apache.bcel.classfile.ConstantMethodref)2 ConstantString (org.apache.bcel.classfile.ConstantString)2 AppInfo (com.jopdesign.common.AppInfo)1 Constant (org.apache.bcel.classfile.Constant)1 ConstantDouble (org.apache.bcel.classfile.ConstantDouble)1 ConstantFloat (org.apache.bcel.classfile.ConstantFloat)1 ConstantInteger (org.apache.bcel.classfile.ConstantInteger)1 ConstantLong (org.apache.bcel.classfile.ConstantLong)1 ConstantUtf8 (org.apache.bcel.classfile.ConstantUtf8)1