Search in sources :

Example 11 with FieldAnnotation

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

the class Section508Compliance method sawOpcode.

/**
 * implements the visitor to find 508 compliance concerns
 *
 * @param seen
 *            the opcode of the currently parsed instruction
 */
@Override
public void sawOpcode(int seen) {
    boolean sawTextLabel = false;
    boolean sawUIManager = false;
    boolean sawAppend = false;
    try {
        stack.precomputation(this);
        if (OpcodeUtils.isAStore(seen)) {
            if (stack.getStackDepth() > 0) {
                OpcodeStack.Item item = stack.getStackItem(0);
                if ("Ljavax/swing/JLabel;".equals(item.getSignature()) && (S508UserValue.SAW_TEXT_LABEL == item.getUserValue())) {
                    int reg = RegisterUtils.getAStoreReg(this, seen);
                    localLabels.put(Integer.valueOf(reg), SourceLineAnnotation.fromVisitedInstruction(this));
                }
            }
        } else if (seen == PUTFIELD) {
            if (stack.getStackDepth() > 0) {
                OpcodeStack.Item item = stack.getStackItem(0);
                if (S508UserValue.SAW_TEXT_LABEL != item.getUserValue()) {
                    FieldAnnotation fa = new FieldAnnotation(getDottedClassName(), getNameConstantOperand(), getSigConstantOperand(), false);
                    fieldLabels.remove(XFactory.createXField(fa));
                }
            }
        } else if (seen == INVOKESPECIAL) {
            String className = getClassConstantOperand();
            String methodName = getNameConstantOperand();
            if ("javax/swing/JLabel".equals(className) && Values.CONSTRUCTOR.equals(methodName)) {
                String signature = getSigConstantOperand();
                if (signature.indexOf(Values.SIG_JAVA_LANG_STRING) >= 0) {
                    sawTextLabel = true;
                }
            }
        } else if (seen == INVOKEVIRTUAL) {
            String className = getClassConstantOperand();
            String methodName = getNameConstantOperand();
            if ("javax/swing/JLabel".equals(className)) {
                if ("setLabelFor".equals(methodName) && (stack.getStackDepth() > 1)) {
                    OpcodeStack.Item item = stack.getStackItem(1);
                    XField field = item.getXField();
                    if (field != null) {
                        fieldLabels.remove(field);
                    } else {
                        int reg = item.getRegisterNumber();
                        if (reg >= 0) {
                            localLabels.remove(Integer.valueOf(reg));
                        }
                    }
                }
            } else if (SignatureUtils.isPlainStringConvertableClass(className)) {
                if ("append".equals(methodName)) {
                    if (stack.getStackDepth() > 0) {
                        OpcodeStack.Item item = stack.getStackItem(0);
                        Object con = item.getConstant();
                        if (con instanceof String) {
                            String literal = (String) con;
                            sawAppend = !literal.startsWith("<");
                        } else {
                            sawAppend = true;
                        }
                    }
                } else if (Values.TOSTRING.equals(methodName) && (stack.getStackDepth() > 0)) {
                    OpcodeStack.Item item = stack.getStackItem(0);
                    if (S508UserValue.APPENDED_STRING == item.getUserValue()) {
                        sawAppend = true;
                    }
                }
            }
            processSetSizeOps(methodName);
            processNullLayouts(className, methodName);
            processSetColorOps(methodName);
        } else if ((seen == INVOKESTATIC) && "javax/swing/UIManager".equals(getClassConstantOperand())) {
            sawUIManager = true;
        }
        if ((seen == INVOKEVIRTUAL) || (seen == INVOKESPECIAL) || (seen == INVOKEINTERFACE)) {
            processFaultyGuiStrings();
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    } finally {
        TernaryPatcher.pre(stack, seen);
        stack.sawOpcode(this, seen);
        TernaryPatcher.post(stack, seen);
        if (sawTextLabel) {
            if (stack.getStackDepth() > 0) {
                OpcodeStack.Item item = stack.getStackItem(0);
                item.setUserValue(S508UserValue.SAW_TEXT_LABEL);
            }
        } else if (sawUIManager) {
            if (stack.getStackDepth() > 0) {
                OpcodeStack.Item item = stack.getStackItem(0);
                item.setUserValue(S508UserValue.FROM_UIMANAGER);
            }
        } else if (sawAppend && (stack.getStackDepth() > 0)) {
            OpcodeStack.Item item = stack.getStackItem(0);
            item.setUserValue(S508UserValue.APPENDED_STRING);
        }
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) XField(edu.umd.cs.findbugs.ba.XField) FieldAnnotation(edu.umd.cs.findbugs.FieldAnnotation)

Example 12 with FieldAnnotation

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

the class WiringIssues method visitClassContext.

@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(value = "SF_SWITCH_NO_DEFAULT", justification = "Only a few cases need special handling")
@Override
public void visitClassContext(ClassContext classContext) {
    try {
        JavaClass cls = classContext.getJavaClass();
        Field[] fields = cls.getFields();
        if (fields.length > 0) {
            Map<WiringType, FieldAnnotation> wiredFields = new HashMap<>();
            boolean loadedParents = false;
            for (Field field : fields) {
                boolean hasAutowired = false;
                String qualifier = "";
                for (AnnotationEntry entry : field.getAnnotationEntries()) {
                    switch(entry.getAnnotationType()) {
                        case SPRING_AUTOWIRED:
                            if (!loadedParents) {
                                loadParentAutowireds(cls.getSuperClass(), wiredFields);
                                loadedParents = true;
                            }
                            hasAutowired = true;
                            break;
                        case SPRING_QUALIFIER:
                            qualifier = entry.getElementValuePairs()[0].getValue().stringifyValue();
                            break;
                    }
                }
                if (hasAutowired) {
                    WiringType wt = new WiringType(field.getSignature(), field.getGenericSignature(), qualifier);
                    FieldAnnotation existingAnnotation = wiredFields.get(wt);
                    if (existingAnnotation == null) {
                        wiredFields.put(wt, FieldAnnotation.fromBCELField(cls.getClassName(), field));
                    } else {
                        bugReporter.reportBug(new BugInstance(this, BugType.WI_DUPLICATE_WIRED_TYPES.name(), NORMAL_PRIORITY).addClass(cls).addField(FieldAnnotation.fromBCELField(cls, field)).addField(existingAnnotation));
                        wiredFields.remove(wt);
                    }
                }
            }
        }
        stack = new OpcodeStack();
        super.visitClassContext(classContext);
    } catch (ClassNotFoundException e) {
        bugReporter.reportMissingClass(e);
    } finally {
        stack = null;
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) HashMap(java.util.HashMap) BugInstance(edu.umd.cs.findbugs.BugInstance) ToString(com.mebigfatguy.fbcontrib.utils.ToString) Field(org.apache.bcel.classfile.Field) AnnotationEntry(org.apache.bcel.classfile.AnnotationEntry) JavaClass(org.apache.bcel.classfile.JavaClass) 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