Search in sources :

Example 1 with ConstantClass

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

the class OldClinitOrder method findDependencies.

private Set findDependencies(OldClassInfo cli, OldMethodInfo mi, boolean inRec) {
    //		System.out.println("find dep. in "+cli.clazz.getClassName()+":"+mi.getMethod().getName());
    Method method = mi.getMethod();
    Set depends = new HashSet();
    if (method.isNative() || method.isAbstract()) {
        // subclasses???? :-(
        return depends;
    }
    ConstantPool cpool = cli.clazz.getConstantPool();
    ConstantPoolGen cpoolgen = new ConstantPoolGen(cpool);
    MethodGen mg = new MethodGen(method, cli.clazz.getClassName(), cpoolgen);
    InstructionList il = mg.getInstructionList();
    InstructionFinder f = new InstructionFinder(il);
    // find instructions that access the constant pool
    // collect all indices to constants in ClassInfo
    String cpInstr = "CPInstruction";
    for (Iterator it = f.search(cpInstr); it.hasNext(); ) {
        InstructionHandle[] match = (InstructionHandle[]) it.next();
        InstructionHandle first = match[0];
        CPInstruction ii = (CPInstruction) first.getInstruction();
        int idx = ii.getIndex();
        Constant co = cpool.getConstant(idx);
        ConstantClass cocl = null;
        Set addDepends = null;
        String clname;
        OldClassInfo clinfo;
        OldMethodInfo minfo;
        switch(co.getTag()) {
            case Constants.CONSTANT_Class:
                cocl = (ConstantClass) co;
                clname = cocl.getBytes(cpool).replace('/', '.');
                clinfo = (OldClassInfo) ai.cliMap.get(clname);
                if (clinfo != null) {
                    minfo = clinfo.getMethodInfo("<init>()V");
                    if (minfo != null) {
                        addDepends = findDependencies(clinfo, minfo, true);
                    }
                }
                break;
            case Constants.CONSTANT_InterfaceMethodref:
                cocl = (ConstantClass) cpool.getConstant(((ConstantInterfaceMethodref) co).getClassIndex());
                break;
            case Constants.CONSTANT_Methodref:
                cocl = (ConstantClass) cpool.getConstant(((ConstantMethodref) co).getClassIndex());
                clname = cocl.getBytes(cpool).replace('/', '.');
                clinfo = (OldClassInfo) ai.cliMap.get(clname);
                int sigidx = ((ConstantMethodref) co).getNameAndTypeIndex();
                ConstantNameAndType signt = (ConstantNameAndType) cpool.getConstant(sigidx);
                String sigstr = signt.getName(cpool) + signt.getSignature(cpool);
                if (clinfo != null) {
                    minfo = clinfo.getMethodInfo(sigstr);
                    if (minfo != null) {
                        addDepends = findDependencies(clinfo, minfo, true);
                    }
                }
                break;
            case Constants.CONSTANT_Fieldref:
                cocl = (ConstantClass) cpool.getConstant(((ConstantFieldref) co).getClassIndex());
                break;
        }
        if (cocl != null) {
            clname = cocl.getBytes(cpool).replace('/', '.');
            OldClassInfo clinf = (OldClassInfo) ai.cliMap.get(clname);
            if (clinf != null) {
                if (clinf.getMethodInfo(OldAppInfo.clinitSig) != null) {
                    // don't add myself as dependency
                    if (clinf != cli) {
                        depends.add(clinf);
                    }
                }
            }
        }
        if (addDepends != null) {
            Iterator itAddDep = addDepends.iterator();
            while (itAddDep.hasNext()) {
                OldClassInfo addCli = (OldClassInfo) itAddDep.next();
                if (addCli == cli) {
                    throw new Error("cyclic indirect <clinit> dependency");
                }
                depends.add(addCli);
            }
        }
    }
    il.dispose();
    return depends;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) InstructionList(org.apache.bcel.generic.InstructionList) Constant(org.apache.bcel.classfile.Constant) Method(org.apache.bcel.classfile.Method) InstructionFinder(org.apache.bcel.util.InstructionFinder) MethodGen(org.apache.bcel.generic.MethodGen) InstructionHandle(org.apache.bcel.generic.InstructionHandle) ConstantNameAndType(org.apache.bcel.classfile.ConstantNameAndType) ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) CPInstruction(org.apache.bcel.generic.CPInstruction) ConstantMethodref(org.apache.bcel.classfile.ConstantMethodref) ConstantPool(org.apache.bcel.classfile.ConstantPool) Iterator(java.util.Iterator) ConstantClass(org.apache.bcel.classfile.ConstantClass) HashSet(java.util.HashSet)

Example 2 with ConstantClass

use of org.apache.bcel.classfile.ConstantClass 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 3 with ConstantClass

use of org.apache.bcel.classfile.ConstantClass in project candle-decompiler by bradsdavis.

the class MethodIntermediateVisitor method visitLDC.

public void visitLDC(LDC instruction) {
    //load from constant pool.
    LOG.debug("Loading from constant pool" + instruction.getClass());
    MethodGen mg = context.getMethodGen();
    ConstantPoolGen cpg = mg.getConstantPool();
    StringBuilder resolvedValue = new StringBuilder();
    Type type = instruction.getType(cpg);
    if (Type.STRING == type) {
        resolvedValue.append("\"");
    }
    Object instructionValue = instruction.getValue(cpg);
    if (instructionValue instanceof ConstantClass) {
        String clzName = getClassName((ConstantClass) instructionValue, cpg.getConstantPool());
        resolvedValue.append(clzName);
    } else {
        resolvedValue.append(instructionValue.toString());
    }
    if (Type.STRING == type) {
        resolvedValue.append("\"");
    }
    Resolved resolved = new Resolved(context.getCurrentInstruction(), type, resolvedValue.toString());
    context.getExpressions().push(resolved);
}
Also used : OperationType(org.candle.decompiler.intermediate.expression.OperationType) ArithmeticType(org.candle.decompiler.intermediate.expression.ArithmeticType) Resolved(org.candle.decompiler.intermediate.expression.Resolved) ConstantClass(org.apache.bcel.classfile.ConstantClass)

Example 4 with ConstantClass

use of org.apache.bcel.classfile.ConstantClass in project candle-decompiler by bradsdavis.

the class MethodIntermediateVisitor method visitLDC2_W.

public void visitLDC2_W(LDC2_W instruction) {
    //load from constant pool.
    LOG.debug("Loading from constant pool" + instruction.getClass());
    MethodGen mg = context.getMethodGen();
    ConstantPoolGen cpg = mg.getConstantPool();
    Type type = instruction.getType(cpg);
    Object instructionValue = instruction.getValue(cpg);
    if (Type.STRING == type) {
        Expression resolved = new StringLiteral(context.getCurrentInstruction(), instructionValue.toString());
        context.getExpressions().push(resolved);
        return;
    } else {
    }
    StringBuilder resolvedValue = new StringBuilder();
    if (instructionValue instanceof ConstantClass) {
        String clzName = getClassName((ConstantClass) instructionValue, cpg.getConstantPool());
        resolvedValue.append(clzName);
    } else {
        resolvedValue.append(instructionValue.toString());
    }
    Expression resolved = new Resolved(context.getCurrentInstruction(), type, resolvedValue.toString());
    context.getExpressions().push(resolved);
}
Also used : OperationType(org.candle.decompiler.intermediate.expression.OperationType) ArithmeticType(org.candle.decompiler.intermediate.expression.ArithmeticType) StringLiteral(org.candle.decompiler.intermediate.expression.StringLiteral) TypedExpression(org.candle.decompiler.intermediate.expression.TypedExpression) Expression(org.candle.decompiler.intermediate.expression.Expression) Resolved(org.candle.decompiler.intermediate.expression.Resolved) ConstantClass(org.apache.bcel.classfile.ConstantClass)

Example 5 with ConstantClass

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

Aggregations

ConstantClass (org.apache.bcel.classfile.ConstantClass)6 ConstantNameAndType (org.apache.bcel.classfile.ConstantNameAndType)4 ConstantFieldref (org.apache.bcel.classfile.ConstantFieldref)3 ConstantMethodref (org.apache.bcel.classfile.ConstantMethodref)3 JavaClassFormatError (com.jopdesign.common.misc.JavaClassFormatError)2 Constant (org.apache.bcel.classfile.Constant)2 ConstantInterfaceMethodref (org.apache.bcel.classfile.ConstantInterfaceMethodref)2 ConstantString (org.apache.bcel.classfile.ConstantString)2 ArithmeticType (org.candle.decompiler.intermediate.expression.ArithmeticType)2 OperationType (org.candle.decompiler.intermediate.expression.OperationType)2 Resolved (org.candle.decompiler.intermediate.expression.Resolved)2 AppInfo (com.jopdesign.common.AppInfo)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Set (java.util.Set)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 ConstantPool (org.apache.bcel.classfile.ConstantPool)1