Search in sources :

Example 31 with Method

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

the class JPAIssues method catalogFieldOrMethod.

/**
 * parses a field or method for spring-tx or jpa annotations
 *
 * @param fm
 *            the currently parsed field or method
 */
private void catalogFieldOrMethod(FieldOrMethod fm) {
    for (AnnotationEntry entry : fm.getAnnotationEntries()) {
        String type = entry.getAnnotationType();
        switch(type) {
            case "Lorg/springframework/transaction/annotation/Transactional;":
                if (fm instanceof Method) {
                    boolean isWrite = true;
                    for (ElementValuePair pair : entry.getElementValuePairs()) {
                        if ("readOnly".equals(pair.getNameString())) {
                            isWrite = "false".equals(pair.getValue().stringifyValue());
                            break;
                        }
                    }
                    transactionalMethods.put(new FQMethod(cls.getClassName(), fm.getName(), fm.getSignature()), isWrite ? TransactionalType.WRITE : TransactionalType.READ);
                }
                break;
            case "Ljavax/persistence/Id;":
                hasId = true;
                break;
            case "Ljavax/persistence/GeneratedValue;":
                hasGeneratedValue = true;
                break;
            case "Ljavax/persistence/OneToMany;":
                for (ElementValuePair pair : entry.getElementValuePairs()) {
                    if ("fetch".equals(pair.getNameString()) && "EAGER".equals(pair.getValue().stringifyValue())) {
                        hasEagerOneToMany = true;
                        break;
                    }
                }
                break;
            case "Lorg/hibernate/annotations/Fetch;":
            case "Lorg/eclipse/persistence/annotations/JoinFetch;":
            case "Lorg/eclipse/persistence/annotations/BatchFetch;":
                hasFetch = true;
                break;
            default:
                break;
        }
    }
}
Also used : AnnotationEntry(org.apache.bcel.classfile.AnnotationEntry) ElementValuePair(org.apache.bcel.classfile.ElementValuePair) FQMethod(com.mebigfatguy.fbcontrib.utils.FQMethod) FieldOrMethod(org.apache.bcel.classfile.FieldOrMethod) Method(org.apache.bcel.classfile.Method) FQMethod(com.mebigfatguy.fbcontrib.utils.FQMethod)

Example 32 with Method

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

the class ListIndexedIterating method visitCode.

/**
 * overrides the visitor to reset the opcode stack
 *
 * @param obj
 *            the code object for the currently parsed Code
 */
@Override
public void visitCode(final Code obj) {
    Method m = getMethod();
    if (prescreen(m)) {
        sawListSize = false;
        stack.resetForMethodEntry(this);
        state = State.SAW_NOTHING;
        stage = Stage.FIND_LOOP_STAGE;
        super.visitCode(obj);
        if (sawListSize && !possibleForLoops.isEmpty()) {
            stack.resetForMethodEntry(this);
            state = State.SAW_NOTHING;
            stage = Stage.FIND_BUG_STAGE;
            super.visitCode(obj);
        }
    }
}
Also used : Method(org.apache.bcel.classfile.Method) XMethod(edu.umd.cs.findbugs.ba.XMethod)

Example 33 with Method

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

the class ConfusingFunctionSemantics method visitCode.

/**
 * implements the visitor to look for any non-immutable typed parameters are assignable to the return type. If found, the method is parsed.
 *
 * @param obj
 *            the context object of the currently parsed code block
 */
@Override
public void visitCode(Code obj) {
    try {
        possibleParmRegs.clear();
        Method m = getMethod();
        String methodSignature = m.getSignature();
        String retSignature = SignatureUtils.getReturnSignature(methodSignature);
        JavaClass returnClass = null;
        int[] parmRegs = null;
        if (retSignature.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX) && !knownImmutables.contains(retSignature)) {
            List<String> parmTypes = SignatureUtils.getParameterSignatures(methodSignature);
            for (int p = 0; p < parmTypes.size(); p++) {
                String parmSignature = parmTypes.get(p);
                if (parmSignature.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX) && !knownImmutables.contains(parmSignature)) {
                    if (returnClass == null) {
                        returnClass = Repository.lookupClass(SignatureUtils.trimSignature(retSignature));
                        parmRegs = RegisterUtils.getParameterRegisters(m);
                    }
                    if (parmRegs != null) {
                        JavaClass parmClass = Repository.lookupClass(SignatureUtils.stripSignature(parmSignature));
                        if (parmClass.instanceOf(returnClass)) {
                            possibleParmRegs.put(Integer.valueOf(parmRegs[p]), new ParmUsage());
                        }
                    }
                }
            }
            if (!possibleParmRegs.isEmpty()) {
                try {
                    stack.resetForMethodEntry(this);
                    super.visitCode(obj);
                    for (ParmUsage pu : possibleParmRegs.values()) {
                        if ((pu.returnPC >= 0) && (pu.alteredPC >= 0) && (pu.returnPC > pu.alteredPC)) {
                            bugReporter.reportBug(new BugInstance(this, BugType.CFS_CONFUSING_FUNCTION_SEMANTICS.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this, pu.returnPC).addSourceLine(this, pu.alteredPC));
                        }
                    }
                } catch (StopOpcodeParsingException e) {
                // no parm regs left
                }
            }
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) StopOpcodeParsingException(com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException) BugInstance(edu.umd.cs.findbugs.BugInstance) Method(org.apache.bcel.classfile.Method) ToString(com.mebigfatguy.fbcontrib.utils.ToString)

Example 34 with Method

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

the class CopiedOverriddenMethod method visitClassContext.

/**
 * overrides the visitor to accept classes derived from non java.lang.Object classes.
 *
 * @param clsContext
 *            the context object of the currently parsed class
 */
@Override
public void visitClassContext(ClassContext clsContext) {
    try {
        JavaClass cls = clsContext.getJavaClass();
        String superName = cls.getSuperclassName();
        if (!Values.DOTTED_JAVA_LANG_OBJECT.equals(superName)) {
            this.classContext = clsContext;
            superclassCode = new HashMap<>();
            JavaClass superCls = cls.getSuperClass();
            childPoolGen = new ConstantPoolGen(cls.getConstantPool());
            parentPoolGen = new ConstantPoolGen(superCls.getConstantPool());
            Method[] methods = superCls.getMethods();
            for (Method m : methods) {
                String methodName = m.getName();
                if (m.isPublic() && !m.isAbstract() && !m.isSynthetic() && !Values.CONSTRUCTOR.equals(methodName) && !Values.STATIC_INITIALIZER.equals(methodName)) {
                    String methodInfo = methodName + ':' + m.getSignature();
                    superclassCode.put(methodInfo, new CodeInfo(m.getCode(), m.getAccessFlags()));
                }
            }
            cls.accept(this);
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    } finally {
        superclassCode = null;
        this.classContext = null;
        childPoolGen = null;
        parentPoolGen = null;
    }
}
Also used : ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) JavaClass(org.apache.bcel.classfile.JavaClass) ToString(com.mebigfatguy.fbcontrib.utils.ToString) Method(org.apache.bcel.classfile.Method)

Example 35 with Method

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

the class NonOwnedSynchronization method visitCode.

/**
 * implements the visitor to reset the opcode stack
 *
 * @param obj
 *            the context object of the currently parsed code block
 */
@Override
public void visitCode(Code obj) {
    Method method = getMethod();
    if (prescreen(method)) {
        stack.resetForMethodEntry(this);
        regPriorities.clear();
        int[] parmRegs = RegisterUtils.getParameterRegisters(method);
        for (int reg : parmRegs) {
            regPriorities.put(Integer.valueOf(reg), Values.NORMAL_BUG_PRIORITY);
        }
        if (!method.isStatic()) {
            regPriorities.put(Values.ZERO, Values.LOW_BUG_PRIORITY);
        }
        super.visitCode(obj);
    }
}
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