Search in sources :

Example 6 with AnnotationEntry

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

the class JAXRSIssues method visitMethod.

@Override
public void visitMethod(Method obj) {
    if (obj.isSynthetic()) {
        return;
    }
    String path = null;
    boolean isJAXRS = false;
    boolean hasGet = false;
    boolean hasConsumes = false;
    for (AnnotationEntry entry : obj.getAnnotationEntries()) {
        String annotationType = entry.getAnnotationType();
        switch(annotationType) {
            case "Ljavax/ws/rs/GET;":
                hasGet = true;
                isJAXRS = true;
                break;
            case "Ljavax/ws/rs/Consumes;":
                hasConsumes = true;
                break;
            case "Ljavax/ws/rs/Path;":
                path = getDefaultAnnotationValue(entry);
                break;
            default:
                // it is fine that GET is not captured here
                if (METHOD_ANNOTATIONS.contains(annotationType)) {
                    isJAXRS = true;
                }
                break;
        }
        if (hasGet && hasConsumes) {
            bugReporter.reportBug(new BugInstance(this, BugType.JXI_GET_ENDPOINT_CONSUMES_CONTENT.name(), NORMAL_PRIORITY).addClass(this).addMethod(this));
            break;
        }
    }
    if (isJAXRS) {
        processJAXRSMethod(obj, pathOnClass + path, hasConsumes || hasClassConsumes);
    }
}
Also used : ParameterAnnotationEntry(org.apache.bcel.classfile.ParameterAnnotationEntry) AnnotationEntry(org.apache.bcel.classfile.AnnotationEntry) BugInstance(edu.umd.cs.findbugs.BugInstance)

Example 7 with AnnotationEntry

use of org.apache.bcel.classfile.AnnotationEntry 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 8 with AnnotationEntry

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

the class JPAIssues method getAnnotatedRollbackExceptions.

/**
 * parses an spring-tx @Transactional annotations for rollbackFor/noRollbackfor attributes of a @Transactional annotation.
 *
 * @param method
 *            the currently parsed method
 *
 * @return the exception classes declared in the @Transactional annotation
 *
 * @throws ClassNotFoundException
 *             if exception classes are not found
 */
private Set<JavaClass> getAnnotatedRollbackExceptions(Method method) throws ClassNotFoundException {
    for (AnnotationEntry annotation : method.getAnnotationEntries()) {
        if ("Lorg/springframework/transaction/annotation/Transactional;".equals(annotation.getAnnotationType())) {
            if (annotation.getNumElementValuePairs() == 0) {
                return Collections.<JavaClass>emptySet();
            }
            Set<JavaClass> rollbackExceptions = new HashSet<>();
            for (ElementValuePair pair : annotation.getElementValuePairs()) {
                if ("rollbackFor".equals(pair.getNameString()) || "noRollbackFor".equals(pair.getNameString())) {
                    String exNames = pair.getValue().stringifyValue();
                    Matcher m = annotationClassPattern.matcher(exNames);
                    while (m.find()) {
                        String exName = m.group(1);
                        JavaClass exCls = Repository.lookupClass(SignatureUtils.trimSignature(exName));
                        if (!exCls.instanceOf(runtimeExceptionClass)) {
                            rollbackExceptions.add(exCls);
                        }
                    }
                }
            }
            return rollbackExceptions;
        }
    }
    return Collections.<JavaClass>emptySet();
}
Also used : AnnotationEntry(org.apache.bcel.classfile.AnnotationEntry) JavaClass(org.apache.bcel.classfile.JavaClass) ElementValuePair(org.apache.bcel.classfile.ElementValuePair) Matcher(java.util.regex.Matcher) HashSet(java.util.HashSet)

Example 9 with AnnotationEntry

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

the class OverlyConcreteParameter method visitMethod.

/**
 * implements the visitor to look to see if this method is constrained by a superclass or interface.
 *
 * @param obj
 *            the currently parsed method
 */
@Override
public void visitMethod(Method obj) {
    methodSignatureIsConstrained = false;
    String methodName = obj.getName();
    if (!Values.CONSTRUCTOR.equals(methodName) && !Values.STATIC_INITIALIZER.equals(methodName)) {
        String methodSig = obj.getSignature();
        methodSignatureIsConstrained = methodIsSpecial(methodName, methodSig) || methodHasSyntheticTwin(methodName, methodSig);
        if (!methodSignatureIsConstrained) {
            for (AnnotationEntry entry : obj.getAnnotationEntries()) {
                if (CONVERSION_ANNOTATIONS.contains(entry.getAnnotationType())) {
                    methodSignatureIsConstrained = true;
                    break;
                }
            }
        }
        if (!methodSignatureIsConstrained) {
            String parms = methodSig.split("\\(|\\)")[1];
            if (parms.indexOf(Values.SIG_QUALIFIED_CLASS_SUFFIX_CHAR) >= 0) {
                outer: for (JavaClass constrainCls : constrainingClasses) {
                    Method[] methods = constrainCls.getMethods();
                    for (Method m : methods) {
                        if (methodName.equals(m.getName()) && methodSig.equals(m.getSignature())) {
                            methodSignatureIsConstrained = true;
                            break outer;
                        }
                    }
                }
            }
        }
    }
}
Also used : AnnotationEntry(org.apache.bcel.classfile.AnnotationEntry) ParameterAnnotationEntry(org.apache.bcel.classfile.ParameterAnnotationEntry) JavaClass(org.apache.bcel.classfile.JavaClass) ToString(com.mebigfatguy.fbcontrib.utils.ToString) Method(org.apache.bcel.classfile.Method)

Example 10 with AnnotationEntry

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

the class WiringIssues method visitClassContext.

@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(value = "SF_SWITCH_NO_DEFAULT", justification = "Only a few cases need special handling")
@Override
public void visitClassContext(ClassContext classContext) {
    try {
        JavaClass cls = classContext.getJavaClass();
        Field[] fields = cls.getFields();
        if (fields.length > 0) {
            Map<WiringType, FieldAnnotation> wiredFields = new HashMap<>();
            boolean loadedParents = false;
            for (Field field : fields) {
                boolean hasAutowired = false;
                String qualifier = "";
                for (AnnotationEntry entry : field.getAnnotationEntries()) {
                    switch(entry.getAnnotationType()) {
                        case SPRING_AUTOWIRED:
                            if (!loadedParents) {
                                loadParentAutowireds(cls.getSuperClass(), wiredFields);
                                loadedParents = true;
                            }
                            hasAutowired = true;
                            break;
                        case SPRING_QUALIFIER:
                            qualifier = entry.getElementValuePairs()[0].getValue().stringifyValue();
                            break;
                    }
                }
                if (hasAutowired) {
                    WiringType wt = new WiringType(field.getSignature(), field.getGenericSignature(), qualifier);
                    FieldAnnotation existingAnnotation = wiredFields.get(wt);
                    if (existingAnnotation == null) {
                        wiredFields.put(wt, FieldAnnotation.fromBCELField(cls.getClassName(), field));
                    } else {
                        bugReporter.reportBug(new BugInstance(this, BugType.WI_DUPLICATE_WIRED_TYPES.name(), NORMAL_PRIORITY).addClass(cls).addField(FieldAnnotation.fromBCELField(cls, field)).addField(existingAnnotation));
                        wiredFields.remove(wt);
                    }
                }
            }
        }
        stack = new OpcodeStack();
        super.visitClassContext(classContext);
    } catch (ClassNotFoundException e) {
        bugReporter.reportMissingClass(e);
    } finally {
        stack = null;
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) HashMap(java.util.HashMap) BugInstance(edu.umd.cs.findbugs.BugInstance) ToString(com.mebigfatguy.fbcontrib.utils.ToString) Field(org.apache.bcel.classfile.Field) AnnotationEntry(org.apache.bcel.classfile.AnnotationEntry) JavaClass(org.apache.bcel.classfile.JavaClass) FieldAnnotation(edu.umd.cs.findbugs.FieldAnnotation)

Aggregations

AnnotationEntry (org.apache.bcel.classfile.AnnotationEntry)15 JavaClass (org.apache.bcel.classfile.JavaClass)6 Method (org.apache.bcel.classfile.Method)5 ToString (com.mebigfatguy.fbcontrib.utils.ToString)4 BugInstance (edu.umd.cs.findbugs.BugInstance)4 Field (org.apache.bcel.classfile.Field)4 ParameterAnnotationEntry (org.apache.bcel.classfile.ParameterAnnotationEntry)4 ElementValuePair (org.apache.bcel.classfile.ElementValuePair)3 FQMethod (com.mebigfatguy.fbcontrib.utils.FQMethod)2 FieldAnnotation (edu.umd.cs.findbugs.FieldAnnotation)2 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)2 FieldOrMethod (org.apache.bcel.classfile.FieldOrMethod)2 BugType (com.mebigfatguy.fbcontrib.utils.BugType)1 SourceLineAnnotation (edu.umd.cs.findbugs.SourceLineAnnotation)1 BitSet (java.util.BitSet)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Matcher (java.util.regex.Matcher)1 ConstantPool (org.apache.bcel.classfile.ConstantPool)1 ConstantUtf8 (org.apache.bcel.classfile.ConstantUtf8)1