Search in sources :

Example 1 with SourceLineAnnotation

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

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

the class Section508Compliance method visitCode.

/**
 * implements the visitor to reset the stack
 *
 * @param obj
 *            the context object for the currently visited code block
 */
@Override
public void visitCode(Code obj) {
    stack.resetForMethodEntry(this);
    localLabels.clear();
    super.visitCode(obj);
    for (SourceLineAnnotation sla : localLabels.values()) {
        BugInstance bug = new BugInstance(this, BugType.S508C_NO_SETLABELFOR.name(), NORMAL_PRIORITY).addClass(this).addMethod(this);
        if (sla != null) {
            bug.addSourceLine(sla);
        }
        bugReporter.reportBug(bug);
    }
}
Also used : SourceLineAnnotation(edu.umd.cs.findbugs.SourceLineAnnotation) BugInstance(edu.umd.cs.findbugs.BugInstance)

Example 3 with SourceLineAnnotation

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

the class UnrelatedCollectionContents method checkAdd.

/**
 * processes an add into a collection, by processing all the super classes/interfaces of an object and removing the possible set of parent classes that have
 * been seen so far, by doing what amounts to a intersection of what has been seen before, and this occurance.
 *
 * @param colItm
 *            the collection that is being added to
 * @param addItm
 *            the added item
 * @throws ClassNotFoundException
 *             if a super class is not found
 */
private void checkAdd(final OpcodeStack.Item colItm, final OpcodeStack.Item addItm) throws ClassNotFoundException {
    int reg = colItm.getRegisterNumber();
    if (reg == -1) {
        XField field = colItm.getXField();
        if (field == null) {
            return;
        }
        Set<SourceLineAnnotation> sla = memberSourceLineAnnotations.get(field.getName());
        if (sla == null) {
            sla = new HashSet<>();
            memberSourceLineAnnotations.put(field.getName(), sla);
        }
        sla.add(SourceLineAnnotation.fromVisitedInstruction(this));
        FQField fqField = new FQField(field.getClassName(), field.getName(), field.getSignature());
        Set<String> commonSupers = memberCollections.get(fqField);
        if (commonSupers == null) {
            commonSupers = new HashSet<>();
            memberCollections.put(fqField, commonSupers);
            addNewItem(commonSupers, addItm);
        } else {
            mergeItem(commonSupers, sla, addItm);
        }
    } else {
        Integer regNumber = Integer.valueOf(reg);
        Set<SourceLineAnnotation> pcs = localSourceLineAnnotations.get(regNumber);
        if (pcs == null) {
            pcs = new HashSet<>();
            localSourceLineAnnotations.put(regNumber, pcs);
        }
        pcs.add(SourceLineAnnotation.fromVisitedInstruction(this, getPC()));
        Set<String> commonSupers = localCollections.get(regNumber);
        if (commonSupers == null) {
            commonSupers = new HashSet<>();
            localCollections.put(Integer.valueOf(reg), commonSupers);
            addNewItem(commonSupers, addItm);
            Integer scopeEnd = Integer.valueOf(RegisterUtils.getLocalVariableEndRange(getMethod().getLocalVariableTable(), reg, getNextPC()));
            BitSet regs = localScopeEnds.get(scopeEnd);
            if (regs == null) {
                regs = new BitSet();
                localScopeEnds.put(scopeEnd, regs);
            }
            regs.set(regNumber);
        } else {
            mergeItem(commonSupers, pcs, addItm);
        }
    }
}
Also used : XField(edu.umd.cs.findbugs.ba.XField) SourceLineAnnotation(edu.umd.cs.findbugs.SourceLineAnnotation) FQField(com.mebigfatguy.fbcontrib.utils.FQField) BitSet(java.util.BitSet)

Example 4 with SourceLineAnnotation

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

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

the class NonRecycleableTaglibs method reportBugs.

/**
 * generates all the bug reports for attributes that are not recycleable
 */
private void reportBugs() {
    for (Map.Entry<QMethod, String> attEntry : attributes.entrySet()) {
        QMethod methodInfo = attEntry.getKey();
        String attType = attEntry.getValue();
        Map<Map.Entry<String, String>, SourceLineAnnotation> fields = methodWrites.get(methodInfo);
        if ((fields == null) || (fields.size() != 1)) {
            continue;
        }
        Map.Entry<String, String> fieldInfo = fields.keySet().iterator().next();
        String fieldType = fieldInfo.getValue();
        if (!attType.equals(fieldType)) {
            continue;
        }
        String fieldName = fieldInfo.getKey();
        for (Map.Entry<QMethod, Map<Map.Entry<String, String>, SourceLineAnnotation>> fwEntry : methodWrites.entrySet()) {
            if (fwEntry.getKey().equals(methodInfo)) {
                continue;
            }
            SourceLineAnnotation sla = fwEntry.getValue().get(fieldInfo);
            if (sla != null) {
                bugReporter.reportBug(new BugInstance(this, BugType.NRTL_NON_RECYCLEABLE_TAG_LIB.name(), NORMAL_PRIORITY).addClass(this).addField(fieldAnnotations.get(fieldName)).addSourceLine(sla));
                break;
            }
        }
    }
}
Also used : QMethod(com.mebigfatguy.fbcontrib.utils.QMethod) SourceLineAnnotation(edu.umd.cs.findbugs.SourceLineAnnotation) BugInstance(edu.umd.cs.findbugs.BugInstance) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) Map(java.util.Map)

Aggregations

SourceLineAnnotation (edu.umd.cs.findbugs.SourceLineAnnotation)11 BugInstance (edu.umd.cs.findbugs.BugInstance)6 Method (org.apache.bcel.classfile.Method)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 JavaClass (org.apache.bcel.classfile.JavaClass)3 QMethod (com.mebigfatguy.fbcontrib.utils.QMethod)2 StopOpcodeParsingException (com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException)2 FieldAnnotation (edu.umd.cs.findbugs.FieldAnnotation)2 AbstractMap (java.util.AbstractMap)2 BitSet (java.util.BitSet)2 HashSet (java.util.HashSet)2 FQField (com.mebigfatguy.fbcontrib.utils.FQField)1 ToString (com.mebigfatguy.fbcontrib.utils.ToString)1 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)1 XField (edu.umd.cs.findbugs.ba.XField)1 ArrayDeque (java.util.ArrayDeque)1 Nullable (javax.annotation.Nullable)1 AnnotationEntry (org.apache.bcel.classfile.AnnotationEntry)1 ConstantPool (org.apache.bcel.classfile.ConstantPool)1