Search in sources :

Example 16 with StopOpcodeParsingException

use of com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException in project fb-contrib by mebigfatguy.

the class PartiallyConstructedObjectAccess method sawOpcode.

@Override
public void sawOpcode(int seen) {
    try {
        stack.precomputation(this);
        if ((seen == Const.INVOKEVIRTUAL) || (seen == Const.INVOKEINTERFACE) || (seen == Const.INVOKESPECIAL)) {
            int parmCount = SignatureUtils.getNumParameters(getSigConstantOperand());
            if (stack.getStackDepth() > parmCount) {
                OpcodeStack.Item itm = stack.getStackItem(parmCount);
                if (itm.getRegisterNumber() == 0) {
                    JavaClass cls = itm.getJavaClass();
                    if (cls != null) {
                        Method m = findMethod(cls, getNameConstantOperand(), getSigConstantOperand());
                        if ((m != null) && (!m.isFinal())) {
                            if (isCtor && (seen != Const.INVOKESPECIAL)) {
                                bugReporter.reportBug(new BugInstance(this, BugType.PCOA_PARTIALLY_CONSTRUCTED_OBJECT_ACCESS.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this, getPC()));
                                throw new StopOpcodeParsingException();
                            } else {
                                if (!Values.CONSTRUCTOR.equals(m.getName())) {
                                    Map<Method, SourceLineAnnotation> calledMethods = methodToCalledMethods.get(getMethod());
                                    calledMethods.put(m, SourceLineAnnotation.fromVisitedInstruction(this));
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    } finally {
        stack.sawOpcode(this, seen);
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) JavaClass(org.apache.bcel.classfile.JavaClass) SourceLineAnnotation(edu.umd.cs.findbugs.SourceLineAnnotation) StopOpcodeParsingException(com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException) BugInstance(edu.umd.cs.findbugs.BugInstance) Method(org.apache.bcel.classfile.Method)

Example 17 with StopOpcodeParsingException

use of com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException in project fb-contrib by mebigfatguy.

the class ModifyingUnmodifiableCollection method sawOpcode.

/**
 * overrides the visitor to find method mutations on collections that have previously been determined to have been created as immutable collections
 *
 * @param seen
 *            the currently parsed opcode
 */
@Override
public void sawOpcode(int seen) {
    ImmutabilityType imType = null;
    try {
        stack.precomputation(this);
        switch(seen) {
            case INVOKESTATIC:
            case INVOKEINTERFACE:
            case INVOKESPECIAL:
            case INVOKEVIRTUAL:
                {
                    String className = getClassConstantOperand();
                    String methodName = getNameConstantOperand();
                    String signature = getSigConstantOperand();
                    MethodInfo mi = Statistics.getStatistics().getMethodStatistics(className, methodName, signature);
                    imType = mi.getImmutabilityType();
                    if (seen == INVOKEINTERFACE) {
                        Integer collectionOffset = MODIFYING_METHODS.get(new QMethod(methodName, signature));
                        if ((collectionOffset != null) && CollectionUtils.isListSetMap(className) && (stack.getStackDepth() > collectionOffset.intValue())) {
                            OpcodeStack.Item item = stack.getStackItem(collectionOffset.intValue());
                            ImmutabilityType type = (ImmutabilityType) item.getUserValue();
                            if ((type == ImmutabilityType.IMMUTABLE) || ((type == ImmutabilityType.POSSIBLY_IMMUTABLE) && (reportedType != ImmutabilityType.POSSIBLY_IMMUTABLE))) {
                                bugReporter.reportBug(new BugInstance(this, BugType.MUC_MODIFYING_UNMODIFIABLE_COLLECTION.name(), (type == ImmutabilityType.IMMUTABLE) ? HIGH_PRIORITY : NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                                if (type == ImmutabilityType.IMMUTABLE) {
                                    throw new StopOpcodeParsingException();
                                }
                                reportedType = type;
                            }
                        }
                    }
                }
                break;
            default:
                break;
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    } finally {
        stack.sawOpcode(this, seen);
        if ((imType != null) && (stack.getStackDepth() > 0)) {
            OpcodeStack.Item item = stack.getStackItem(0);
            item.setUserValue(imType);
        }
    }
}
Also used : QMethod(com.mebigfatguy.fbcontrib.utils.QMethod) OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) ImmutabilityType(com.mebigfatguy.fbcontrib.collect.ImmutabilityType) StopOpcodeParsingException(com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException) BugInstance(edu.umd.cs.findbugs.BugInstance) MethodInfo(com.mebigfatguy.fbcontrib.collect.MethodInfo)

Example 18 with StopOpcodeParsingException

use of com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException in project fb-contrib by mebigfatguy.

the class UnusedParameter method visitCode.

/**
 * implements the visitor to clear the parm set, and check for potential methods
 *
 * @param obj
 *            the context object of the currently parsed code block
 */
@Override
public void visitCode(Code obj) {
    unusedParms.clear();
    regToParm.clear();
    stack.resetForMethodEntry(this);
    Method m = getMethod();
    String methodName = m.getName();
    if (IGNORE_METHODS.contains(methodName)) {
        return;
    }
    int accessFlags = m.getAccessFlags();
    if (((accessFlags & (Const.ACC_STATIC | Const.ACC_PRIVATE)) == 0) || ((accessFlags & Const.ACC_SYNTHETIC) != 0)) {
        return;
    }
    List<String> parmTypes = SignatureUtils.getParameterSignatures(m.getSignature());
    if (parmTypes.isEmpty()) {
        return;
    }
    int firstReg = 0;
    if ((accessFlags & Const.ACC_STATIC) == 0) {
        ++firstReg;
    }
    int reg = firstReg;
    for (int i = 0; i < parmTypes.size(); ++i) {
        unusedParms.set(reg);
        regToParm.put(Integer.valueOf(reg), Integer.valueOf(i + 1));
        String parmSig = parmTypes.get(i);
        reg += SignatureUtils.getSignatureSize(parmSig);
    }
    try {
        super.visitCode(obj);
        if (!unusedParms.isEmpty()) {
            LocalVariableTable lvt = m.getLocalVariableTable();
            reg = unusedParms.nextSetBit(firstReg);
            while (reg >= 0) {
                LocalVariable lv = (lvt == null) ? null : lvt.getLocalVariable(reg, 0);
                if (lv != null) {
                    String parmName = lv.getName();
                    bugReporter.reportBug(new BugInstance(this, BugType.UP_UNUSED_PARAMETER.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addString("Parameter " + regToParm.get(Integer.valueOf(reg)) + ": " + parmName));
                }
                reg = unusedParms.nextSetBit(reg + 1);
            }
        }
    } catch (StopOpcodeParsingException e) {
    // no unusedParms left
    }
}
Also used : LocalVariableTable(org.apache.bcel.classfile.LocalVariableTable) StopOpcodeParsingException(com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException) LocalVariable(org.apache.bcel.classfile.LocalVariable) BugInstance(edu.umd.cs.findbugs.BugInstance) Method(org.apache.bcel.classfile.Method)

Example 19 with StopOpcodeParsingException

use of com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException in project fb-contrib by mebigfatguy.

the class DubiousMapCollection method sawOpcode.

@Override
public void sawOpcode(int seen) {
    try {
        if (getPC() >= ternaryTarget) {
            if (stack.getStackDepth() > 0) {
                OpcodeStack.Item itm = stack.getStackItem(0);
                XField xf = itm.getXField();
                if (xf != null) {
                    itm.setUserValue(ternaryAccessedField);
                }
            }
            ternaryAccessedField = null;
            ternaryTarget = -1;
        }
        if ((seen == Const.INVOKEINTERFACE) || (seen == Const.INVOKEVIRTUAL)) {
            processNormalInvoke();
            processMethodCall();
        } else if ((seen == Const.INVOKESPECIAL) || (seen == Const.INVOKESTATIC) || (seen == Const.INVOKEDYNAMIC)) {
            processMethodCall();
        } else if ((seen == Const.ARETURN) || (OpcodeUtils.isAStore(seen))) {
            if (stack.getStackDepth() > 0) {
                OpcodeStack.Item item = stack.getStackItem(0);
                removeField(item);
            }
        } else if ((seen == Const.PUTFIELD) || (seen == Const.PUTSTATIC)) {
            XField xf = getXFieldOperand();
            if (xf != null) {
                if (!isInSpecial) {
                    mapFields.remove(xf.getName());
                } else {
                    if (stack.getStackDepth() > 0) {
                        OpcodeStack.Item item = stack.getStackItem(0);
                        if (item.getRegisterNumber() >= 0) {
                            removeField(item);
                        }
                    }
                }
                if (mapFields.isEmpty()) {
                    throw new StopOpcodeParsingException();
                }
            }
        } else if ((seen == Const.GOTO) || (seen == Const.GOTO_W)) {
            if ((getBranchOffset() > 0) && (stack.getStackDepth() > 0)) {
                OpcodeStack.Item itm = stack.getStackItem(0);
                ternaryAccessedField = itm.getXField();
                if (ternaryAccessedField != null) {
                    ternaryTarget = getBranchTarget();
                }
            }
        }
    } finally {
        stack.sawOpcode(this, seen);
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) XField(edu.umd.cs.findbugs.ba.XField) StopOpcodeParsingException(com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException)

Example 20 with StopOpcodeParsingException

use of com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException in project fb-contrib by mebigfatguy.

the class DubiousMapCollection method removeField.

/*
     * removes a potential field that was accessed from the map fields if this field
     * was retrieved from a ternary, the user value will hold the first variable,
     * and that will be removed too.
     *
     * @param itm the stack item that may potentially be a map field to remove as it
     * is used like a map
     */
private void removeField(OpcodeStack.Item itm) {
    XField xf = itm.getXField();
    if (xf != null) {
        mapFields.remove(xf.getName());
        xf = (XField) itm.getUserValue();
        if (xf != null) {
            mapFields.remove(xf.getName());
        }
        if (mapFields.isEmpty()) {
            throw new StopOpcodeParsingException();
        }
    }
}
Also used : XField(edu.umd.cs.findbugs.ba.XField) StopOpcodeParsingException(com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException)

Aggregations

StopOpcodeParsingException (com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException)24 BugInstance (edu.umd.cs.findbugs.BugInstance)11 Method (org.apache.bcel.classfile.Method)11 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)8 JavaClass (org.apache.bcel.classfile.JavaClass)5 XField (edu.umd.cs.findbugs.ba.XField)4 ToString (com.mebigfatguy.fbcontrib.utils.ToString)3 SourceLineAnnotation (edu.umd.cs.findbugs.SourceLineAnnotation)2 CodeException (org.apache.bcel.classfile.CodeException)2 ImmutabilityType (com.mebigfatguy.fbcontrib.collect.ImmutabilityType)1 MethodInfo (com.mebigfatguy.fbcontrib.collect.MethodInfo)1 BugType (com.mebigfatguy.fbcontrib.utils.BugType)1 QMethod (com.mebigfatguy.fbcontrib.utils.QMethod)1 BitSet (java.util.BitSet)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 Code (org.apache.bcel.classfile.Code)1 ExceptionTable (org.apache.bcel.classfile.ExceptionTable)1