Search in sources :

Example 1 with ElementValuePair

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

the class UnsynchronizedSingletonFieldWrites method isSingleton.

private boolean isSingleton(JavaClass cls) {
    if (cls.isEnum()) {
        return true;
    }
    AnnotationEntry[] annotations = cls.getAnnotationEntries();
    if (CollectionUtils.isEmpty(annotations)) {
        return false;
    }
    boolean isSpringBean = false;
    for (AnnotationEntry annotation : annotations) {
        String type = annotation.getAnnotationType();
        if (SPRING_CLASS_ANNOTATIONS.contains(type)) {
            isSpringBean = true;
        } else if (SPRING_SCOPE_ANNOTATION.equals(type)) {
            ElementValuePair[] pairs = annotation.getElementValuePairs();
            if (!CollectionUtils.isEmpty(pairs)) {
                for (ElementValuePair pair : pairs) {
                    String propName = pair.getNameString();
                    if ("value".equals(propName) || "scopeName".equals(propName)) {
                        ElementValue value = pair.getValue();
                        return "singleton".equals(value.stringifyValue());
                    }
                }
            }
        }
    }
    return isSpringBean;
}
Also used : AnnotationEntry(org.apache.bcel.classfile.AnnotationEntry) ElementValuePair(org.apache.bcel.classfile.ElementValuePair) ElementValue(org.apache.bcel.classfile.ElementValue)

Example 2 with ElementValuePair

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

use of org.apache.bcel.classfile.ElementValuePair 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)

Aggregations

AnnotationEntry (org.apache.bcel.classfile.AnnotationEntry)3 ElementValuePair (org.apache.bcel.classfile.ElementValuePair)3 FQMethod (com.mebigfatguy.fbcontrib.utils.FQMethod)1 HashSet (java.util.HashSet)1 Matcher (java.util.regex.Matcher)1 ElementValue (org.apache.bcel.classfile.ElementValue)1 FieldOrMethod (org.apache.bcel.classfile.FieldOrMethod)1 JavaClass (org.apache.bcel.classfile.JavaClass)1 Method (org.apache.bcel.classfile.Method)1