Search in sources :

Example 11 with Constant

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

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

the class DescendingClassTraverser method visitConstantPool.

// /////////////////////////////////////////////////////////////////
// Other methods to visit only parts of a class
// /////////////////////////////////////////////////////////////////
public void visitConstantPool(ClassInfo classInfo) {
    ConstantPoolGen cpg = classInfo.getConstantPoolGen();
    if (visitor.visitConstantPoolGen(classInfo, cpg)) {
        bcelVisitor.setClassInfo(classInfo);
        for (int i = 1; i < cpg.getSize(); i++) {
            Constant c = cpg.getConstant(i);
            // Some entries might be null (continuation of previous entry)
            if (c == null)
                continue;
            c.accept(bcelVisitor);
        }
        visitor.finishConstantPoolGen(classInfo, cpg);
    }
}
Also used : ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) Constant(org.apache.bcel.classfile.Constant)

Example 13 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 14 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, k;
    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;
        case org.apache.bcel.Const.CONSTANT_MethodType:
            i = ((ConstantMethodType) c).getDescriptorIndex();
            v[i] = 1;
            str = constantToString(cp.getConstant(i), cp, v);
            break;
        // }
        case org.apache.bcel.Const.CONSTANT_MethodHandle:
            // TODO fix implementation as per below to have proper lookup table/constants
            // i = ((ConstantMethodHandle) c).getReferenceKind();
            // v[i] = 1;
            // j = ((ConstantMethodHandle) c).getReferenceIndex();
            // v[j] = 1;
            // str = (constantToString(cp.getConstant(i), cp, v) + ' ' +
            // constantToString(cp.getConstant(j), cp, v));
            str = "";
            break;
        // }
        case org.apache.bcel.Const.CONSTANT_InvokeDynamic:
            // TODO fix implementation as per below and add bootstrap method tables first
            // i = ((ConstantInvokeDynamic) c).getClassIndex();
            // v[i] = 1;
            // j = ((ConstantInvokeDynamic) c).getNameAndTypeIndex();
            // v[j] = 1;
            // k = ((ConstantInvokeDynamic) c).getBootstrapMethodAttrIndex();
            // v[k] = 1;
            // str = (constantToString(cp.getConstant(i), cp, v) + ' ' +
            // constantToString(cp.getConstant(j), cp, v) + ' ' +
            // constantToString(cp.getConstant(k), cp, v));
            str = "";
            break;
        case org.apache.bcel.Const.CONSTANT_Package:
            i = ((ConstantPackage) c).getNameIndex();
            v[i] = 1;
            str = constantToString(cp.getConstant(i), cp, v);
            break;
        case org.apache.bcel.Const.CONSTANT_Module:
            i = ((ConstantModule) c).getNameIndex();
            v[i] = 1;
            str = constantToString(cp.getConstant(i), 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)

Example 15 with Constant

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

the class JavaClassAnalyzer method getContent.

// TODO this class needs to be thread safe to avoid bug 13364, which was fixed by just updating bcel to 5.2
private void getContent(Writer out, Writer fout, JavaClass c, List<String> defs, List<String> refs, List<String> full) throws IOException {
    String t;
    ConstantPool cp = c.getConstantPool();
    int[] v = new int[cp.getLength() + 1];
    out.write(linkPath(t = c.getSourceFileName()));
    defs.add(t);
    refs.add(t);
    fout.write(t);
    out.write(EOL);
    fout.write(EOL);
    out.write(PACKAGE);
    fout.write(PACKAGE);
    out.write(linkDef(t = c.getPackageName()));
    defs.add(t);
    refs.add(t);
    fout.write(t);
    out.write(EOL);
    fout.write(EOL);
    String aflg;
    out.write(aflg = Utility.accessToString(c.getAccessFlags(), true));
    if (aflg != null) {
        out.write(SPACE);
        fout.write(aflg);
        fout.write(SPACE);
    }
    v[c.getClassNameIndex()] = 1;
    out.write(tagDef(t = c.getClassName()));
    defs.add(t);
    refs.add(t);
    fout.write(t);
    out.write(EXTENDS);
    fout.write(EXTENDS);
    v[c.getSuperclassNameIndex()] = 1;
    out.write(linkDef(t = c.getSuperclassName()));
    refs.add(t);
    fout.write(t);
    for (int i : c.getInterfaceIndices()) {
        v[i] = 1;
    }
    String[] ins = c.getInterfaceNames();
    if (ins != null && ins.length > 0) {
        out.write(IMPLEMENTS);
        fout.write(IMPLEMENTS);
        for (String in : ins) {
            out.write(linkDef(t = in));
            refs.add(t);
            fout.write(t);
            out.write(SPACE);
            fout.write(SPACE);
        }
    }
    out.write(LCBREOL);
    fout.write(LCBREOL);
    for (Attribute a : c.getAttributes()) {
        if (a.getTag() == org.apache.bcel.Const.ATTR_CODE) {
            for (Attribute ca : ((Code) a).getAttributes()) {
                if (ca.getTag() == org.apache.bcel.Const.ATTR_LOCAL_VARIABLE_TABLE) {
                    for (LocalVariable l : ((LocalVariableTable) ca).getLocalVariableTable()) {
                        printLocal(out, fout, l, v, defs, refs);
                    }
                }
            }
        } else if (a.getTag() == org.apache.bcel.Const.ATTR_BOOTSTRAP_METHODS) {
        // TODO fill in bootstrap methods, fix the else if
        } else if (a.getTag() == org.apache.bcel.Const.ATTR_SOURCE_FILE) {
            v[a.getNameIndex()] = 1;
            break;
        }
    }
    String aflgs;
    String fldsig;
    String tdef;
    for (org.apache.bcel.classfile.Field fld : c.getFields()) {
        out.write(TAB);
        fout.write(TAB);
        aflgs = Utility.accessToString(fld.getAccessFlags());
        if (aflgs != null && aflgs.length() > 0) {
            out.write(aflgs);
            fout.write(aflgs);
            fout.write(SPACE);
            out.write(SPACE);
        }
        fldsig = Utility.typeSignatureToString(fld.getSignature(), true);
        out.write(fldsig);
        fout.write(fldsig);
        out.write(SPACE);
        fout.write(SPACE);
        tdef = tagDef(t = fld.getName());
        out.write(tdef);
        fout.write(tdef);
        defs.add(t);
        refs.add(t);
        out.write(EOL);
        fout.write(EOL);
    // TODO show Attributes
    }
    String sig;
    String msig;
    String ltdef;
    for (org.apache.bcel.classfile.Method m : c.getMethods()) {
        out.write(TAB);
        fout.write(TAB);
        aflgs = Utility.accessToString(m.getAccessFlags());
        if (aflgs != null && aflgs.length() > 0) {
            out.write(aflgs);
            fout.write(aflgs);
            out.write(SPACE);
            fout.write(SPACE);
        }
        sig = m.getSignature();
        msig = Utility.methodSignatureReturnType(sig, false);
        out.write(msig);
        fout.write(msig);
        out.write(SPACE);
        fout.write(SPACE);
        ltdef = tagDef(t = m.getName());
        out.write(ltdef);
        fout.write(ltdef);
        defs.add(t);
        refs.add(t);
        out.write(LBRA);
        fout.write(LBRA);
        String[] args = Utility.methodSignatureArgumentTypes(sig, false);
        for (int i = 0; i < args.length; i++) {
            t = args[i];
            out.write(t);
            fout.write(t);
            int spi = t.indexOf(SPACE);
            if (spi > 0) {
                refs.add(t.substring(0, spi));
                defs.add(t.substring(spi + 1));
            }
            if (i < args.length - 1) {
                out.write(COMMA);
                fout.write(COMMA);
            }
        }
        out.write(RBRA);
        fout.write(RBRA);
        ArrayList<LocalVariable[]> locals = new ArrayList<>();
        for (Attribute a : m.getAttributes()) {
            if (a.getTag() == org.apache.bcel.Const.ATTR_EXCEPTIONS) {
                for (int i : ((ExceptionTable) a).getExceptionIndexTable()) {
                    v[i] = 1;
                }
                String[] exs = ((ExceptionTable) a).getExceptionNames();
                if (exs != null && exs.length > 0) {
                    out.write(THROWS);
                    fout.write(THROWS);
                    for (String ex : exs) {
                        out.write(linkDef(ex));
                        fout.write(ex);
                        refs.add(ex);
                        out.write(SPACE);
                        fout.write(SPACE);
                    }
                }
            } else if (a.getTag() == org.apache.bcel.Const.ATTR_CODE) {
                for (Attribute ca : ((Code) a).getAttributes()) {
                    if (ca.getTag() == org.apache.bcel.Const.ATTR_LOCAL_VARIABLE_TABLE) {
                        locals.add(((LocalVariableTable) ca).getLocalVariableTable());
                    }
                }
            }
        }
        out.write(EOL);
        fout.write(EOL);
        if (!locals.isEmpty()) {
            for (LocalVariable[] ls : locals) {
                for (LocalVariable l : ls) {
                    printLocal(out, fout, l, v, defs, refs);
                }
            }
        }
    }
    out.write(RCBREOL);
    fout.write(RCBREOL);
    for (int i = 0; i < v.length - 1; i++) {
        if (v[i] != 1) {
            Constant constant = cp.getConstant(i);
            if (constant != null) {
                full.add(constantToString(constant, cp, v));
            }
        }
    }
}
Also used : LocalVariableTable(org.apache.bcel.classfile.LocalVariableTable) Attribute(org.apache.bcel.classfile.Attribute) Constant(org.apache.bcel.classfile.Constant) LocalVariable(org.apache.bcel.classfile.LocalVariable) ArrayList(java.util.ArrayList) ExceptionTable(org.apache.bcel.classfile.ExceptionTable) ConstantString(org.apache.bcel.classfile.ConstantString) Code(org.apache.bcel.classfile.Code) ConstantPool(org.apache.bcel.classfile.ConstantPool)

Aggregations

Constant (org.apache.bcel.classfile.Constant)21 ConstantPool (org.apache.bcel.classfile.ConstantPool)7 ConstantString (org.apache.bcel.classfile.ConstantString)7 ConstantClass (org.apache.bcel.classfile.ConstantClass)6 BugInstance (edu.umd.cs.findbugs.BugInstance)5 ToString (com.mebigfatguy.fbcontrib.utils.ToString)4 ConstantPoolGen (org.apache.bcel.generic.ConstantPoolGen)4 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)3 ArrayList (java.util.ArrayList)3 Iterator (java.util.Iterator)3 ConstantNameAndType (org.apache.bcel.classfile.ConstantNameAndType)3 ConstantUtf8 (org.apache.bcel.classfile.ConstantUtf8)3 Method (org.apache.bcel.classfile.Method)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 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Attribute (org.apache.bcel.classfile.Attribute)2