Search in sources :

Example 6 with CodeException

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

the class AbnormalFinallyBlockReturn method catchBlockInFinally.

/**
 * looks to see if any try/catch block exists inside this finally block, that wrap the current pc. This is a lax check as the try catch block may not catch
 * exceptions that are thrown, but doing so would be prohibitively slow. But it should catch some problems.
 *
 * @param fBlockInfo
 *            the finally block the pc is currently in
 *
 * @return if all exceptions are caught inside this finally block
 */
private boolean catchBlockInFinally(FinallyBlockInfo fBlockInfo) {
    CodeException[] catchExceptions = getCode().getExceptionTable();
    if (CollectionUtils.isEmpty(catchExceptions)) {
        return false;
    }
    int pc = getPC();
    for (CodeException ex : catchExceptions) {
        if ((ex.getStartPC() <= pc) && (ex.getEndPC() >= pc) && (ex.getStartPC() >= fBlockInfo.startPC)) {
            return true;
        }
    }
    return false;
}
Also used : CodeException(org.apache.bcel.classfile.CodeException)

Example 7 with CodeException

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

the class AbnormalFinallyBlockReturn method visitCode.

/**
 * overrides the visitor to collect finally block info.
 *
 * @param obj
 *            the code object to scan for finally blocks
 */
@Override
public void visitCode(Code obj) {
    fbInfo.clear();
    loadedReg = -1;
    CodeException[] exc = obj.getExceptionTable();
    if (exc != null) {
        for (CodeException ce : exc) {
            if ((ce.getCatchType() == 0) && (ce.getStartPC() == ce.getHandlerPC())) {
                fbInfo.add(new FinallyBlockInfo(ce.getStartPC()));
            }
        }
    }
    if (!fbInfo.isEmpty()) {
        try {
            super.visitCode(obj);
        } catch (StopOpcodeParsingException e) {
        // no more finally blocks to check
        }
    }
}
Also used : CodeException(org.apache.bcel.classfile.CodeException) StopOpcodeParsingException(com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException)

Example 8 with CodeException

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

the class StackedTryBlocks method visitCode.

/**
 * overrides the visitor to look for 'idea' try catch blocks to find issues specifically, method needs two or more try catch blocks that only catch one
 * exception type.
 *
 * @param obj
 *            the currently parsed code object
 */
@Override
public void visitCode(Code obj) {
    try {
        XMethod xMethod = getXMethod();
        if (xMethod != null) {
            String[] tes = xMethod.getThrownExceptions();
            Set<String> thrownExceptions = new HashSet<>(Arrays.<String>asList((tes == null) ? new String[0] : tes));
            blocks = new ArrayList<>();
            inBlocks = new ArrayList<>();
            transitionPoints = new BitSet();
            CodeException[] ces = obj.getExceptionTable();
            for (CodeException ce : ces) {
                TryBlock tb = new TryBlock(ce);
                int existingBlock = blocks.indexOf(tb);
                if (existingBlock >= 0) {
                    tb = blocks.get(existingBlock);
                    tb.addCatchType(ce);
                } else {
                    blocks.add(tb);
                }
            }
            Iterator<TryBlock> it = blocks.iterator();
            while (it.hasNext()) {
                TryBlock block = it.next();
                if (block.hasMultipleHandlers() || block.isFinally() || block.catchIsThrown(getConstantPool(), thrownExceptions)) {
                    it.remove();
                }
            }
            if (blocks.size() > 1) {
                stack.resetForMethodEntry(this);
                super.visitCode(obj);
                if (blocks.size() > 1) {
                    TryBlock firstBlock = blocks.get(0);
                    for (int i = 1; i < blocks.size(); i++) {
                        TryBlock secondBlock = blocks.get(i);
                        if (!blocksSplitAcrossTransitions(firstBlock, secondBlock) && (firstBlock.getCatchType() == secondBlock.getCatchType()) && firstBlock.getThrowSignature().equals(secondBlock.getThrowSignature()) && firstBlock.getMessage().equals(secondBlock.getMessage()) && firstBlock.getExceptionSignature().equals(secondBlock.getExceptionSignature())) {
                            bugReporter.reportBug(new BugInstance(this, BugType.STB_STACKED_TRY_BLOCKS.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLineRange(this, firstBlock.getStartPC(), firstBlock.getEndHandlerPC()).addSourceLineRange(this, secondBlock.getStartPC(), secondBlock.getEndHandlerPC()));
                        }
                        firstBlock = secondBlock;
                    }
                }
            }
        }
    } finally {
        blocks = null;
        inBlocks = null;
        transitionPoints = null;
    }
}
Also used : CodeException(org.apache.bcel.classfile.CodeException) BitSet(java.util.BitSet) BugInstance(edu.umd.cs.findbugs.BugInstance) ToString(com.mebigfatguy.fbcontrib.utils.ToString) XMethod(edu.umd.cs.findbugs.ba.XMethod) HashSet(java.util.HashSet)

Example 9 with CodeException

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

the class OverlyConcreteParameter method isExceptionHandled.

/**
 * returns whether this exception is handled either in a try/catch or throws clause at this pc
 *
 * @param ex
 *            the name of the exception
 *
 * @return whether the exception is handled
 */
private boolean isExceptionHandled(String ex) {
    try {
        JavaClass thrownEx = Repository.lookupClass(ex);
        // First look at the throws clause
        ExceptionTable et = getMethod().getExceptionTable();
        if (et != null) {
            String[] throwClauseExNames = et.getExceptionNames();
            for (String throwClauseExName : throwClauseExNames) {
                JavaClass throwClauseEx = Repository.lookupClass(throwClauseExName);
                if (thrownEx.instanceOf(throwClauseEx)) {
                    return true;
                }
            }
        }
        // Next look at the try catch blocks
        CodeException[] catchExs = getCode().getExceptionTable();
        if (catchExs != null) {
            int pc = getPC();
            for (CodeException catchEx : catchExs) {
                if ((pc >= catchEx.getStartPC()) && (pc <= catchEx.getEndPC())) {
                    int type = catchEx.getCatchType();
                    if (type != 0) {
                        String catchExName = getConstantPool().getConstantString(type, Const.CONSTANT_Class);
                        JavaClass catchException = Repository.lookupClass(catchExName);
                        if (thrownEx.instanceOf(catchException)) {
                            return true;
                        }
                    }
                }
            }
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    }
    return false;
}
Also used : CodeException(org.apache.bcel.classfile.CodeException) JavaClass(org.apache.bcel.classfile.JavaClass) ExceptionTable(org.apache.bcel.classfile.ExceptionTable) ToString(com.mebigfatguy.fbcontrib.utils.ToString)

Example 10 with CodeException

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

the class UnnecessaryNewNullCheck method visitCode.

@Override
public void visitCode(Code obj) {
    if (prescreen()) {
        stack.resetForMethodEntry(this);
        allocationRegs.clear();
        transitionPoints.clear();
        CodeException[] ce = obj.getExceptionTable();
        if (ce != null) {
            for (CodeException element : ce) {
                transitionPoints.set(element.getEndPC());
            }
        }
        super.visitCode(obj);
    }
}
Also used : CodeException(org.apache.bcel.classfile.CodeException)

Aggregations

CodeException (org.apache.bcel.classfile.CodeException)15 ToString (com.mebigfatguy.fbcontrib.utils.ToString)4 BitSet (java.util.BitSet)3 Method (org.apache.bcel.classfile.Method)3 StopOpcodeParsingException (com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException)2 BugInstance (edu.umd.cs.findbugs.BugInstance)2 ArrayList (java.util.ArrayList)2 JavaClass (org.apache.bcel.classfile.JavaClass)2 FQMethod (com.mebigfatguy.fbcontrib.utils.FQMethod)1 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)1 XMethod (edu.umd.cs.findbugs.ba.XMethod)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 ConstantClass (org.apache.bcel.classfile.ConstantClass)1 ConstantPool (org.apache.bcel.classfile.ConstantPool)1 ExceptionTable (org.apache.bcel.classfile.ExceptionTable)1