Search in sources :

Example 1 with CodeException

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

the class ExceptionSoftening method collectExceptions.

/**
 * collects all the valid exception objects (ones where start and finish are before the target) and with a catch type
 *
 * @param exceptions
 *            the exceptions from the class file
 * @return the filtered exceptions keyed by catch end pc
 */
private static LinkedHashMap<Integer, CodeException> collectExceptions(CodeException... exceptions) {
    List<CodeException> filteredEx = new ArrayList<>();
    for (CodeException ce : exceptions) {
        if ((ce.getCatchType() != 0) && (ce.getStartPC() < ce.getEndPC()) && (ce.getEndPC() <= ce.getHandlerPC())) {
            filteredEx.add(ce);
        }
    }
    LinkedHashMap<Integer, CodeException> handlers = new LinkedHashMap<>();
    for (CodeException ex : filteredEx) {
        handlers.put(Integer.valueOf(ex.getEndPC()), ex);
    }
    return handlers;
}
Also used : CodeException(org.apache.bcel.classfile.CodeException) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with CodeException

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

the class PossiblyRedundantMethodCalls method visitCode.

/**
 * implements the visitor to reset the stack, and method call maps for new method Note: that when collecting branch targets, it's unfortunately not good
 * enough to just collect the handler pcs, as javac plays fast and loose, and will sometimes jam code below the end pc and before the first handler pc,
 * which gets executed. So we need to clear our maps if we go past the end pc as well.
 *
 * @param obj
 *            the context object of the currently parsed code block
 */
@Override
public void visitCode(Code obj) {
    stack.resetForMethodEntry(this);
    localMethodCalls.clear();
    fieldMethodCalls.clear();
    staticMethodCalls.clear();
    branchTargets.clear();
    CodeException[] codeExceptions = obj.getExceptionTable();
    for (CodeException codeEx : codeExceptions) {
        // adding the end pc seems silly, but it is need because javac may repeat
        // part of the finally block in the try block, at times.
        branchTargets.set(codeEx.getEndPC());
        branchTargets.set(codeEx.getHandlerPC());
    }
    super.visitCode(obj);
}
Also used : CodeException(org.apache.bcel.classfile.CodeException)

Example 3 with CodeException

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

the class PresizeCollections method addExceptionRanges.

/**
 * adds optionalRanges for all try/catch blocks
 *
 * @param c
 *            the currently parsed code object
 */
private void addExceptionRanges(Code c) {
    Map<CodeRange, List<Integer>> ranges = new HashMap<>();
    CodeException[] ces = c.getExceptionTable();
    if (ces != null) {
        for (CodeException ce : ces) {
            CodeRange range = new CodeRange(ce.getStartPC(), ce.getEndPC(), false);
            List<Integer> handlers = ranges.get(range);
            if (handlers == null) {
                handlers = new ArrayList<>(6);
                ranges.put(range, handlers);
            }
            handlers.add(ce.getHandlerPC());
        }
    }
    for (Map.Entry<CodeRange, List<Integer>> entry : ranges.entrySet()) {
        optionalRanges.add(entry.getKey());
        List<Integer> handlers = entry.getValue();
        Collections.sort(handlers);
        for (int h = 0; h < (handlers.size() - 1); h++) {
            optionalRanges.add(new CodeRange(handlers.get(h), handlers.get(h + 1), false));
        }
    }
}
Also used : CodeException(org.apache.bcel.classfile.CodeException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with CodeException

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

the class UnnecessaryStoreBeforeReturn method visitCode.

/**
 * implements the visitor to make sure method returns a value, and then clears the targets
 *
 * @param obj
 *            the context object of the currently parsed code block
 */
@Override
public void visitCode(Code obj) {
    Method m = getMethod();
    String sig = m.getSignature();
    if (!Values.SIG_VOID.equals(SignatureUtils.getReturnSignature(sig))) {
        state = State.SEEN_NOTHING;
        branchTargets.clear();
        CodeException[] ces = obj.getExceptionTable();
        catchTargets.clear();
        stack.resetForMethodEntry(this);
        for (CodeException ce : ces) {
            if (ce.getCatchType() != 0) {
                catchTargets.set(ce.getHandlerPC());
            }
        }
        super.visitCode(obj);
    }
}
Also used : CodeException(org.apache.bcel.classfile.CodeException) Method(org.apache.bcel.classfile.Method)

Example 5 with CodeException

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

the class UseTryWithResources method prescreen.

private boolean prescreen(Code obj) {
    if (getMethod().isNative()) {
        return false;
    }
    CodeException[] ces = obj.getExceptionTable();
    if (CollectionUtils.isEmpty(ces)) {
        return false;
    }
    boolean hasFinally = false;
    for (CodeException ce : ces) {
        if (ce.getCatchType() == 0) {
            finallyBlocks.put(Integer.valueOf(ce.getHandlerPC()), new TryBlock(ce.getStartPC(), ce.getEndPC(), ce.getHandlerPC(), obj.getCode().length - 1));
            hasFinally = true;
        }
    }
    return hasFinally;
}
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