Search in sources :

Example 6 with LocalVariable

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

the class CommonsStringBuilderToString method sawOpcode.

@Override
public void sawOpcode(int seen) {
    switch(seen) {
        case Const.ALOAD:
        case Const.ALOAD_0:
        case Const.ALOAD_1:
        case Const.ALOAD_2:
        case Const.ALOAD_3:
            LocalVariable lv = getMethod().getLocalVariableTable().getLocalVariable(RegisterUtils.getALoadReg(this, seen), getNextPC());
            if (lv != null) {
                String signature = lv.getSignature();
                if (isToStringBuilder(signature)) {
                    Integer loadReg = Integer.valueOf(getRegisterOperand());
                    Boolean appendInvoked = registerTracker.get(loadReg);
                    if (appendInvoked != null) {
                        stackTracker.add(new StringBuilderInvokedStatus(loadReg.intValue(), appendInvoked.booleanValue()));
                    }
                }
            }
            break;
        case Const.ASTORE:
        case Const.ASTORE_0:
        case Const.ASTORE_1:
        case Const.ASTORE_2:
        case Const.ASTORE_3:
            Item si = stack.getStackItem(0);
            String signature = si.getSignature();
            if (isToStringBuilder(signature)) {
                int storeReg = getRegisterOperand();
                StringBuilderInvokedStatus p = stackTracker.pop();
                registerTracker.put(Integer.valueOf(storeReg), p.register == -1 ? Boolean.FALSE : registerTracker.get(Integer.valueOf(p.register)));
            }
            break;
        case Const.POP:
            si = stack.getStackItem(0);
            signature = si.getSignature();
            if (isToStringBuilder(signature) && !stackTracker.isEmpty()) {
                StringBuilderInvokedStatus p = stackTracker.pop();
                registerTracker.put(Integer.valueOf(p.register), Boolean.valueOf(p.appendInvoked));
            }
            break;
        case Const.INVOKESPECIAL:
        case Const.INVOKEVIRTUAL:
            String loadClassName = getClassConstantOperand();
            String calledMethodName = getNameConstantOperand();
            if ("org/apache/commons/lang3/builder/ToStringBuilder".equals(loadClassName) || "org/apache/commons/lang/builder/ToStringBuilder".equals(loadClassName)) {
                String calledMethodSig = getSigConstantOperand();
                if (Values.CONSTRUCTOR.equals(calledMethodName) && TOSTRINGBUILDER_CTOR_SIGS.contains(calledMethodSig)) {
                    stackTracker.add(new StringBuilderInvokedStatus(-1, false));
                } else if ("append".equals(calledMethodName)) {
                    StringBuilderInvokedStatus p = stackTracker.pop();
                    stackTracker.add(new StringBuilderInvokedStatus(p.register, true));
                } else if (Values.TOSTRING.equals(calledMethodName) && SignatureBuilder.SIG_VOID_TO_STRING.equals(calledMethodSig)) {
                    StringBuilderInvokedStatus p = stackTracker.pop();
                    if (!p.appendInvoked) {
                        bugReporter.reportBug(new BugInstance(this, "CSBTS_COMMONS_STRING_BUILDER_TOSTRING", HIGH_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                    }
                }
            }
            break;
        default:
            break;
    }
}
Also used : Item(edu.umd.cs.findbugs.OpcodeStack.Item) LocalVariable(org.apache.bcel.classfile.LocalVariable) BugInstance(edu.umd.cs.findbugs.BugInstance) ToString(com.mebigfatguy.fbcontrib.utils.ToString)

Example 7 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.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)

Example 8 with LocalVariable

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

the class SloppyClassReflection method visitMethod.

/**
 * overrides the visitor reset the opcode stack
 *
 * @param obj
 *            the method object of the currently parsed method
 */
@Override
public void visitMethod(Method obj) {
    if (Values.STATIC_INITIALIZER.equals(obj.getName())) {
        return;
    }
    if (state == State.COLLECT) {
        Type[] argTypes = obj.getArgumentTypes();
        for (Type t : argTypes) {
            addType(t);
        }
        Type resultType = obj.getReturnType();
        addType(resultType);
        LocalVariableTable lvt = obj.getLocalVariableTable();
        if (lvt != null) {
            LocalVariable[] lvs = lvt.getLocalVariableTable();
            if (lvs != null) {
                for (LocalVariable lv : lvs) {
                    if (lv != null) {
                        Type t = Type.getType(lv.getSignature());
                        addType(t);
                    }
                }
            }
        }
    } else {
        state = State.SEEN_NOTHING;
    }
    super.visitMethod(obj);
}
Also used : LocalVariableTable(org.apache.bcel.classfile.LocalVariableTable) BugType(com.mebigfatguy.fbcontrib.utils.BugType) Type(org.apache.bcel.generic.Type) LocalVariable(org.apache.bcel.classfile.LocalVariable)

Example 9 with LocalVariable

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

the class OverlyConcreteParameter method reportBugs.

/**
 * implements the post processing steps to report the remaining unremoved parameter definers, ie those, that can be defined more abstractly.
 */
private void reportBugs() {
    Iterator<Map.Entry<Integer, Map<JavaClass, List<MethodInfo>>>> it = parameterDefiners.entrySet().iterator();
    while (it.hasNext()) {
        try {
            Map.Entry<Integer, Map<JavaClass, List<MethodInfo>>> entry = it.next();
            Integer reg = entry.getKey();
            if (!usedParameters.get(reg.intValue())) {
                it.remove();
                continue;
            }
            Map<JavaClass, List<MethodInfo>> definers = entry.getValue();
            definers.remove(objectClass);
            if (definers.size() > 1) {
                removeInheritedInterfaces(definers);
            }
            if (definers.size() == 1) {
                if (!hasOverloadedMethod()) {
                    String name = "";
                    LocalVariableTable lvt = getMethod().getLocalVariableTable();
                    if (lvt != null) {
                        LocalVariable lv = lvt.getLocalVariable(reg.intValue(), 0);
                        if (lv != null) {
                            name = lv.getName();
                        }
                    }
                    int parm = reg.intValue();
                    if (!methodIsStatic) {
                        parm--;
                    }
                    // users expect 1 based parameters
                    parm++;
                    String infName = definers.keySet().iterator().next().getClassName();
                    bugReporter.reportBug(new BugInstance(this, BugType.OCP_OVERLY_CONCRETE_PARAMETER.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this, 0).addString(getCardinality(parm) + " parameter '" + name + "' could be declared as " + infName + " instead"));
                }
            }
        } catch (ClassNotFoundException e) {
            bugReporter.reportMissingClass(e);
        }
    }
}
Also used : LocalVariableTable(org.apache.bcel.classfile.LocalVariableTable) LocalVariable(org.apache.bcel.classfile.LocalVariable) BugInstance(edu.umd.cs.findbugs.BugInstance) ToString(com.mebigfatguy.fbcontrib.utils.ToString) AnnotationEntry(org.apache.bcel.classfile.AnnotationEntry) ParameterAnnotationEntry(org.apache.bcel.classfile.ParameterAnnotationEntry) JavaClass(org.apache.bcel.classfile.JavaClass) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with LocalVariable

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

the class SuspiciousLoopSearch method sawOpcodeAfterBranch.

private void sawOpcodeAfterBranch(int seen) {
    if (!ifBlocks.isEmpty()) {
        IfBlock block = ifBlocks.get(ifBlocks.size() - 1);
        if (OpcodeUtils.isStore(seen)) {
            int reg = RegisterUtils.getStoreReg(this, seen);
            if (!loadedRegs.containsKey(reg)) {
                LocalVariableTable lvt = getMethod().getLocalVariableTable();
                String sig = "";
                if (lvt != null) {
                    LocalVariable lv = lvt.getLocalVariable(reg, getPC());
                    if (lv != null) {
                        sig = lv.getSignature();
                    }
                }
                // relatively normal occurrence
                if (!Values.SIG_PRIMITIVE_BOOLEAN.equals(sig) && !Values.SIG_JAVA_LANG_BOOLEAN.equals(sig)) {
                    block.storeRegs.put(Integer.valueOf(RegisterUtils.getStoreReg(this, seen)), Integer.valueOf(getPC()));
                }
            }
        } else if (OpcodeUtils.isReturn(seen)) {
            copyStoredIntoLoadedforBlock(block);
        }
        if (block.end <= getPC()) {
            state = State.SAW_NOTHING;
        }
    }
}
Also used : LocalVariableTable(org.apache.bcel.classfile.LocalVariableTable) LocalVariable(org.apache.bcel.classfile.LocalVariable) ToString(com.mebigfatguy.fbcontrib.utils.ToString)

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