Search in sources :

Example 1 with AnnotationEntry

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

the class FieldCouldBeLocal method visitClassContext.

/**
 * overrides the visitor to collect localizable fields, and then report those that survive all method checks.
 *
 * @param classContext
 *            the context object that holds the JavaClass parsed
 */
@Override
public void visitClassContext(ClassContext classContext) {
    try {
        localizableFields = new HashMap<>();
        visitedBlocks = new BitSet();
        clsContext = classContext;
        clsName = clsContext.getJavaClass().getClassName();
        clsSig = SignatureUtils.classToSignature(clsName);
        JavaClass cls = classContext.getJavaClass();
        Field[] fields = cls.getFields();
        ConstantPool cp = classContext.getConstantPoolGen().getConstantPool();
        for (Field f : fields) {
            if (!f.isStatic() && !f.isVolatile() && (f.getName().indexOf(Values.SYNTHETIC_MEMBER_CHAR) < 0) && f.isPrivate()) {
                FieldAnnotation fa = new FieldAnnotation(cls.getClassName(), f.getName(), f.getSignature(), false);
                boolean hasExternalAnnotation = false;
                for (AnnotationEntry entry : f.getAnnotationEntries()) {
                    ConstantUtf8 cutf = (ConstantUtf8) cp.getConstant(entry.getTypeIndex());
                    if (!cutf.getBytes().startsWith(Values.JAVA)) {
                        hasExternalAnnotation = true;
                        break;
                    }
                }
                localizableFields.put(f.getName(), new FieldInfo(fa, hasExternalAnnotation));
            }
        }
        if (!localizableFields.isEmpty()) {
            buildMethodFieldModifiers(classContext);
            super.visitClassContext(classContext);
            for (FieldInfo fi : localizableFields.values()) {
                FieldAnnotation fa = fi.getFieldAnnotation();
                SourceLineAnnotation sla = fi.getSrcLineAnnotation();
                BugInstance bug = new BugInstance(this, BugType.FCBL_FIELD_COULD_BE_LOCAL.name(), NORMAL_PRIORITY).addClass(this).addField(fa);
                if (sla != null) {
                    bug.addSourceLine(sla);
                }
                bugReporter.reportBug(bug);
            }
        }
    } finally {
        localizableFields = null;
        visitedBlocks = null;
        clsContext = null;
        methodFieldModifiers = null;
    }
}
Also used : BitSet(java.util.BitSet) BugInstance(edu.umd.cs.findbugs.BugInstance) ConstantUtf8(org.apache.bcel.classfile.ConstantUtf8) Field(org.apache.bcel.classfile.Field) AnnotationEntry(org.apache.bcel.classfile.AnnotationEntry) JavaClass(org.apache.bcel.classfile.JavaClass) SourceLineAnnotation(edu.umd.cs.findbugs.SourceLineAnnotation) ConstantPool(org.apache.bcel.classfile.ConstantPool) FieldAnnotation(edu.umd.cs.findbugs.FieldAnnotation)

Example 2 with AnnotationEntry

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

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

the class UnsynchronizedSingletonFieldWrites method isIgnorableMethod.

/**
 * looks for methods that should not be scanned for fields writes for a variety of reasons
 * <ul>
 * <li>Constructor</li>
 * <li>Static Initializer</li>
 * <li>static method</li>
 * <li>Has a synchronized attribute</li>
 * <li>Has a @PostConstruct annotation</li>
 * <li>Has an @Autowired annotation</li>
 * </ul>
 *
 * @param m
 *            the method to check
 * @return if the method should not be scanned
 */
private boolean isIgnorableMethod(Method m) {
    if (m.isStatic() || m.isSynchronized()) {
        return true;
    }
    String name = m.getName();
    if (Values.CONSTRUCTOR.equals(name) || Values.STATIC_INITIALIZER.equals(name)) {
        return true;
    }
    AnnotationEntry[] annotations = m.getAnnotationEntries();
    if (CollectionUtils.isEmpty(annotations)) {
        return false;
    }
    for (AnnotationEntry annotation : annotations) {
        String type = annotation.getAnnotationType();
        if (IGNORABLE_METHOD_ANNOTATIONS.contains(type)) {
            return true;
        }
    }
    return false;
}
Also used : AnnotationEntry(org.apache.bcel.classfile.AnnotationEntry)

Example 4 with AnnotationEntry

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

the class UnitTestAssertionOddities method detectFrameworkType.

/**
 * Attempt to identify whether we are dealing with JUnit or TestNG.
 */
private void detectFrameworkType() {
    hasAnnotation = false;
    Method m = getMethod();
    if (isTestCaseDerived && m.getName().startsWith("test")) {
        frameworkType = TestFrameworkType.JUNIT;
        return;
    }
    frameworkType = TestFrameworkType.UNKNOWN;
    if (!isAnnotationCapable) {
        return;
    }
    AnnotationEntry[] annotations = m.getAnnotationEntries();
    if (annotations == null) {
        return;
    }
    for (AnnotationEntry annotation : annotations) {
        String annotationType = annotation.getAnnotationType();
        if (annotation.isRuntimeVisible()) {
            if (TEST_ANNOTATION_SIGNATURE.equals(annotationType)) {
                frameworkType = TestFrameworkType.JUNIT;
                hasAnnotation = true;
                return;
            } else if (TESTNG_ANNOTATION_SIGNATURE.equals(annotationType)) {
                frameworkType = TestFrameworkType.TESTNG;
                hasAnnotation = true;
                return;
            }
        }
    }
}
Also used : AnnotationEntry(org.apache.bcel.classfile.AnnotationEntry) Method(org.apache.bcel.classfile.Method)

Example 5 with AnnotationEntry

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

the class WiringIssues method visitCode.

@Override
public void visitCode(Code obj) {
    Method m = getMethod();
    for (AnnotationEntry annotation : m.getAnnotationEntries()) {
        String type = annotation.getAnnotationType();
        if (type.startsWith("Lorg/junit/") || type.startsWith("Lorg/testng/")) {
            return;
        }
    }
    stack.resetForMethodEntry(this);
    super.visitCode(obj);
}
Also used : AnnotationEntry(org.apache.bcel.classfile.AnnotationEntry) Method(org.apache.bcel.classfile.Method) ToString(com.mebigfatguy.fbcontrib.utils.ToString)

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