Search in sources :

Example 1 with FieldAnnotation

use of edu.umd.cs.findbugs.FieldAnnotation in project wcomponents by BorderTech.

the class CheckWComponentFields method visitField.

/**
 * {@inheritDoc}
 */
@Override
public void visitField(final Field obj) {
    // All fields inside WComponents must be static or final
    if (!obj.isFinal() && !obj.isStatic()) {
        FieldAnnotation ann = FieldAnnotation.fromVisitedField(this);
        util.getBugReporter().reportBug(new BugInstance(this, "WCF_NON_FINAL_WCOMPONENT_FIELD", HIGH_PRIORITY).addClass(this).addField(ann));
    }
    // Component models must never be stored as fields
    if (util.isComponentModel(util.getClassNameFromSignature(obj.getType().getSignature()))) {
        FieldAnnotation ann = FieldAnnotation.fromVisitedField(this);
        util.getBugReporter().reportBug(new BugInstance(this, "WCF_COMPONENT_MODEL_FIELD", HIGH_PRIORITY).addClass(this).addField(ann));
    }
    // UIContexts must never be stored as fields
    if (util.isUIContext(util.getClassNameFromSignature(obj.getType().getSignature()))) {
        FieldAnnotation ann = FieldAnnotation.fromVisitedField(this);
        util.getBugReporter().reportBug(new BugInstance(this, "WCF_UICONTEXT_FIELD", HIGH_PRIORITY).addClass(this).addField(ann));
    }
}
Also used : FieldAnnotation(edu.umd.cs.findbugs.FieldAnnotation) BugInstance(edu.umd.cs.findbugs.BugInstance)

Example 2 with FieldAnnotation

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

the class DeletingWhileIterating method sawOpcode.

/**
 * implements the visitor to look for deletes on collections that are being iterated
 *
 * @param seen
 *            the opcode of the currently parsed instruction
 */
@Override
public void sawOpcode(int seen) {
    int groupId = -1;
    try {
        stack.precomputation(this);
        if (seen == Const.INVOKEINTERFACE) {
            String className = getClassConstantOperand();
            String methodName = getNameConstantOperand();
            String signature = getSigConstantOperand();
            QMethod methodInfo = new QMethod(methodName, signature);
            if (isCollection(className)) {
                if (collectionMethods.contains(methodInfo) || ITERATOR.equals(methodInfo)) {
                    if (stack.getStackDepth() > 0) {
                        OpcodeStack.Item itm = stack.getStackItem(0);
                        groupId = findCollectionGroup(itm, true);
                    }
                } else if (REMOVE.equals(methodInfo)) {
                    if (stack.getStackDepth() > 1) {
                        OpcodeStack.Item itm = stack.getStackItem(1);
                        int id = findCollectionGroup(itm, true);
                        if ((id >= 0) && collectionGroups.get(id).isStandardCollection()) {
                            Integer it = groupToIterator.get(Integer.valueOf(id));
                            Loop loop = loops.get(it);
                            if (loop != null) {
                                int pc = getPC();
                                if (loop.hasPC(pc)) {
                                    boolean needPop = !Values.SIG_VOID.equals(SignatureUtils.getReturnSignature(signature));
                                    if (!breakFollows(loop, needPop) && !returnFollows(needPop)) {
                                        bugReporter.reportBug(new BugInstance(this, BugType.DWI_DELETING_WHILE_ITERATING.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                                    }
                                }
                            }
                        }
                    }
                } else {
                    Integer numArgs = modifyingMethods.get(methodInfo);
                    if ((numArgs != null) && (stack.getStackDepth() > numArgs.intValue())) {
                        OpcodeStack.Item itm = stack.getStackItem(numArgs.intValue());
                        int id = findCollectionGroup(itm, true);
                        if (id >= 0) {
                            Integer it = groupToIterator.get(Integer.valueOf(id));
                            if (it != null) {
                                Loop loop = loops.get(it);
                                if (loop != null) {
                                    int pc = getPC();
                                    if (loop.hasPC(pc)) {
                                        boolean needPop = !Values.SIG_VOID.equals(SignatureUtils.getReturnSignature(signature));
                                        boolean breakFollows = breakFollows(loop, needPop);
                                        boolean returnFollows = !breakFollows && returnFollows(needPop);
                                        if (!breakFollows && !returnFollows) {
                                            bugReporter.reportBug(new BugInstance(this, BugType.DWI_MODIFYING_WHILE_ITERATING.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } else if ("java/util/Iterator".equals(className) && HASNEXT.equals(methodInfo) && (stack.getStackDepth() > 0)) {
                OpcodeStack.Item itm = stack.getStackItem(0);
                Integer id = (Integer) itm.getUserValue();
                if (id != null) {
                    groupId = id.intValue();
                }
            }
        } else if ((seen == Const.PUTFIELD) || (seen == Const.PUTSTATIC)) {
            if (stack.getStackDepth() > 1) {
                OpcodeStack.Item itm = stack.getStackItem(0);
                Integer id = (Integer) itm.getUserValue();
                if (id == null) {
                    FieldAnnotation fa = FieldAnnotation.fromFieldDescriptor(new FieldDescriptor(getClassConstantOperand(), getNameConstantOperand(), getSigConstantOperand(), false));
                    itm = new OpcodeStack.Item(itm.getSignature(), fa, stack.getStackItem(1).getRegisterNumber());
                    removeFromCollectionGroup(itm);
                    groupId = findCollectionGroup(itm, true);
                }
            }
        } else if (OpcodeUtils.isAStore(seen)) {
            if (stack.getStackDepth() > 0) {
                OpcodeStack.Item itm = stack.getStackItem(0);
                Integer id = (Integer) itm.getUserValue();
                if (id != null) {
                    int reg = RegisterUtils.getAStoreReg(this, seen);
                    try {
                        JavaClass cls = itm.getJavaClass();
                        if ((cls != null) && cls.implementationOf(iteratorClass)) {
                            Integer regIt = Integer.valueOf(reg);
                            Iterator<Integer> curIt = groupToIterator.values().iterator();
                            while (curIt.hasNext()) {
                                if (curIt.next().equals(regIt)) {
                                    curIt.remove();
                                }
                            }
                            groupToIterator.put(id, regIt);
                        }
                        GroupPair pair = collectionGroups.get(id.intValue());
                        if (pair != null) {
                            pair.addMember(Integer.valueOf(reg));
                        }
                    } catch (ClassNotFoundException cnfe) {
                        bugReporter.reportMissingClass(cnfe);
                    }
                } else {
                    String cls = itm.getSignature();
                    if ((cls != null) && cls.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX)) {
                        cls = SignatureUtils.trimSignature(cls);
                        if (isCollection(cls) || "java/util/Iterator".equals(cls)) {
                            int reg = RegisterUtils.getAStoreReg(this, seen);
                            removeFromCollectionGroup(new OpcodeStack.Item(itm, reg));
                            Iterator<Integer> it = groupToIterator.values().iterator();
                            while (it.hasNext()) {
                                if (it.next().intValue() == reg) {
                                    it.remove();
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        } else if (OpcodeUtils.isALoad(seen)) {
            int reg = RegisterUtils.getALoadReg(this, seen);
            OpcodeStack.Item itm = new OpcodeStack.Item(new OpcodeStack.Item(), reg);
            groupId = findCollectionGroup(itm, false);
        } else if ((seen == Const.IFEQ) && (stack.getStackDepth() > 0)) {
            OpcodeStack.Item itm = stack.getStackItem(0);
            Integer id = (Integer) itm.getUserValue();
            if (id != null) {
                int target = getBranchTarget();
                int gotoAddr = target - 3;
                int ins = getCode().getCode()[gotoAddr];
                if (ins < 0) {
                    ins = 256 + ins;
                }
                if ((ins == Const.GOTO) || (ins == Const.GOTO_W)) {
                    Integer reg = groupToIterator.get(id);
                    if (reg != null) {
                        loops.put(reg, new Loop(getPC(), gotoAddr));
                    }
                }
            }
        }
    } finally {
        TernaryPatcher.pre(stack, seen);
        stack.sawOpcode(this, seen);
        TernaryPatcher.post(stack, seen);
        if ((groupId >= 0) && (stack.getStackDepth() > 0)) {
            OpcodeStack.Item itm = stack.getStackItem(0);
            itm.setUserValue(Integer.valueOf(groupId));
        }
        processEndOfScopes(Integer.valueOf(getPC()));
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) BugInstance(edu.umd.cs.findbugs.BugInstance) ToString(com.mebigfatguy.fbcontrib.utils.ToString) FieldDescriptor(edu.umd.cs.findbugs.classfile.FieldDescriptor) QMethod(com.mebigfatguy.fbcontrib.utils.QMethod) JavaClass(org.apache.bcel.classfile.JavaClass) FieldAnnotation(edu.umd.cs.findbugs.FieldAnnotation)

Example 3 with FieldAnnotation

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

the class DubiousListCollection method reportBugs.

/**
 * implements the detector, by reporting all remaining fields that only have set based access
 */
private void reportBugs() {
    int major = getClassContext().getJavaClass().getMajor();
    for (Map.Entry<String, FieldInfo> entry : fieldsReported.entrySet()) {
        String field = entry.getKey();
        FieldInfo fi = entry.getValue();
        int cnt = fi.getSetCount();
        if (cnt > 0) {
            FieldAnnotation fa = getFieldAnnotation(field);
            if (fa != null) {
                // can't use LinkedHashSet in 1.3 so report at LOW
                bugReporter.reportBug(new BugInstance(this, BugType.DLC_DUBIOUS_LIST_COLLECTION.name(), (major >= Const.MAJOR_1_4) ? NORMAL_PRIORITY : LOW_PRIORITY).addClass(this).addField(fa).addSourceLine(fi.getSourceLineAnnotation()));
            }
        }
    }
}
Also used : FieldAnnotation(edu.umd.cs.findbugs.FieldAnnotation) BugInstance(edu.umd.cs.findbugs.BugInstance) ToString(com.mebigfatguy.fbcontrib.utils.ToString) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with FieldAnnotation

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

the class DubiousListCollection method getFieldAnnotation.

/**
 * builds a field annotation by finding the field in the classes' field list
 *
 * @param fieldName
 *            the field for which to built the field annotation
 *
 * @return the field annotation of the specified field
 */
@Nullable
private FieldAnnotation getFieldAnnotation(final String fieldName) {
    JavaClass cls = getClassContext().getJavaClass();
    Field[] fields = cls.getFields();
    for (Field f : fields) {
        if (f.getName().equals(fieldName)) {
            return new FieldAnnotation(cls.getClassName(), fieldName, f.getSignature(), f.isStatic());
        }
    }
    // shouldn't happen
    return null;
}
Also used : Field(org.apache.bcel.classfile.Field) XField(edu.umd.cs.findbugs.ba.XField) JavaClass(org.apache.bcel.classfile.JavaClass) FieldAnnotation(edu.umd.cs.findbugs.FieldAnnotation) Nullable(javax.annotation.Nullable)

Example 5 with FieldAnnotation

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

the class DubiousMapCollection method visitClassContext.

@Override
public void visitClassContext(ClassContext classContext) {
    if ((mapInterface == null) || (propertiesClass == null)) {
        return;
    }
    try {
        stack = new OpcodeStack();
        mapFields = new HashMap<>();
        super.visitClassContext(classContext);
        for (FieldAnnotation mapField : mapFields.values()) {
            bugReporter.reportBug(new BugInstance(this, BugType.DMC_DUBIOUS_MAP_COLLECTION.toString(), NORMAL_PRIORITY).addClass(this).addField(mapField));
        }
    } finally {
        mapFields = null;
        stack = null;
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) FieldAnnotation(edu.umd.cs.findbugs.FieldAnnotation) BugInstance(edu.umd.cs.findbugs.BugInstance)

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