Search in sources :

Example 76 with Method

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

the class BogusExceptionDeclaration method sawOpcode.

/**
 * implements the visitor to look for method calls that could throw the exceptions that are listed in the declaration.
 */
@Override
public void sawOpcode(int seen) {
    try {
        stack.precomputation(this);
        if (OpcodeUtils.isStandardInvoke(seen)) {
            String clsName = getClassConstantOperand();
            if (!safeClasses.contains(clsName)) {
                try {
                    JavaClass cls = Repository.lookupClass(clsName);
                    Method[] methods = cls.getMethods();
                    String methodName = getNameConstantOperand();
                    String signature = getSigConstantOperand();
                    boolean found = false;
                    for (Method m : methods) {
                        if (m.getName().equals(methodName) && m.getSignature().equals(signature)) {
                            if (isAnonymousInnerCtor(m, cls)) {
                                // constructors, so just clear if so
                                break;
                            }
                            ExceptionTable et = m.getExceptionTable();
                            if (et != null) {
                                String[] thrownExceptions = et.getExceptionNames();
                                for (String thrownException : thrownExceptions) {
                                    removeThrownExceptionHierarchy(thrownException);
                                }
                            }
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        clearExceptions();
                    }
                } catch (ClassNotFoundException cnfe) {
                    bugReporter.reportMissingClass(cnfe);
                    clearExceptions();
                }
            } else if ("wait".equals(getNameConstantOperand())) {
                removeException("java.lang.InterruptedException");
            }
        } else if (seen == Const.ATHROW) {
            if (stack.getStackDepth() > 0) {
                OpcodeStack.Item item = stack.getStackItem(0);
                String exSig = item.getSignature();
                String thrownException = SignatureUtils.stripSignature(exSig);
                removeThrownExceptionHierarchy(thrownException);
            } else {
                clearExceptions();
            }
        }
    } finally {
        stack.sawOpcode(this, seen);
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) ExceptionTable(org.apache.bcel.classfile.ExceptionTable) Method(org.apache.bcel.classfile.Method)

Example 77 with Method

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

the class BuryingLogic method visitCode.

@Override
public void visitCode(Code obj) {
    Method m = getMethod();
    if (m.getReturnType() == Type.VOID) {
        return;
    }
    stack.resetForMethodEntry(this);
    ifBlocks.clear();
    activeUnconditional = null;
    CodeException[] ces = obj.getExceptionTable();
    if (CollectionUtils.isEmpty(ces)) {
        catchPCs = null;
    } else {
        catchPCs = new BitSet();
        for (CodeException ce : ces) {
            catchPCs.set(ce.getHandlerPC());
        }
    }
    gotoBranchPCs.clear();
    casePositions.clear();
    lookingForResetOp = false;
    try {
        super.visitCode(obj);
    } catch (StopOpcodeParsingException e) {
    // reported an issue, so get out
    }
}
Also used : CodeException(org.apache.bcel.classfile.CodeException) StopOpcodeParsingException(com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException) BitSet(java.util.BitSet) Method(org.apache.bcel.classfile.Method)

Example 78 with Method

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

the class FunctionalInterfaceIssues method visitClassContext.

@Override
public void visitClassContext(ClassContext classContext) {
    try {
        JavaClass cls = classContext.getJavaClass();
        if (cls.getMajor() >= Const.MAJOR_1_8) {
            bootstrapAtt = getBootstrapAttribute(cls);
            if (bootstrapAtt != null) {
                stack = new OpcodeStack();
                functionalInterfaceInfo = new HashMap<>();
                super.visitClassContext(classContext);
                for (Map.Entry<Method, List<FIInfo>> entry : functionalInterfaceInfo.entrySet()) {
                    for (FIInfo fii : entry.getValue()) {
                    }
                }
            }
        }
    } finally {
        functionalInterfaceInfo = null;
        bootstrapAtt = null;
        stack = null;
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) ArrayList(java.util.ArrayList) List(java.util.List) Method(org.apache.bcel.classfile.Method) HashMap(java.util.HashMap) Map(java.util.Map)

Example 79 with Method

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

the class CollectMethodsReturningImmutableCollections method visitCode.

/**
 * overrides the visitor to reset the stack for the new method, then checks if the immutability field is set to immutable and if so reports it
 *
 * @param obj
 *            the context object of the currently parsed method
 */
@Override
public void visitCode(Code obj) {
    try {
        String signature = SignatureUtils.getReturnSignature(getMethod().getSignature());
        if (signature.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX) && CollectionUtils.isListSetMap(SignatureUtils.stripSignature(signature))) {
            stack.resetForMethodEntry(this);
            imType = ImmutabilityType.UNKNOWN;
            super.visitCode(obj);
            if ((imType == ImmutabilityType.IMMUTABLE) || (imType == ImmutabilityType.POSSIBLY_IMMUTABLE)) {
                Method m = getMethod();
                Statistics.getStatistics().addImmutabilityStatus(clsName, m.getName(), m.getSignature(), imType);
            }
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    }
}
Also used : Method(org.apache.bcel.classfile.Method)

Aggregations

Method (org.apache.bcel.classfile.Method)79 JavaClass (org.apache.bcel.classfile.JavaClass)28 BugInstance (edu.umd.cs.findbugs.BugInstance)20 ToString (com.mebigfatguy.fbcontrib.utils.ToString)12 StopOpcodeParsingException (com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException)11 FQMethod (com.mebigfatguy.fbcontrib.utils.FQMethod)7 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7 Field (org.apache.bcel.classfile.Field)6 Type (org.apache.bcel.generic.Type)6 AnnotationEntry (org.apache.bcel.classfile.AnnotationEntry)5 ExceptionTable (org.apache.bcel.classfile.ExceptionTable)5 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)4 SourceLineAnnotation (edu.umd.cs.findbugs.SourceLineAnnotation)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 ConstantPoolGen (org.apache.bcel.generic.ConstantPoolGen)4 BugType (com.mebigfatguy.fbcontrib.utils.BugType)3 QMethod (com.mebigfatguy.fbcontrib.utils.QMethod)3 XMethod (edu.umd.cs.findbugs.ba.XMethod)3