Search in sources :

Example 6 with FieldAnnotation

use of edu.umd.cs.findbugs.FieldAnnotation 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 7 with FieldAnnotation

use of edu.umd.cs.findbugs.FieldAnnotation in project fb-contrib by mebigfatguy.

the class NonRecycleableTaglibs method sawOpcode.

/**
 * implements the visitor to record storing of fields, and where they occur
 *
 * @param seen
 *            the currently parsed opcode
 */
@Override
public void sawOpcode(int seen) {
    if (seen == PUTFIELD) {
        QMethod methodInfo = new QMethod(getMethodName(), getMethodSig());
        Map<Map.Entry<String, String>, SourceLineAnnotation> fields = methodWrites.get(methodInfo);
        if (fields == null) {
            fields = new HashMap<>();
            methodWrites.put(methodInfo, fields);
        }
        String fieldName = getNameConstantOperand();
        String fieldSig = getSigConstantOperand();
        FieldAnnotation fa = new FieldAnnotation(getDottedClassName(), fieldName, fieldSig, false);
        fieldAnnotations.put(fieldName, fa);
        fields.put(new AbstractMap.SimpleImmutableEntry(fieldName, fieldSig), SourceLineAnnotation.fromVisitedInstruction(this));
    }
}
Also used : AbstractMap(java.util.AbstractMap) QMethod(com.mebigfatguy.fbcontrib.utils.QMethod) SourceLineAnnotation(edu.umd.cs.findbugs.SourceLineAnnotation) FieldAnnotation(edu.umd.cs.findbugs.FieldAnnotation)

Example 8 with FieldAnnotation

use of edu.umd.cs.findbugs.FieldAnnotation in project fb-contrib by mebigfatguy.

the class OverzealousCasting method sawOpcode.

/**
 * implements the visitor to look for a checkcast followed by a astore, where the types of the objects are different.
 *
 * @param seen
 *            the opcode of the currently parsed instruction
 */
@Override
public void sawOpcode(int seen) {
    switch(state) {
        case SAW_NOTHING:
            if (seen == Const.CHECKCAST) {
                castClass = getClassConstantOperand();
                state = State.SAW_CHECKCAST;
            } else if (seen == Const.INVOKEINTERFACE) {
                // enhanced for loops add an incorrect checkcast instruction, so
                // ignore checkcasts after iterator.next()
                String clsName = getClassConstantOperand();
                String methodName = getNameConstantOperand();
                if ("java/util/Iterator".equals(clsName) && "next".equals(methodName)) {
                    state = State.SAW_NEXT;
                }
            }
            break;
        case SAW_CHECKCAST:
            if (OpcodeUtils.isAStore(seen)) {
                int reg = RegisterUtils.getAStoreReg(this, seen);
                LocalVariable lv = lvt.getLocalVariable(reg, getNextPC());
                if (lv != null) {
                    String sig = lv.getSignature();
                    if (sig.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX)) {
                        sig = SignatureUtils.trimSignature(sig);
                    }
                    if (!sig.equals(castClass)) {
                        bugReporter.reportBug(new BugInstance(this, BugType.OC_OVERZEALOUS_CASTING.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                    }
                }
            } else if (seen == Const.PUTFIELD) {
                FieldAnnotation f = FieldAnnotation.fromReferencedField(this);
                String sig = f.getFieldSignature();
                if (sig.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX)) {
                    sig = SignatureUtils.trimSignature(sig);
                }
                // if the signature is Object, the field might be genericized, so ignore
                if (!sig.equals(Values.SLASHED_JAVA_LANG_OBJECT) && !sig.equals(castClass)) {
                    bugReporter.reportBug(new BugInstance(this, BugType.OC_OVERZEALOUS_CASTING.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                }
            }
            state = State.SAW_NOTHING;
            break;
        case SAW_NEXT:
        default:
            state = State.SAW_NOTHING;
            break;
    }
}
Also used : LocalVariable(org.apache.bcel.classfile.LocalVariable) FieldAnnotation(edu.umd.cs.findbugs.FieldAnnotation) BugInstance(edu.umd.cs.findbugs.BugInstance)

Example 9 with FieldAnnotation

use of edu.umd.cs.findbugs.FieldAnnotation in project fb-contrib by mebigfatguy.

the class NeedlessMemberCollectionSynchronization method visitField.

/**
 * implements the visitor to find collection fields
 *
 * @param obj
 *            the context object of the currently parse field
 */
@Override
public void visitField(Field obj) {
    if (obj.isPrivate()) {
        String signature = obj.getSignature();
        if (signature.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX)) {
            try {
                JavaClass cls = Repository.lookupClass(SignatureUtils.stripSignature(signature));
                if (cls.implementationOf(collectionClass) || cls.implementationOf(mapClass)) {
                    FieldAnnotation fa = FieldAnnotation.fromVisitedField(this);
                    collectionFields.put(fa.getFieldName(), new FieldInfo(fa));
                }
            } catch (ClassNotFoundException cnfe) {
                bugReporter.reportMissingClass(cnfe);
            }
        }
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) FieldAnnotation(edu.umd.cs.findbugs.FieldAnnotation)

Example 10 with FieldAnnotation

use of edu.umd.cs.findbugs.FieldAnnotation in project fb-contrib by mebigfatguy.

the class Section508Compliance method visitField.

/**
 * looks for fields that are JLabels and stores them in a set
 *
 * @param obj
 *            the field object of the current field
 */
@Override
public void visitField(Field obj) {
    String fieldSig = obj.getSignature();
    if ("Ljavax/swing/JLabel;".equals(fieldSig)) {
        FieldAnnotation fa = FieldAnnotation.fromVisitedField(this);
        fieldLabels.add(XFactory.createXField(fa));
    }
}
Also used : FieldAnnotation(edu.umd.cs.findbugs.FieldAnnotation)

Aggregations

FieldAnnotation (edu.umd.cs.findbugs.FieldAnnotation)12 BugInstance (edu.umd.cs.findbugs.BugInstance)7 JavaClass (org.apache.bcel.classfile.JavaClass)5 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)4 ToString (com.mebigfatguy.fbcontrib.utils.ToString)3 Field (org.apache.bcel.classfile.Field)3 QMethod (com.mebigfatguy.fbcontrib.utils.QMethod)2 SourceLineAnnotation (edu.umd.cs.findbugs.SourceLineAnnotation)2 XField (edu.umd.cs.findbugs.ba.XField)2 HashMap (java.util.HashMap)2 AnnotationEntry (org.apache.bcel.classfile.AnnotationEntry)2 FieldDescriptor (edu.umd.cs.findbugs.classfile.FieldDescriptor)1 AbstractMap (java.util.AbstractMap)1 BitSet (java.util.BitSet)1 Map (java.util.Map)1 Nullable (javax.annotation.Nullable)1 ConstantPool (org.apache.bcel.classfile.ConstantPool)1 ConstantUtf8 (org.apache.bcel.classfile.ConstantUtf8)1 LocalVariable (org.apache.bcel.classfile.LocalVariable)1