Java Code Examples for org.apache.bcel.generic.CodeExceptionGen#getCatchType()

The following examples show how to use org.apache.bcel.generic.CodeExceptionGen#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: BlockTypeAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void transfer(BasicBlock basicBlock, @CheckForNull InstructionHandle end, BlockType start, BlockType result)
        throws DataflowAnalysisException {
    result.copyFrom(start);

    if (start.isValid() && basicBlock.isExceptionHandler()) {
        CodeExceptionGen exceptionGen = basicBlock.getExceptionGen();
        ObjectType catchType = exceptionGen.getCatchType();
        if (catchType == null) {
            // Probably a finally block, or a synchronized block
            // exception-compensation catch block.
            result.pushFinally();
        } else {
            // Catch type was explicitly specified:
            // this is probably a programmer-written catch block
            result.pushCatch();
        }
    }
}
 
Example 2
Source File: ExceptionHandlers.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor. Creates a new ExceptionHandlers instance.
 */
public ExceptionHandlers(final MethodGen mg) {
    exceptionHandlers = new HashMap<>();
    final CodeExceptionGen[] cegs = mg.getExceptionHandlers();
    for (final CodeExceptionGen ceg : cegs) {
        final ExceptionHandler eh = new ExceptionHandler(ceg.getCatchType(), ceg.getHandlerPC());
        for (InstructionHandle ih=ceg.getStartPC(); ih != ceg.getEndPC().getNext(); ih=ih.getNext()) {
            Set<ExceptionHandler> hs;
            hs = exceptionHandlers.get(ih);
            if (hs == null) {
                hs = new HashSet<>();
                exceptionHandlers.put(ih, hs);
            }
            hs.add(eh);
        }
    }
}
 
Example 3
Source File: BCELFactory.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
private void updateExceptionHandlers() {
    final CodeExceptionGen[] handlers = _mg.getExceptionHandlers();
    for (final CodeExceptionGen h : handlers) {
        final String type = (h.getCatchType() == null) ? "null" : BCELifier.printType(h
                .getCatchType());
        _out.println("    method.addExceptionHandler(" + "ih_" + h.getStartPC().getPosition()
                + ", " + "ih_" + h.getEndPC().getPosition() + ", " + "ih_"
                + h.getHandlerPC().getPosition() + ", " + type + ");");
    }
}
 
Example 4
Source File: TypeAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void meetInto(TypeFrame fact, Edge edge, TypeFrame result) throws DataflowAnalysisException {
    BasicBlock basicBlock = edge.getTarget();

    if (fact.isValid()) {
        TypeFrame tmpFact = null;

        // Handling an exception?
        if (basicBlock.isExceptionHandler()) {
            tmpFact = modifyFrame(fact, null);

            // Special case: when merging predecessor facts for entry to
            // an exception handler, we clear the stack and push a
            // single entry for the exception object. That way, the locals
            // can still be merged.
            CodeExceptionGen exceptionGen = basicBlock.getExceptionGen();
            tmpFact.clearStack();

            // Determine the type of exception(s) caught.
            Type catchType = null;

            if (FORCE_ACCURATE_EXCEPTIONS
                    || AnalysisContext.currentAnalysisContext().getBoolProperty(AnalysisFeatures.ACCURATE_EXCEPTIONS)) {
                try {
                    // Ideally, the exceptions that can be propagated
                    // on this edge has already been computed.
                    CachedExceptionSet cachedExceptionSet = getCachedExceptionSet(edge.getSource());
                    ExceptionSet edgeExceptionSet = cachedExceptionSet.getEdgeExceptionSet(edge);
                    if (!edgeExceptionSet.isEmpty()) {
                        // System.out.println("Using computed edge exception set!");
                        catchType = ExceptionObjectType.fromExceptionSet(edgeExceptionSet);
                    }
                } catch (ClassNotFoundException e) {
                    lookupFailureCallback.reportMissingClass(e);
                }
            }

            if (catchType == null) {
                // No information about propagated exceptions, so
                // pick a type conservatively using the handler catch type.
                catchType = exceptionGen.getCatchType();
                if (catchType == null) {
                    catchType = Type.THROWABLE; // handle catches anything
                    // throwable
                }
            }

            tmpFact.pushValue(catchType);
        }

        // See if we can make some types more precise due to
        // a successful instanceof check in the source block.
        if (valueNumberDataflow != null) {
            tmpFact = handleInstanceOfBranch(fact, tmpFact, edge);
        }

        if (tmpFact != null) {
            fact = tmpFact;
        }
    }

    mergeInto(fact, result);
}
 
Example 5
Source File: TypeAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Based on the set of exceptions that can be thrown from the source basic
 * block, compute the set of exceptions that can propagate along given
 * exception edge. This method should be called for each outgoing exception
 * edge in sequence, so the caught exceptions can be removed from the thrown
 * exception set as needed.
 *
 * @param edge
 *            the exception edge
 * @param thrownExceptionSet
 *            current set of exceptions that can be thrown, taking earlier
 *            (higher priority) exception edges into account
 * @return the set of exceptions that can propagate along this edge
 */
private ExceptionSet computeEdgeExceptionSet(Edge edge, ExceptionSet thrownExceptionSet) {

    if (thrownExceptionSet.isEmpty()) {
        return thrownExceptionSet;
    }
    ExceptionSet result = exceptionSetFactory.createExceptionSet();

    if (edge.getType() == UNHANDLED_EXCEPTION_EDGE) {
        // The unhandled exception edge always comes
        // after all of the handled exception edges.
        result.addAll(thrownExceptionSet);
        thrownExceptionSet.clear();
        return result;
    }

    BasicBlock handlerBlock = edge.getTarget();
    CodeExceptionGen handler = handlerBlock.getExceptionGen();
    ObjectType catchType = handler.getCatchType();

    if (Hierarchy.isUniversalExceptionHandler(catchType)) {
        result.addAll(thrownExceptionSet);
        thrownExceptionSet.clear();
    } else {
        // Go through the set of thrown exceptions.
        // Any that will DEFINITELY be caught be this handler, remove.
        // Any that MIGHT be caught, but won't definitely be caught,
        // remain.

        for (ExceptionSet.ThrownExceptionIterator i = thrownExceptionSet.iterator(); i.hasNext();) {
            // ThrownException thrownException = i.next();
            ObjectType thrownType = i.next();
            boolean explicit = i.isExplicit();

            if (DEBUG) {
                System.out.println("\texception type " + thrownType + ", catch type " + catchType);
            }

            try {
                if (Hierarchy.isSubtype(thrownType, catchType)) {
                    // Exception can be thrown along this edge
                    result.add(thrownType, explicit);

                    // And it will definitely be caught
                    i.remove();

                    if (DEBUG) {
                        System.out.println("\tException is subtype of catch type: " + "will definitely catch");
                    }
                } else if (Hierarchy.isSubtype(catchType, thrownType)) {
                    // Exception possibly thrown along this edge
                    result.add(thrownType, explicit);

                    if (DEBUG) {
                        System.out.println("\tException is supertype of catch type: " + "might catch");
                    }
                }
            } catch (ClassNotFoundException e) {
                // As a special case, if a class hierarchy lookup
                // fails, then we will conservatively assume that the
                // exception in question CAN, but WON'T NECESSARILY
                // be caught by the handler.
                AnalysisContext.reportMissingClass(e);
                result.add(thrownType, explicit);
            }
        }
    }

    return result;
}