Java Code Examples for org.apache.bcel.classfile.CodeException#getCatchType()

The following examples show how to use org.apache.bcel.classfile.CodeException#getCatchType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DontCatchIllegalMonitorStateException.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void visit(CodeException obj) {
    int type = obj.getCatchType();
    if (type == 0) {
        return;
    }
    String name = getConstantPool().constantToString(getConstantPool().getConstant(type));
    if (DEBUG) {
        String msg = "Catching " + name + " in " + getFullyQualifiedMethodName();
        if (msgs.add(msg)) {
            System.out.println(msg);
        }
    }
    if ("java.lang.IllegalMonitorStateException".equals(name)) {
        bugReporter.reportBug(new BugInstance(this, "IMSE_DONT_CATCH_IMSE", HIGH_PRIORITY).addClassAndMethod(this)
                .addSourceLine(this.classContext, this, obj.getHandlerPC()));
    }

}
 
Example 2
Source File: FindNullDeref.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private boolean uniqueLocations(Collection<Location> derefLocationSet) {
    boolean uniqueDereferenceLocations = false;
    CodeException[] exceptionTable = method.getCode().getExceptionTable();
    if (exceptionTable == null) {
        return true;
    }
    checkForCatchAll: {
        for (CodeException e : exceptionTable) {
            if (e.getCatchType() == 0) {
                break checkForCatchAll;
            }
        }
        return true;
    }

    LineNumberTable table = method.getLineNumberTable();
    if (table == null) {
        uniqueDereferenceLocations = true;
    } else {
        BitSet linesMentionedMultipleTimes = classContext.linesMentionedMultipleTimes(method);
        for (Location loc : derefLocationSet) {
            int lineNumber = table.getSourceLine(loc.getHandle().getPosition());
            if (lineNumber > 0 && !linesMentionedMultipleTimes.get(lineNumber)) {
                uniqueDereferenceLocations = true;
            }
        }
    }
    return uniqueDereferenceLocations;
}
 
Example 3
Source File: OpcodeStack.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static String getExceptionSig(DismantleBytecode dbc, CodeException e) {
    if (e.getCatchType() == 0) {
        return "Ljava/lang/Throwable;";
    }
    Constant c = dbc.getConstantPool().getConstant(e.getCatchType());
    if (c instanceof ConstantClass) {
        return "L" + ((ConstantClass) c).getBytes(dbc.getConstantPool()) + ";";
    }
    return "Ljava/lang/Throwable;";
}
 
Example 4
Source File: DumbMethods.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Flush out cached state at the end of a method.
 */
private void flush() {

    if (pendingAbsoluteValueBug != null) {
        absoluteValueAccumulator.accumulateBug(pendingAbsoluteValueBug, pendingAbsoluteValueBugSourceLine);
        pendingAbsoluteValueBug = null;
        pendingAbsoluteValueBugSourceLine = null;
    }
    accumulator.reportAccumulatedBugs();
    if (sawLoadOfMinValue) {
        absoluteValueAccumulator.clearBugs();
    } else {
        absoluteValueAccumulator.reportAccumulatedBugs();
    }
    if (gcInvocationBugReport != null && !sawCurrentTimeMillis) {
        // Make sure the GC invocation is not in an exception handler
        // for OutOfMemoryError.
        boolean outOfMemoryHandler = false;
        for (CodeException handler : exceptionTable) {
            if (gcInvocationPC < handler.getHandlerPC() || gcInvocationPC > handler.getHandlerPC() + OOM_CATCH_LEN) {
                continue;
            }
            int catchTypeIndex = handler.getCatchType();
            if (catchTypeIndex > 0) {
                ConstantPool cp = getThisClass().getConstantPool();
                Constant constant = cp.getConstant(catchTypeIndex);
                if (constant instanceof ConstantClass) {
                    String exClassName = (String) ((ConstantClass) constant).getConstantValue(cp);
                    if ("java/lang/OutOfMemoryError".equals(exClassName)) {
                        outOfMemoryHandler = true;
                        break;
                    }
                }
            }
        }

        if (!outOfMemoryHandler) {
            bugReporter.reportBug(gcInvocationBugReport);
        }
    }

    sawCurrentTimeMillis = false;
    gcInvocationBugReport = null;

    exceptionTable = null;
}
 
Example 5
Source File: FindNoSideEffectMethods.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void visit(Code obj) {
    uselessVoidCandidate = !classInit && !constructor && !getXMethod().isSynthetic() && Type.getReturnType(getMethodSig()) == Type.VOID;
    byte[] code = obj.getCode();
    if (code.length == 4 && (code[0] & 0xFF) == Const.GETSTATIC && (code[3] & 0xFF) == Const.ARETURN) {
        getStaticMethods.add(getMethodDescriptor());
        handleStatus();
        return;
    }

    if (code.length <= 2 && !getXMethod().isStatic() && (getXMethod().isPublic() || getXMethod().isProtected())
            && !getXMethod().isFinal() && (getXClass().isPublic() || getXClass().isProtected())) {
        for (byte[] stubMethod : STUB_METHODS) {
            if (Arrays.equals(stubMethod, code)
                    && (getClassName().endsWith("Visitor") || getClassName().endsWith("Listener") || !hasOtherImplementations(getXMethod()))) {
                // stub method which can be extended: assume it can be extended with possible side-effect
                status = SideEffectStatus.SIDE_EFFECT;
                handleStatus();
                return;
            }
        }
    }
    if (statusMap.containsKey(getMethodDescriptor())) {
        return;
    }
    finallyTargets = new HashSet<>();
    for (CodeException ex : getCode().getExceptionTable()) {
        if (ex.getCatchType() == 0) {
            finallyTargets.add(ex.getHandlerPC());
        }
    }
    finallyExceptionRegisters = new HashSet<>();
    try {
        super.visit(obj);
    } catch (EarlyExitException e) {
        // Ignore
    }
    if (uselessVoidCandidate && code.length > 1
            && (status == SideEffectStatus.UNSURE || status == SideEffectStatus.NO_SIDE_EFFECT)) {
        uselessVoidCandidates.add(getMethodDescriptor());
    }
    handleStatus();
}
 
Example 6
Source File: NullDerefAndRedundantComparisonFinder.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Examine redundant branches.
 */
private void examineRedundantBranches() {
    for (RedundantBranch redundantBranch : redundantBranchList) {
        if (DEBUG) {
            System.out.println("Redundant branch: " + redundantBranch);
        }
        int lineNumber = redundantBranch.lineNumber;

        // The source to bytecode compiler may sometimes duplicate blocks of
        // code along different control paths. So, to report the bug,
        // we check to ensure that the branch is REALLY determined each
        // place it is duplicated, and that it is determined in the same
        // way.

        boolean confused = undeterminedBranchSet.get(lineNumber)
                || (definitelySameBranchSet.get(lineNumber) && definitelyDifferentBranchSet.get(lineNumber));

        // confused if there is JSR confusion or multiple null checks with
        // different results on the same line

        boolean reportIt = true;

        if (lineMentionedMultipleTimes.get(lineNumber) && confused) {
            reportIt = false;
        } else if (redundantBranch.location.getBasicBlock().isInJSRSubroutine() /*
                                                                                * occurs
                                                                                * in
                                                                                * a
                                                                                * JSR
                                                                                */
                && confused) {
            reportIt = false;
        } else {
            int pc = redundantBranch.location.getHandle().getPosition();
            for (CodeException e : method.getCode().getExceptionTable()) {
                if (e.getCatchType() == 0 && e.getStartPC() != e.getHandlerPC() && e.getEndPC() <= pc
                        && pc <= e.getEndPC() + 5) {
                    reportIt = false;
                }
            }
        }

        if (reportIt) {
            collector.foundRedundantNullCheck(redundantBranch.location, redundantBranch);
        }
    }
}
 
Example 7
Source File: MethodGen.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/**
 * Instantiate from existing method.
 *
 * @param method method
 * @param className class name containing this method
 * @param cp constant pool
 */
public MethodGen(final Method method, final String className, final ConstantPoolGen cp) {
    this(method.getAccessFlags(), Type.getReturnType(method.getSignature()),
        Type.getArgumentTypes(method.getSignature()), null /* may be overridden anyway */
        , method.getName(), className,
        ((method.getAccessFlags() & (Const.ACC_ABSTRACT | Const.ACC_NATIVE)) == 0)
            ? new InstructionList(getByteCodes(method))
            : null,
        cp);
    final Attribute[] attributes = method.getAttributes();
    for (final Attribute attribute : attributes) {
        Attribute a = attribute;
        if (a instanceof Code) {
            final Code c = (Code) a;
            setMaxStack(c.getMaxStack());
            setMaxLocals(c.getMaxLocals());
            final CodeException[] ces = c.getExceptionTable();
            if (ces != null) {
                for (final CodeException ce : ces) {
                    final int type = ce.getCatchType();
                    ObjectType c_type = null;
                    if (type > 0) {
                        final String cen = method.getConstantPool().getConstantString(type, Const.CONSTANT_Class);
                        c_type = ObjectType.getInstance(cen);
                    }
                    final int end_pc = ce.getEndPC();
                    final int length = getByteCodes(method).length;
                    InstructionHandle end;
                    if (length == end_pc) { // May happen, because end_pc is exclusive
                        end = il.getEnd();
                    } else {
                        end = il.findHandle(end_pc);
                        end = end.getPrev(); // Make it inclusive
                    }
                    addExceptionHandler(il.findHandle(ce.getStartPC()), end, il.findHandle(ce.getHandlerPC()),
                        c_type);
                }
            }
            final Attribute[] c_attributes = c.getAttributes();
            for (final Attribute c_attribute : c_attributes) {
                a = c_attribute;
                if (a instanceof LineNumberTable) {
                    final LineNumber[] ln = ((LineNumberTable) a).getLineNumberTable();
                    for (final LineNumber l : ln) {
                        final InstructionHandle ih = il.findHandle(l.getStartPC());
                        if (ih != null) {
                            addLineNumber(ih, l.getLineNumber());
                        }
                    }
                } else if (a instanceof LocalVariableTable) {
                    updateLocalVariableTable((LocalVariableTable) a);
                } else if (a instanceof LocalVariableTypeTable) {
                    this.localVariableTypeTable = (LocalVariableTypeTable) a.copy(cp.getConstantPool());
                } else {
                    addCodeAttribute(a);
                }
            }
        } else if (a instanceof ExceptionTable) {
            final String[] names = ((ExceptionTable) a).getExceptionNames();
            for (final String name2 : names) {
                addException(name2);
            }
        } else if (a instanceof Annotations) {
            final Annotations runtimeAnnotations = (Annotations) a;
            final AnnotationEntry[] aes = runtimeAnnotations.getAnnotationEntries();
            for (final AnnotationEntry element : aes) {
                addAnnotationEntry(new AnnotationEntryGen(element, cp, false));
            }
        } else {
            addAttribute(a);
        }
    }
}