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

The following examples show how to use org.apache.bcel.classfile.Code#getExceptionTable() . 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: PreorderVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visitCode(Code obj) {
    code = obj;
    super.visitCode(obj);
    CodeException[] exceptions = obj.getExceptionTable();
    for (CodeException exception : exceptions) {
        exception.accept(this);
    }
    Attribute[] attributes = obj.getAttributes();
    for (Attribute attribute : attributes) {
        attribute.accept(this);
    }
    visitAfter(obj);
    code = null;
}
 
Example 2
Source File: Util.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static @CheckForNull CodeException getSurroundingTryBlock(ConstantPool constantPool, Code code,
        @CheckForNull String vmNameOfExceptionClass, int pc) {
    int size = Integer.MAX_VALUE;
    if (code.getExceptionTable() == null) {
        return null;
    }
    CodeException result = null;
    for (CodeException catchBlock : code.getExceptionTable()) {
        if (vmNameOfExceptionClass != null) {
            Constant catchType = constantPool.getConstant(catchBlock.getCatchType());
            if (catchType == null && !vmNameOfExceptionClass.isEmpty() || catchType instanceof ConstantClass
                    && !((ConstantClass) catchType).getBytes(constantPool).equals(vmNameOfExceptionClass)) {
                continue;
            }
        }
        int startPC = catchBlock.getStartPC();
        int endPC = catchBlock.getEndPC();
        if (pc >= startPC && pc <= endPC) {
            int thisSize = endPC - startPC;
            if (size > thisSize) {
                size = thisSize;
                result = catchBlock;
            }
        }
    }
    return result;
}
 
Example 3
Source File: DumbMethods.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visit(Method method) {
    String cName = getDottedClassName();

    for (SubDetector subDetector : subDetectors) {
        subDetector.initMethod(method);
    }

    // System.out.println(getFullyQualifiedMethodName());
    isPublicStaticVoidMain = method.isPublic() && method.isStatic() && "main".equals(getMethodName())
            || cName.toLowerCase().indexOf("benchmark") >= 0;
    prevOpcodeWasReadLine = false;
    Code code = method.getCode();
    if (code != null) {
        this.exceptionTable = code.getExceptionTable();
    }
    if (this.exceptionTable == null) {
        this.exceptionTable = new CodeException[0];
    }
    primitiveObjCtorSeen = null;
    ctorSeen = false;
    randomNextIntState = 0;
    checkForBitIorofSignedByte = false;
    sinceBufferedInputStreamReady = 100000;
    sawCheckForNonNegativeSignedByte = -1000;
    sawLoadOfMinValue = false;
    previousMethodCall = null;

}
 
Example 4
Source File: Util.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static int getSizeOfSurroundingTryBlock(ConstantPool constantPool, Code code,
        @CheckForNull @SlashedClassName String vmNameOfExceptionClass, int pc) {
    int size = Integer.MAX_VALUE;
    int tightStartPC = 0;
    int tightEndPC = Integer.MAX_VALUE;
    if (code.getExceptionTable() == null) {
        return size;
    }
    for (CodeException catchBlock : code.getExceptionTable()) {
        if (vmNameOfExceptionClass != null) {
            Constant catchType = constantPool.getConstant(catchBlock.getCatchType());
            if (catchType == null && !vmNameOfExceptionClass.isEmpty() || catchType instanceof ConstantClass
                    && !((ConstantClass) catchType).getBytes(constantPool).equals(vmNameOfExceptionClass)) {
                continue;
            }
        }
        int startPC = catchBlock.getStartPC();
        int endPC = catchBlock.getEndPC();
        if (pc >= startPC && pc <= endPC) {
            int thisSize = endPC - startPC;
            if (size > thisSize) {
                size = thisSize;
                tightStartPC = startPC;
                tightEndPC = endPC;
            }
        }
    }
    if (size == Integer.MAX_VALUE) {
        return size;
    }

    // try to guestimate number of lines that correspond
    size = (size + 7) / 8;
    LineNumberTable lineNumberTable = code.getLineNumberTable();
    if (lineNumberTable == null) {
        return size;
    }

    int count = 0;
    for (LineNumber line : lineNumberTable.getLineNumberTable()) {
        if (line.getStartPC() > tightEndPC) {
            break;
        }
        if (line.getStartPC() >= tightStartPC) {
            count++;
        }
    }
    return count;

}
 
Example 5
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);
        }
    }
}