Search in sources :

Example 66 with Method

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

the class ConfusingAutoboxedOverloading method visitClassContext.

/**
 * overrides the visitor to look for confusing signatures
 *
 * @param classContext
 *            the context object that holds the JavaClass currently being parsed
 */
@Override
public void visitClassContext(ClassContext classContext) {
    JavaClass cls = classContext.getJavaClass();
    if (cls.isClass() && (cls.getMajor() >= JDK15_MAJOR)) {
        Map<String, Set<String>> methodInfo = new HashMap<>();
        populateMethodInfo(cls, methodInfo);
        Method[] methods = cls.getMethods();
        for (Method m : methods) {
            String name = m.getName();
            String signature = m.getSignature();
            Set<String> sigs = methodInfo.get(name);
            if (sigs != null) {
                for (String sig : sigs) {
                    if (confusingSignatures(sig, signature)) {
                        bugReporter.reportBug(new BugInstance(this, BugType.CAO_CONFUSING_AUTOBOXED_OVERLOADING.name(), NORMAL_PRIORITY).addClass(cls.getClassName()).addString(name + signature).addString(name + sig));
                    }
                }
            }
        }
    }
}
Also used : Set(java.util.Set) UnmodifiableSet(com.mebigfatguy.fbcontrib.utils.UnmodifiableSet) HashSet(java.util.HashSet) JavaClass(org.apache.bcel.classfile.JavaClass) HashMap(java.util.HashMap) BugInstance(edu.umd.cs.findbugs.BugInstance) Method(org.apache.bcel.classfile.Method)

Example 67 with Method

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

the class CopiedOverriddenMethod method visitCode.

/**
 * overrides the visitor to find code blocks of methods that are the same as its parents
 *
 * @param obj
 *            the code object of the currently parsed method
 */
@Override
public void visitCode(Code obj) {
    try {
        Method m = getMethod();
        if ((!m.isPublic() && !m.isProtected()) || m.isAbstract() || m.isSynthetic()) {
            return;
        }
        CodeInfo superCode = superclassCode.remove(curMethodInfo);
        if (superCode != null) {
            if (sameAccess(getMethod().getAccessFlags(), superCode.getAccess()) && codeEquals(obj, superCode.getCode())) {
                bugReporter.reportBug(new BugInstance(this, BugType.COM_COPIED_OVERRIDDEN_METHOD.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(classContext, this, getPC()));
                return;
            }
            if ((getMethod().getAccessFlags() & Const.ACC_SYNCHRONIZED) != (superCode.getAccess() & Const.ACC_SYNCHRONIZED)) {
                return;
            }
            parmTypes = getMethod().getArgumentTypes();
            nextParmIndex = 0;
            nextParmOffset = getMethod().isStatic() ? 0 : 1;
            sawAload0 = nextParmOffset == 0;
            sawParentCall = false;
            super.visitCode(obj);
        }
    } catch (StopOpcodeParsingException e) {
    // method is unique
    }
}
Also used : StopOpcodeParsingException(com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException) BugInstance(edu.umd.cs.findbugs.BugInstance) Method(org.apache.bcel.classfile.Method)

Example 68 with Method

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

the class LoggerOddities method visitCode.

/**
 * implements the visitor to reset the stack
 *
 * @param obj
 *            the context object of the currently parsed code block
 */
@Override
public void visitCode(Code obj) {
    stack.resetForMethodEntry(this);
    Method m = getMethod();
    if (Values.CONSTRUCTOR.equals(m.getName())) {
        for (String parmSig : SignatureUtils.getParameterSignatures(m.getSignature())) {
            if (SignatureUtils.classToSignature(SLF4J_LOGGER).equals(parmSig) || SignatureUtils.classToSignature(LOG4J_LOGGER).equals(parmSig) || SignatureUtils.classToSignature(LOG4J2_LOGGER).equals(parmSig) || SignatureUtils.classToSignature(COMMONS_LOGGER).equals(parmSig)) {
                bugReporter.reportBug(new BugInstance(this, BugType.LO_SUSPECT_LOG_PARAMETER.name(), NORMAL_PRIORITY).addClass(this).addMethod(this));
            }
        }
    }
    isStaticInitializer = Values.STATIC_INITIALIZER.equals(m.getName());
    super.visitCode(obj);
}
Also used : BugInstance(edu.umd.cs.findbugs.BugInstance) Method(org.apache.bcel.classfile.Method) XMethod(edu.umd.cs.findbugs.ba.XMethod) ToString(com.mebigfatguy.fbcontrib.utils.ToString)

Example 69 with Method

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

the class MethodReturnsConstant method visitCode.

/**
 * implements the visitor to reset the stack and proceed for private methods
 *
 * @param obj
 *            the context object of the currently parsed code block
 */
@Override
public void visitCode(Code obj) {
    Method m = getMethod();
    if (overloadedMethods.contains(m)) {
        return;
    }
    int aFlags = m.getAccessFlags();
    if ((((aFlags & Const.ACC_PRIVATE) != 0) || ((aFlags & Const.ACC_STATIC) != 0)) && ((aFlags & Const.ACC_SYNTHETIC) == 0) && (!m.getSignature().endsWith(")Z"))) {
        stack.resetForMethodEntry(this);
        returnRegister = Values.NEGATIVE_ONE;
        returnConstant = null;
        registerConstants.clear();
        returnPC = -1;
        try {
            super.visitCode(obj);
            if ((returnConstant != null)) {
                BugInstance bi = new BugInstance(this, BugType.MRC_METHOD_RETURNS_CONSTANT.name(), ((aFlags & Const.ACC_PRIVATE) != 0) ? NORMAL_PRIORITY : LOW_PRIORITY).addClass(this).addMethod(this);
                if (returnPC >= 0) {
                    bi.addSourceLine(this, returnPC);
                }
                bi.addString(returnConstant.toString());
                bugReporter.reportBug(bi);
            }
        } catch (StopOpcodeParsingException e) {
        // method was not suspect
        }
    }
}
Also used : StopOpcodeParsingException(com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException) BugInstance(edu.umd.cs.findbugs.BugInstance) Method(org.apache.bcel.classfile.Method)

Example 70 with Method

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

the class AbnormalFinallyBlockReturn method sawOpcode.

/**
 * overrides the visitor to find return/exceptions from the finally block.
 *
 * @param seen
 *            the opcode that is being visited
 */
@Override
public void sawOpcode(int seen) {
    FinallyBlockInfo fbi = fbInfo.get(0);
    if (getPC() < fbi.startPC) {
        return;
    }
    if (getPC() == fbi.startPC) {
        if (OpcodeUtils.isAStore(seen)) {
            fbi.exReg = RegisterUtils.getAStoreReg(this, seen);
        } else {
            removeEarliestFinallyBlock();
            sawOpcode(seen);
            return;
        }
        return;
    }
    if (seen == Const.MONITORENTER) {
        fbi.monitorCount++;
    } else if (seen == Const.MONITOREXIT) {
        fbi.monitorCount--;
        if (fbi.monitorCount < 0) {
            removeEarliestFinallyBlock();
            sawOpcode(seen);
            return;
        }
    }
    if ((seen == Const.ATHROW) && (loadedReg == fbi.exReg)) {
        removeEarliestFinallyBlock();
        sawOpcode(seen);
        return;
    } else if (OpcodeUtils.isALoad(seen)) {
        loadedReg = RegisterUtils.getALoadReg(this, seen);
    } else {
        loadedReg = -1;
    }
    if (OpcodeUtils.isReturn(seen) || (seen == Const.ATHROW)) {
        bugReporter.reportBug(new BugInstance(this, BugType.AFBR_ABNORMAL_FINALLY_BLOCK_RETURN.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
        removeEarliestFinallyBlock();
    } else if (OpcodeUtils.isStandardInvoke(seen)) {
        try {
            JavaClass cls = Repository.lookupClass(getClassConstantOperand());
            Method m = findMethod(cls, getNameConstantOperand(), getSigConstantOperand());
            if (m != null) {
                ExceptionTable et = m.getExceptionTable();
                if ((et != null) && (et.getLength() > 0) && !catchBlockInFinally(fbi)) {
                    bugReporter.reportBug(new BugInstance(this, BugType.AFBR_ABNORMAL_FINALLY_BLOCK_RETURN.name(), LOW_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                    removeEarliestFinallyBlock();
                }
            }
        } catch (ClassNotFoundException cnfe) {
            bugReporter.reportMissingClass(cnfe);
        }
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) BugInstance(edu.umd.cs.findbugs.BugInstance) ExceptionTable(org.apache.bcel.classfile.ExceptionTable) 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