Search in sources :

Example 1 with LocalVariable

use of org.apache.bcel.classfile.LocalVariable 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.signatureToString(fld.getSignature());
        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)

Example 2 with LocalVariable

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

the class PoorlyDefinedParameter method sawOpcodeAfterCheckCast.

private void sawOpcodeAfterCheckCast(int seen) {
    state = State.SAW_NOTHING;
    if (!((seen == Const.PUTFIELD) || OpcodeUtils.isAStore(seen))) {
        return;
    }
    String parmName = null;
    LocalVariableTable lvt = getMethod().getLocalVariableTable();
    if (lvt != null) {
        LocalVariable lv = lvt.getLocalVariable(loadedReg, 1);
        if (lv != null) {
            parmName = lv.getName();
        }
    }
    if (parmName == null) {
        parmName = "(" + loadedReg + ')';
    }
    BugInstance bug = new BugInstance(this, BugType.PDP_POORLY_DEFINED_PARAMETER.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this).addString(parmName);
    Integer lr = Integer.valueOf(loadedReg);
    BugInfo bi = bugs.get(lr);
    if (bi == null) {
        bugs.put(lr, new BugInfo(castClass, bug));
    } else {
        // report it altho suspect
        if (!bi.castClass.equals(castClass)) {
            bugs.remove(lr);
        }
    }
}
Also used : LocalVariableTable(org.apache.bcel.classfile.LocalVariableTable) LocalVariable(org.apache.bcel.classfile.LocalVariable) BugInstance(edu.umd.cs.findbugs.BugInstance)

Example 3 with LocalVariable

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

the class PossibleConstantAllocationInLoop method isFirstUse.

/**
 * looks to see if this register has already in scope or whether is a new assignment. return true if it's a new assignment. If you can't tell, return true
 * anyway. might want to change.
 *
 * @param reg
 *            the store register
 * @return whether this is a new register scope assignment
 */
private boolean isFirstUse(int reg) {
    LocalVariableTable lvt = getMethod().getLocalVariableTable();
    if (lvt == null) {
        return true;
    }
    LocalVariable lv = lvt.getLocalVariable(reg, getPC());
    return lv == null;
}
Also used : LocalVariableTable(org.apache.bcel.classfile.LocalVariableTable) LocalVariable(org.apache.bcel.classfile.LocalVariable)

Example 4 with LocalVariable

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

the class SillynessPotPourri method sawLoad.

private void sawLoad(int seen) {
    lastLoadWasString = false;
    LocalVariableTable lvt = getMethod().getLocalVariableTable();
    if (lvt != null) {
        LocalVariable lv = LVTHelper.getLocalVariableAtPC(lvt, RegisterUtils.getALoadReg(this, seen), getPC());
        if (lv != null) {
            lastLoadWasString = Values.SIG_JAVA_LANG_STRING.equals(lv.getSignature());
        }
    }
}
Also used : LocalVariableTable(org.apache.bcel.classfile.LocalVariableTable) LocalVariable(org.apache.bcel.classfile.LocalVariable)

Example 5 with LocalVariable

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

the class OverzealousCasting method sawOpcode.

/**
 * implements the visitor to look for a checkcast followed by a astore, where the types of the objects are different.
 *
 * @param seen
 *            the opcode of the currently parsed instruction
 */
@Override
public void sawOpcode(int seen) {
    switch(state) {
        case SAW_NOTHING:
            if (seen == Const.CHECKCAST) {
                castClass = getClassConstantOperand();
                state = State.SAW_CHECKCAST;
            } else if (seen == Const.INVOKEINTERFACE) {
                // enhanced for loops add an incorrect checkcast instruction, so
                // ignore checkcasts after iterator.next()
                String clsName = getClassConstantOperand();
                String methodName = getNameConstantOperand();
                if ("java/util/Iterator".equals(clsName) && "next".equals(methodName)) {
                    state = State.SAW_NEXT;
                }
            }
            break;
        case SAW_CHECKCAST:
            if (OpcodeUtils.isAStore(seen)) {
                int reg = RegisterUtils.getAStoreReg(this, seen);
                LocalVariable lv = lvt.getLocalVariable(reg, getNextPC());
                if (lv != null) {
                    String sig = lv.getSignature();
                    if (sig.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX)) {
                        sig = SignatureUtils.trimSignature(sig);
                    }
                    if (!sig.equals(castClass)) {
                        bugReporter.reportBug(new BugInstance(this, BugType.OC_OVERZEALOUS_CASTING.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                    }
                }
            } else if (seen == Const.PUTFIELD) {
                FieldAnnotation f = FieldAnnotation.fromReferencedField(this);
                String sig = f.getFieldSignature();
                if (sig.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX)) {
                    sig = SignatureUtils.trimSignature(sig);
                }
                // if the signature is Object, the field might be genericized, so ignore
                if (!sig.equals(Values.SLASHED_JAVA_LANG_OBJECT) && !sig.equals(castClass)) {
                    bugReporter.reportBug(new BugInstance(this, BugType.OC_OVERZEALOUS_CASTING.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                }
            }
            state = State.SAW_NOTHING;
            break;
        case SAW_NEXT:
        default:
            state = State.SAW_NOTHING;
            break;
    }
}
Also used : LocalVariable(org.apache.bcel.classfile.LocalVariable) FieldAnnotation(edu.umd.cs.findbugs.FieldAnnotation) BugInstance(edu.umd.cs.findbugs.BugInstance)

Aggregations

LocalVariable (org.apache.bcel.classfile.LocalVariable)16 LocalVariableTable (org.apache.bcel.classfile.LocalVariableTable)12 BugInstance (edu.umd.cs.findbugs.BugInstance)5 ToString (com.mebigfatguy.fbcontrib.utils.ToString)3 ArrayList (java.util.ArrayList)3 Attribute (org.apache.bcel.classfile.Attribute)2 Code (org.apache.bcel.classfile.Code)2 Constant (org.apache.bcel.classfile.Constant)2 ConstantPool (org.apache.bcel.classfile.ConstantPool)2 ConstantString (org.apache.bcel.classfile.ConstantString)2 ExceptionTable (org.apache.bcel.classfile.ExceptionTable)2 BugType (com.mebigfatguy.fbcontrib.utils.BugType)1 StopOpcodeParsingException (com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException)1 FieldAnnotation (edu.umd.cs.findbugs.FieldAnnotation)1 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)1 Item (edu.umd.cs.findbugs.OpcodeStack.Item)1 BitSet (java.util.BitSet)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1