Search in sources :

Example 11 with AnnotationEntry

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

the class WiringIssues method loadParentAutowireds.

/**
 * loads all the types that are injected by @Autowired annotations in super classes
 *
 * @param cls
 *            the class who's parents you want to load
 * @param wiredFields
 *            the collected map of autowired types
 * @throws ClassNotFoundException
 *             if a parent class can't be loaded
 */
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(value = "SF_SWITCH_NO_DEFAULT", justification = "Only a few cases need special handling")
private void loadParentAutowireds(JavaClass cls, Map<WiringType, FieldAnnotation> wiredFields) throws ClassNotFoundException {
    if (Values.DOTTED_JAVA_LANG_OBJECT.equals(cls.getClassName())) {
        return;
    }
    loadParentAutowireds(cls.getSuperClass(), wiredFields);
    Field[] fields = cls.getFields();
    if (fields.length > 0) {
        for (Field field : fields) {
            boolean hasAutowired = false;
            String qualifier = "";
            for (AnnotationEntry entry : field.getAnnotationEntries()) {
                switch(entry.getAnnotationType()) {
                    case SPRING_AUTOWIRED:
                        hasAutowired = true;
                        break;
                    case SPRING_QUALIFIER:
                        qualifier = entry.getElementValuePairs()[0].getValue().stringifyValue();
                        break;
                }
            }
            if (hasAutowired) {
                WiringType wt = new WiringType(field.getSignature(), field.getGenericSignature(), qualifier);
                wiredFields.put(wt, FieldAnnotation.fromBCELField(cls.getClassName(), field));
            }
        }
    }
}
Also used : Field(org.apache.bcel.classfile.Field) AnnotationEntry(org.apache.bcel.classfile.AnnotationEntry) ToString(com.mebigfatguy.fbcontrib.utils.ToString)

Example 12 with AnnotationEntry

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

the class JAXRSIssues method processJAXRSMethod.

private void processJAXRSMethod(Method m, String path, boolean hasConsumes) {
    Type[] parmTypes = m.getArgumentTypes();
    int numParms = parmTypes.length;
    if (numParms > 0) {
        boolean sawBareParm = false;
        ParameterAnnotationEntry[] pes = m.getParameterAnnotationEntries();
        int parmIndex = 0;
        for (ParameterAnnotationEntry pe : pes) {
            boolean foundParamAnnotation = false;
            for (AnnotationEntry a : pe.getAnnotationEntries()) {
                String annotationType = a.getAnnotationType();
                if (PARAM_ANNOTATIONS.contains(annotationType)) {
                    foundParamAnnotation = true;
                    if ((path != null) && "Ljavax/ws/rs/PathParam;".equals(annotationType)) {
                        String parmPath = getDefaultAnnotationValue(a);
                        if ((parmPath != null) && (!path.matches(".*\\{" + parmPath + "\\b.*"))) {
                            bugReporter.reportBug(new BugInstance(this, BugType.JXI_PARM_PARAM_NOT_FOUND_IN_PATH.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addString("Path param: " + parmPath));
                        }
                    } else if ("Ljavax/ws/rs/core/Context;".equals(annotationType)) {
                        String parmSig = parmTypes[parmIndex].getSignature();
                        if (!VALID_CONTEXT_TYPES.contains(parmSig)) {
                            bugReporter.reportBug(new BugInstance(this, BugType.JXI_INVALID_CONTEXT_PARAMETER_TYPE.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addString("Parameter signature: " + parmSig));
                        }
                    }
                }
            }
            if (!foundParamAnnotation) {
                if ((!sawBareParm) && (hasConsumes || NATIVE_JAXRS_TYPES.contains(parmTypes[parmIndex].getSignature()))) {
                    sawBareParm = true;
                } else {
                    bugReporter.reportBug(new BugInstance(this, BugType.JXI_UNDEFINED_PARAMETER_SOURCE_IN_ENDPOINT.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addString("Parameter " + (parmIndex + 1)));
                    break;
                }
            }
            parmIndex++;
        }
    }
}
Also used : BugType(com.mebigfatguy.fbcontrib.utils.BugType) Type(org.apache.bcel.generic.Type) ParameterAnnotationEntry(org.apache.bcel.classfile.ParameterAnnotationEntry) AnnotationEntry(org.apache.bcel.classfile.AnnotationEntry) BugInstance(edu.umd.cs.findbugs.BugInstance) ParameterAnnotationEntry(org.apache.bcel.classfile.ParameterAnnotationEntry)

Example 13 with AnnotationEntry

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

the class JAXRSIssues method visitClassContext.

@Override
public void visitClassContext(ClassContext classContext) {
    JavaClass cls = classContext.getJavaClass();
    pathOnClass = "";
    hasClassConsumes = false;
    for (AnnotationEntry entry : cls.getAnnotationEntries()) {
        if ("Ljavax/ws/rs/Consumes;".equals(entry.getAnnotationType())) {
            hasClassConsumes = true;
        } else if ("Ljavax/ws/rs/Path;".equals(entry.getAnnotationType())) {
            pathOnClass = getDefaultAnnotationValue(entry);
        }
    }
    cls.accept(this);
}
Also used : ParameterAnnotationEntry(org.apache.bcel.classfile.ParameterAnnotationEntry) AnnotationEntry(org.apache.bcel.classfile.AnnotationEntry) JavaClass(org.apache.bcel.classfile.JavaClass)

Example 14 with AnnotationEntry

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

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

the class CollectStatistics method visitClassContext.

/**
 * implements the visitor to collect statistics on this class
 *
 * @param classContext
 *            the currently class
 */
@Override
public void visitClassContext(ClassContext classContext) {
    try {
        JavaClass cls = classContext.getJavaClass();
        AnnotationEntry[] annotations = cls.getAnnotationEntries();
        classHasAnnotation = !CollectionUtils.isEmpty(annotations);
        stack = new OpcodeStack();
        selfCallTree = new HashMap<>();
        super.visitClassContext(classContext);
        performModifyStateClosure(classContext.getJavaClass());
    } finally {
        stack = null;
        selfCallTree = null;
        curMethod = null;
    }
}
Also used : AnnotationEntry(org.apache.bcel.classfile.AnnotationEntry) JavaClass(org.apache.bcel.classfile.JavaClass) OpcodeStack(edu.umd.cs.findbugs.OpcodeStack)

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