Search in sources :

Example 1 with CFGBuilderException

use of edu.umd.cs.findbugs.ba.CFGBuilderException in project fb-contrib by mebigfatguy.

the class CyclomaticComplexity method visitMethod.

/**
 * overrides the visitor to navigate the basic block list to count branches
 *
 * @param obj
 *            the method of the currently parsed method
 */
@Override
public void visitMethod(final Method obj) {
    try {
        if (obj.isSynthetic()) {
            return;
        }
        Code code = obj.getCode();
        if (code == null) {
            return;
        }
        // bother
        if (code.getCode().length < (2 * reportLimit)) {
            return;
        }
        BitSet exceptionNodeTargets = new BitSet();
        CFG cfg = classContext.getCFG(obj);
        int branches = 0;
        Iterator<BasicBlock> bbi = cfg.blockIterator();
        while (bbi.hasNext()) {
            BasicBlock bb = bbi.next();
            Iterator<Edge> iei = cfg.outgoingEdgeIterator(bb);
            int lastSwitchTargetBlockLabel = Integer.MIN_VALUE;
            while (iei.hasNext()) {
                Edge e = iei.next();
                int edgeType = e.getType();
                if ((edgeType != EdgeTypes.FALL_THROUGH_EDGE) && (edgeType != EdgeTypes.RETURN_EDGE) && (edgeType != EdgeTypes.UNKNOWN_EDGE)) {
                    if ((edgeType == EdgeTypes.UNHANDLED_EXCEPTION_EDGE) || (edgeType == EdgeTypes.HANDLED_EXCEPTION_EDGE)) {
                        int nodeTarget = e.getTarget().getLabel();
                        if (!exceptionNodeTargets.get(nodeTarget)) {
                            exceptionNodeTargets.set(nodeTarget);
                            branches++;
                        }
                    } else if ((edgeType == EdgeTypes.SWITCH_EDGE) || (edgeType == EdgeTypes.SWITCH_DEFAULT_EDGE)) {
                        int nodeTarget = e.getTarget().getLabel();
                        if (nodeTarget != lastSwitchTargetBlockLabel) {
                            branches++;
                        }
                        lastSwitchTargetBlockLabel = nodeTarget;
                    } else {
                        branches++;
                    }
                }
            }
        }
        if (branches > reportLimit) {
            int priority = (branches > (reportLimit * 2) ? HIGH_PRIORITY : NORMAL_PRIORITY);
            BugInstance bug = new BugInstance(this, BugType.CC_CYCLOMATIC_COMPLEXITY.name(), priority).addClass(this).addMethod(this).addSourceLine(classContext, this, 0).addInt(branches);
            bugReporter.reportBug(bug);
        }
    } catch (CFGBuilderException cbe) {
        bugReporter.logError("Failure examining basic blocks for method " + classContext.getJavaClass().getClassName() + '.' + obj.getName() + " in Cyclomatic Complexity detector", cbe);
    }
}
Also used : CFG(edu.umd.cs.findbugs.ba.CFG) CFGBuilderException(edu.umd.cs.findbugs.ba.CFGBuilderException) BitSet(java.util.BitSet) BasicBlock(edu.umd.cs.findbugs.ba.BasicBlock) BugInstance(edu.umd.cs.findbugs.BugInstance) Code(org.apache.bcel.classfile.Code) Edge(edu.umd.cs.findbugs.ba.Edge)

Example 2 with CFGBuilderException

use of edu.umd.cs.findbugs.ba.CFGBuilderException in project fb-contrib by mebigfatguy.

the class FieldCouldBeLocal method visitMethod.

/**
 * overrides the visitor to navigate basic blocks looking for all first usages of fields, removing those that are read from first.
 *
 * @param obj
 *            the context object of the currently parsed method
 */
@Override
public void visitMethod(Method obj) {
    if (localizableFields.isEmpty()) {
        return;
    }
    try {
        cfg = clsContext.getCFG(obj);
        cpg = cfg.getMethodGen().getConstantPool();
        BasicBlock bb = cfg.getEntry();
        Set<String> uncheckedFields = new HashSet<>(localizableFields.keySet());
        visitedBlocks.clear();
        checkBlock(bb, uncheckedFields);
    } catch (CFGBuilderException cbe) {
        localizableFields.clear();
    } finally {
        cfg = null;
        cpg = null;
    }
}
Also used : CFGBuilderException(edu.umd.cs.findbugs.ba.CFGBuilderException) BasicBlock(edu.umd.cs.findbugs.ba.BasicBlock) ToString(com.mebigfatguy.fbcontrib.utils.ToString) HashSet(java.util.HashSet)

Aggregations

BasicBlock (edu.umd.cs.findbugs.ba.BasicBlock)2 CFGBuilderException (edu.umd.cs.findbugs.ba.CFGBuilderException)2 ToString (com.mebigfatguy.fbcontrib.utils.ToString)1 BugInstance (edu.umd.cs.findbugs.BugInstance)1 CFG (edu.umd.cs.findbugs.ba.CFG)1 Edge (edu.umd.cs.findbugs.ba.Edge)1 BitSet (java.util.BitSet)1 HashSet (java.util.HashSet)1 Code (org.apache.bcel.classfile.Code)1