Search in sources :

Example 61 with Method

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

the class UnusedParameter method visitCode.

/**
 * implements the visitor to clear the parm set, and check for potential methods
 *
 * @param obj
 *            the context object of the currently parsed code block
 */
@Override
public void visitCode(Code obj) {
    unusedParms.clear();
    regToParm.clear();
    stack.resetForMethodEntry(this);
    Method m = getMethod();
    String methodName = m.getName();
    if (IGNORE_METHODS.contains(methodName)) {
        return;
    }
    int accessFlags = m.getAccessFlags();
    if (((accessFlags & (Const.ACC_STATIC | Const.ACC_PRIVATE)) == 0) || ((accessFlags & Const.ACC_SYNTHETIC) != 0)) {
        return;
    }
    List<String> parmTypes = SignatureUtils.getParameterSignatures(m.getSignature());
    if (parmTypes.isEmpty()) {
        return;
    }
    int firstReg = 0;
    if ((accessFlags & Const.ACC_STATIC) == 0) {
        ++firstReg;
    }
    int reg = firstReg;
    for (int i = 0; i < parmTypes.size(); ++i) {
        unusedParms.set(reg);
        regToParm.put(Integer.valueOf(reg), Integer.valueOf(i + 1));
        String parmSig = parmTypes.get(i);
        reg += SignatureUtils.getSignatureSize(parmSig);
    }
    try {
        super.visitCode(obj);
        if (!unusedParms.isEmpty()) {
            LocalVariableTable lvt = m.getLocalVariableTable();
            reg = unusedParms.nextSetBit(firstReg);
            while (reg >= 0) {
                LocalVariable lv = (lvt == null) ? null : lvt.getLocalVariable(reg, 0);
                if (lv != null) {
                    String parmName = lv.getName();
                    bugReporter.reportBug(new BugInstance(this, BugType.UP_UNUSED_PARAMETER.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addString("Parameter " + regToParm.get(Integer.valueOf(reg)) + ": " + parmName));
                }
                reg = unusedParms.nextSetBit(reg + 1);
            }
        }
    } catch (StopOpcodeParsingException e) {
    // no unusedParms left
    }
}
Also used : LocalVariableTable(org.apache.bcel.classfile.LocalVariableTable) StopOpcodeParsingException(com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException) LocalVariable(org.apache.bcel.classfile.LocalVariable) BugInstance(edu.umd.cs.findbugs.BugInstance) Method(org.apache.bcel.classfile.Method)

Example 62 with Method

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

the class UseVarArgs method hasMethod.

/**
 * looks to see if a class has a method with a specific name and signature
 *
 * @param c
 *            the class to check
 * @param candidateMethod
 *            the method to look for
 *
 * @return whether this class has the exact method
 */
private static boolean hasMethod(JavaClass c, Method candidateMethod) {
    String name = candidateMethod.getName();
    String sig = candidateMethod.getSignature();
    for (Method method : c.getMethods()) {
        if (!method.isStatic() && method.getName().equals(name) && method.getSignature().equals(sig)) {
            return true;
        }
    }
    return false;
}
Also used : Method(org.apache.bcel.classfile.Method)

Example 63 with Method

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

the class JPAIssues method catalogClass.

/**
 * parses the current class for spring-tx and jpa annotations, as well as hashCode and equals methods.
 *
 * @param cls
 *            the currently parsed class
 */
private void catalogClass(JavaClass cls) {
    transactionalMethods = new HashMap<>();
    isEntity = false;
    hasId = false;
    hasGeneratedValue = false;
    hasEagerOneToMany = false;
    hasHCEquals = false;
    for (AnnotationEntry entry : cls.getAnnotationEntries()) {
        if ("Ljavax/persistence/Entity;".equals(entry.getAnnotationType())) {
            isEntity = true;
            break;
        }
    }
    for (Method m : cls.getMethods()) {
        catalogFieldOrMethod(m);
        if (("equals".equals(m.getName()) && SignatureBuilder.SIG_OBJECT_TO_BOOLEAN.equals(m.getSignature())) || (Values.HASHCODE.equals(m.getName()) && SignatureBuilder.SIG_VOID_TO_INT.equals(m.getSignature()))) {
            hasHCEquals = true;
        }
    }
    for (Field f : cls.getFields()) {
        catalogFieldOrMethod(f);
    }
}
Also used : Field(org.apache.bcel.classfile.Field) AnnotationEntry(org.apache.bcel.classfile.AnnotationEntry) FieldOrMethod(org.apache.bcel.classfile.FieldOrMethod) Method(org.apache.bcel.classfile.Method) FQMethod(com.mebigfatguy.fbcontrib.utils.FQMethod)

Example 64 with Method

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

the class JPAIssues method visitCode.

/**
 * implements the visitor to reset the opcode stack, Note that the synthetic check is done in both visitMethod and visitCode as visitMethod is not a proper
 * listener stopping method. We don't want to report issues reported in visitMethod if it is synthetic, but we also don't want it to get into sawOpcode, so
 * that is why it is done here as well.
 *
 * @param obj
 *            the currently parsed code block
 */
@Override
public void visitCode(Code obj) {
    Method m = getMethod();
    if (m.isSynthetic()) {
        return;
    }
    isPublic = m.isPublic();
    stack.resetForMethodEntry(this);
    super.visitCode(obj);
}
Also used : FieldOrMethod(org.apache.bcel.classfile.FieldOrMethod) Method(org.apache.bcel.classfile.Method) FQMethod(com.mebigfatguy.fbcontrib.utils.FQMethod)

Example 65 with Method

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

the class ConfusingAutoboxedOverloading method populateMethodInfo.

/**
 * fills out a set of method details for possibly confusing method signatures
 *
 * @param cls
 *            the current class being parsed
 * @param methodInfo
 *            a collection to hold possibly confusing methods
 */
private void populateMethodInfo(JavaClass cls, Map<String, Set<String>> methodInfo) {
    try {
        if (Values.DOTTED_JAVA_LANG_OBJECT.equals(cls.getClassName())) {
            return;
        }
        Method[] methods = cls.getMethods();
        for (Method m : methods) {
            String sig = m.getSignature();
            if (isPossiblyConfusingSignature(sig)) {
                String name = m.getName();
                Set<String> sigs = methodInfo.get(name);
                if (sigs == null) {
                    sigs = new HashSet<>(3);
                    methodInfo.put(name, sigs);
                }
                sigs.add(m.getSignature());
            }
        }
        populateMethodInfo(cls.getSuperClass(), methodInfo);
    } 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