Search in sources :

Example 16 with Field

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

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

the class DubiousListCollection method visitClassContext.

/**
 * overrides the visitor to accept classes that define List based fields
 *
 * @param classContext
 *            the context object for the currently parsed class
 */
@Override
public void visitClassContext(final ClassContext classContext) {
    JavaClass cls = classContext.getJavaClass();
    Field[] flds = cls.getFields();
    for (Field f : flds) {
        String sig = f.getSignature();
        if (sig.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX)) {
            if (sig.startsWith("Ljava/util/") && sig.endsWith("List;")) {
                fieldsReported.put(f.getName(), new FieldInfo());
            }
        }
    }
    if (!fieldsReported.isEmpty()) {
        super.visitClassContext(classContext);
        reportBugs();
    }
}
Also used : Field(org.apache.bcel.classfile.Field) XField(edu.umd.cs.findbugs.ba.XField) JavaClass(org.apache.bcel.classfile.JavaClass) ToString(com.mebigfatguy.fbcontrib.utils.ToString)

Example 18 with Field

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

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

the class LocalHangingExecutor method parseFieldsForHangingCandidates.

private void parseFieldsForHangingCandidates(ClassContext classContext) {
    JavaClass cls = classContext.getJavaClass();
    Field[] fields = cls.getFields();
    for (Field f : fields) {
        String sig = f.getSignature();
        if (hangableSig.contains(sig)) {
            hangingFieldCandidates.put(XFactory.createXField(cls.getClassName(), f.getName(), f.getSignature(), f.isStatic()), new AnnotationPriority(FieldAnnotation.fromBCELField(cls, f), NORMAL_PRIORITY));
        }
    }
}
Also used : Field(org.apache.bcel.classfile.Field) XField(edu.umd.cs.findbugs.ba.XField) JavaClass(org.apache.bcel.classfile.JavaClass) ToString(com.mebigfatguy.fbcontrib.utils.ToString)

Example 20 with Field

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

the class ImmatureClass method visitClassContext.

/**
 * overrides the visitor to report on classes without toStrings that have fields
 *
 * @param classContext
 *            the context object of the currently parsed class
 */
@Override
public void visitClassContext(ClassContext classContext) {
    JavaClass cls = classContext.getJavaClass();
    fieldStatus = FieldStatus.NONE;
    String packageName = cls.getPackageName();
    if (packageName.isEmpty()) {
        bugReporter.reportBug(new BugInstance(this, BugType.IMC_IMMATURE_CLASS_NO_PACKAGE.name(), LOW_PRIORITY).addClass(cls));
    }
    if (!packageName.equals(packageName.toLowerCase(Locale.ENGLISH))) {
        bugReporter.reportBug(new BugInstance(this, BugType.IMC_IMMATURE_CLASS_UPPER_PACKAGE.name(), LOW_PRIORITY).addClass(cls));
    }
    String simpleClassName = cls.getClassName();
    int dotPos = simpleClassName.lastIndexOf('.');
    if (dotPos >= 0) {
        simpleClassName = simpleClassName.substring(dotPos + 1);
    }
    if (!Character.isUpperCase(simpleClassName.charAt(0)) && (simpleClassName.indexOf(Values.INNER_CLASS_SEPARATOR) < 0) && !PACKAGE_INFO.equals(simpleClassName)) {
        bugReporter.reportBug(new BugInstance(this, BugType.IMC_IMMATURE_CLASS_LOWER_CLASS.name(), LOW_PRIORITY).addClass(cls));
    }
    if ((!cls.isAbstract()) && (!cls.isEnum()) && (cls.getClassName().indexOf(Values.INNER_CLASS_SEPARATOR) < 0) && !isTestClass(cls)) {
        try {
            boolean clsHasRuntimeAnnotation = classHasRuntimeVisibleAnnotation(cls);
            if (clsHasRuntimeAnnotation) {
                classIsJPAEntity = classIsJPAEntity(cls);
            } else {
                classIsJPAEntity = false;
            }
            HEStatus heStatus = HEStatus.UNKNOWN;
            checkIDEGeneratedParmNames(cls);
            for (Field f : cls.getFields()) {
                if (!f.isStatic() && !f.isSynthetic()) {
                    boolean fieldHasRuntimeAnnotation = fieldHasRuntimeVisibleAnnotation(f);
                    if (!fieldHasRuntimeAnnotation) {
                        /* only report one of these, so as not to flood the report */
                        if (!classIsJPAEntity && !hasMethodInHierarchy(cls, Values.TOSTRING, SignatureBuilder.SIG_VOID_TO_STRING)) {
                            bugReporter.reportBug(new BugInstance(this, BugType.IMC_IMMATURE_CLASS_NO_TOSTRING.name(), LOW_PRIORITY).addClass(cls));
                            heStatus = HEStatus.NOT_NEEDED;
                            break;
                        }
                        if (heStatus != HEStatus.NOT_NEEDED) {
                            String fieldSig = f.getSignature();
                            if (fieldSig.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX)) {
                                if (!fieldSig.startsWith("Ljava")) {
                                    JavaClass fieldClass = Repository.lookupClass(SignatureUtils.trimSignature(fieldSig));
                                    if (!hasMethodInHierarchy(fieldClass, "equals", SignatureBuilder.SIG_OBJECT_TO_BOOLEAN)) {
                                        heStatus = HEStatus.NOT_NEEDED;
                                    }
                                } else if (!fieldSig.startsWith("Ljava/lang/") && !fieldSig.startsWith("Ljava/util/")) {
                                    heStatus = HEStatus.NOT_NEEDED;
                                }
                            } else if (!fieldSig.startsWith(Values.SIG_ARRAY_PREFIX)) {
                                heStatus = HEStatus.NEEDED;
                            }
                        }
                    } else {
                        heStatus = HEStatus.NOT_NEEDED;
                    }
                }
            }
            if (!clsHasRuntimeAnnotation && (heStatus == HEStatus.NEEDED)) {
                if (!hasMethodInHierarchy(cls, "equals", SignatureBuilder.SIG_OBJECT_TO_BOOLEAN)) {
                    bugReporter.reportBug(new BugInstance(this, BugType.IMC_IMMATURE_CLASS_NO_EQUALS.name(), LOW_PRIORITY).addClass(cls));
                } else if (!hasMethodInHierarchy(cls, Values.HASHCODE, SignatureBuilder.SIG_VOID_TO_INT)) {
                    bugReporter.reportBug(new BugInstance(this, BugType.IMC_IMMATURE_CLASS_NO_HASHCODE.name(), LOW_PRIORITY).addClass(cls));
                }
            }
        } catch (ClassNotFoundException cnfe) {
            bugReporter.reportMissingClass(cnfe);
        }
    }
    super.visitClassContext(classContext);
}
Also used : Field(org.apache.bcel.classfile.Field) JavaClass(org.apache.bcel.classfile.JavaClass) BugInstance(edu.umd.cs.findbugs.BugInstance)

Aggregations

Field (org.apache.bcel.classfile.Field)23 JavaClass (org.apache.bcel.classfile.JavaClass)17 XField (edu.umd.cs.findbugs.ba.XField)6 Method (org.apache.bcel.classfile.Method)6 ToString (com.mebigfatguy.fbcontrib.utils.ToString)5 BugInstance (edu.umd.cs.findbugs.BugInstance)5 AnnotationEntry (org.apache.bcel.classfile.AnnotationEntry)4 FieldAnnotation (edu.umd.cs.findbugs.FieldAnnotation)3 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)3 FieldInfo (com.jopdesign.common.FieldInfo)2 MethodInfo (com.jopdesign.common.MethodInfo)2 EnclosingMethod (com.jopdesign.common.bcel.EnclosingMethod)2 ConstantFieldInfo (com.jopdesign.common.type.ConstantFieldInfo)2 ConstantMethodInfo (com.jopdesign.common.type.ConstantMethodInfo)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 ConstantInteger (org.apache.bcel.classfile.ConstantInteger)2 ConstantValue (org.apache.bcel.classfile.ConstantValue)2 InvokeSite (com.jopdesign.common.code.InvokeSite)1