Search in sources :

Example 6 with Constant

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

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

the class ClassIntermediateVisitor method visitJavaClass.

@Override
public void visitJavaClass(JavaClass obj) {
    this.classBlock.setClassName(javaClass.getClassName());
    this.classBlock.setPackageName(obj.getPackageName());
    this.classBlock.setSuperClassName(obj.getSuperclassName());
    // process the pool.
    Constant[] pool = obj.getConstantPool().getConstantPool();
    for (Constant c : pool) {
        if (c == null)
            continue;
        c.accept(this);
    }
    Field[] fields = obj.getFields();
    for (int i = 0, j = fields.length; i < j; i++) {
        fields[i].accept(this);
    }
    // run through all of the methods
    Method[] methods = obj.getMethods();
    for (int i = 0, j = methods.length; i < j; i++) {
        methods[i].accept(this);
    }
}
Also used : Field(org.apache.bcel.classfile.Field) Constant(org.apache.bcel.classfile.Constant) Method(org.apache.bcel.classfile.Method)

Example 8 with Constant

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

the class FindUsedConstants method find.

private void find(Method method) {
    MethodGen mg = new MethodGen(method, clazz.getClassName(), cpool);
    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);
        int len = 1;
        switch(co.getTag()) {
            case Constants.CONSTANT_Long:
            case Constants.CONSTANT_Double:
                len = 2;
                break;
        }
        // we don't need the field references in the cpool anymore
        if (co.getTag() != Constants.CONSTANT_Fieldref) {
            getCli().addUsedConst(idx, len);
        }
    // also modify the index!
    //			Constant cnst = cpool.getConstant(ii.getIndex());
    //			int newIndex = addConstant(cnst);
    //System.out.println(ii+" -> "+newIndex);
    //			ii.setIndex(newIndex);			
    }
    il.dispose();
    CodeExceptionGen[] et = mg.getExceptionHandlers();
    for (int i = 0; i < et.length; i++) {
        ObjectType ctype = et[i].getCatchType();
        if (ctype != null) {
            getCli().addUsedConst(cpool.lookupClass(ctype.getClassName()), 1);
        }
    }
}
Also used : InstructionList(org.apache.bcel.generic.InstructionList) Constant(org.apache.bcel.classfile.Constant) InstructionFinder(org.apache.bcel.util.InstructionFinder) MethodGen(org.apache.bcel.generic.MethodGen) InstructionHandle(org.apache.bcel.generic.InstructionHandle) CPInstruction(org.apache.bcel.generic.CPInstruction) ObjectType(org.apache.bcel.generic.ObjectType) Iterator(java.util.Iterator) CodeExceptionGen(org.apache.bcel.generic.CodeExceptionGen)

Example 9 with Constant

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

the class ClassInfo method setConstantInfo.

/**
     * Replace a constant with a new constant value in the constant pool.
     * Be aware that this does not check for duplicate entries, and may create additional
     * new entries in the constant pool.
     *
     * @see #addConstantInfo(ConstantInfo)
     * @param i the index of the constant to replace.
     * @param constant the new value.
     */
public void setConstantInfo(int i, ConstantInfo constant) {
    Constant c = constant.createConstant(cpg);
    cpg.setConstant(i, c);
}
Also used : Constant(org.apache.bcel.classfile.Constant)

Example 10 with Constant

use of org.apache.bcel.classfile.Constant in project OpenGrok by OpenGrok.

the class JavaClassAnalyzer method constantToString.

public String constantToString(Constant c, ConstantPool cp, int[] v) throws ClassFormatException {
    String str;
    int i, j;
    byte tag = c.getTag();
    switch(tag) {
        case org.apache.bcel.Const.CONSTANT_Class:
            i = ((ConstantClass) c).getNameIndex();
            v[i] = 1;
            Constant con = cp.getConstant(i, org.apache.bcel.Const.CONSTANT_Utf8);
            str = Utility.compactClassName(((ConstantUtf8) con).getBytes(), false);
            break;
        case org.apache.bcel.Const.CONSTANT_String:
            i = ((ConstantString) c).getStringIndex();
            v[i] = 1;
            Constant con2 = cp.getConstant(i, org.apache.bcel.Const.CONSTANT_Utf8);
            str = ((ConstantUtf8) con2).getBytes();
            break;
        case org.apache.bcel.Const.CONSTANT_Utf8:
            str = ((ConstantUtf8) c).toString();
            break;
        case org.apache.bcel.Const.CONSTANT_Double:
            str = ((ConstantDouble) c).toString();
            break;
        case org.apache.bcel.Const.CONSTANT_Float:
            str = ((ConstantFloat) c).toString();
            break;
        case org.apache.bcel.Const.CONSTANT_Long:
            str = ((ConstantLong) c).toString();
            break;
        case org.apache.bcel.Const.CONSTANT_Integer:
            str = ((ConstantInteger) c).toString();
            break;
        case org.apache.bcel.Const.CONSTANT_NameAndType:
            i = ((ConstantNameAndType) c).getNameIndex();
            v[i] = 1;
            j = ((ConstantNameAndType) c).getSignatureIndex();
            v[j] = 1;
            String sig = constantToString(cp.getConstant(j), cp, v);
            if (sig.charAt(0) == '(') {
                str = Utility.methodSignatureToString(sig, constantToString(cp.getConstant(i), cp, v), " ");
            } else {
                str = Utility.signatureToString(sig) + ' ' + constantToString(cp.getConstant(i), cp, v);
            }
            break;
        case org.apache.bcel.Const.CONSTANT_InterfaceMethodref:
        case org.apache.bcel.Const.CONSTANT_Methodref:
        case org.apache.bcel.Const.CONSTANT_Fieldref:
            i = ((ConstantCP) c).getClassIndex();
            v[i] = 1;
            j = ((ConstantCP) c).getNameAndTypeIndex();
            v[j] = 1;
            str = (constantToString(cp.getConstant(i), cp, v) + ' ' + constantToString(cp.getConstant(j), cp, v));
            break;
        default:
            // Never reached
            throw new ClassFormatException("Unknown constant type " + tag);
    }
    return str;
}
Also used : Constant(org.apache.bcel.classfile.Constant) ConstantString(org.apache.bcel.classfile.ConstantString) ConstantUtf8(org.apache.bcel.classfile.ConstantUtf8) ClassFormatException(org.apache.bcel.classfile.ClassFormatException)

Aggregations

Constant (org.apache.bcel.classfile.Constant)10 ConstantPoolGen (org.apache.bcel.generic.ConstantPoolGen)4 Iterator (java.util.Iterator)3 CPInstruction (org.apache.bcel.generic.CPInstruction)3 InstructionHandle (org.apache.bcel.generic.InstructionHandle)3 InstructionList (org.apache.bcel.generic.InstructionList)3 InstructionFinder (org.apache.bcel.util.InstructionFinder)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 ConstantClass (org.apache.bcel.classfile.ConstantClass)2 ConstantNameAndType (org.apache.bcel.classfile.ConstantNameAndType)2 ConstantPool (org.apache.bcel.classfile.ConstantPool)2 ConstantString (org.apache.bcel.classfile.ConstantString)2 Method (org.apache.bcel.classfile.Method)2 MethodGen (org.apache.bcel.generic.MethodGen)2 ClassInfo (com.jopdesign.common.ClassInfo)1 MethodInfo (com.jopdesign.common.MethodInfo)1 JavaClassFormatError (com.jopdesign.common.misc.JavaClassFormatError)1 ClassRef (com.jopdesign.common.type.ClassRef)1