Search in sources :

Example 26 with OpcodeStack

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

the class PoorMansEnum method visitClassContext.

@Override
public void visitClassContext(ClassContext classContext) {
    try {
        JavaClass cls = classContext.getJavaClass();
        if (cls.getMajor() >= Const.MAJOR_1_5) {
            fieldValues = new HashMap<>();
            nameToField = new HashMap<>();
            for (Field f : cls.getFields()) {
                if (f.isPrivate() && !f.isSynthetic()) {
                    String fieldName = f.getName();
                    // preallocating a set per field is just a waste, so just insert the empty set as a place holder
                    fieldValues.put(fieldName, Collections.emptySet());
                    nameToField.put(fieldName, f);
                }
            }
            if (!fieldValues.isEmpty()) {
                stack = new OpcodeStack();
                firstFieldUse = new HashMap<>();
                try {
                    super.visitClassContext(classContext);
                    for (Map.Entry<String, Set<Object>> fieldInfo : fieldValues.entrySet()) {
                        Set<Object> values = fieldInfo.getValue();
                        if (values.size() >= 3) {
                            String fieldName = fieldInfo.getKey();
                            bugReporter.reportBug(new BugInstance(this, BugType.PME_POOR_MANS_ENUM.name(), NORMAL_PRIORITY).addClass(this).addField(XFactory.createXField(cls, nameToField.get(fieldName))).addSourceLine(firstFieldUse.get(fieldName)));
                        }
                    }
                } catch (StopOpcodeParsingException e) {
                // no fields left
                }
            }
        }
    } finally {
        fieldValues = null;
        nameToField = null;
        firstFieldUse = null;
        stack = null;
    }
}
Also used : Field(org.apache.bcel.classfile.Field) Set(java.util.Set) HashSet(java.util.HashSet) JavaClass(org.apache.bcel.classfile.JavaClass) OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) StopOpcodeParsingException(com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException) BugInstance(edu.umd.cs.findbugs.BugInstance) HashMap(java.util.HashMap) Map(java.util.Map)

Example 27 with OpcodeStack

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

the class LocalTypeDetector method visitClassContext.

/**
 * implements the visitor to create and clear the stack and suspectLocals
 *
 * @param classContext
 *            the context object of the currently parsed class
 */
@Override
public void visitClassContext(ClassContext classContext) {
    try {
        stack = new OpcodeStack();
        suspectLocals = new HashMap<>();
        classVersion = classContext.getJavaClass().getMajor();
        super.visitClassContext(classContext);
    } finally {
        stack = null;
        suspectLocals = null;
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack)

Example 28 with OpcodeStack

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

the class LoggerOddities method visitClassContext.

/**
 * implements the visitor to discover what the class name is if it is a normal class, or the owning class, if the class is an anonymous class.
 *
 * @param classContext
 *            the context object of the currently parsed class
 */
@Override
public void visitClassContext(ClassContext classContext) {
    try {
        stack = new OpcodeStack();
        nameOfThisClass = SignatureUtils.getNonAnonymousPortion(classContext.getJavaClass().getClassName());
        formatterLoggers = new HashSet<>();
        super.visitClassContext(classContext);
    } finally {
        formatterLoggers = null;
        stack = null;
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack)

Example 29 with OpcodeStack

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

the class LostExceptionStackTrace method visitClassContext.

/**
 * implements the visitor to make sure the jdk is 1.4 or better
 *
 * @param classContext
 *            the context object of the currently parsed class
 */
@Override
public void visitClassContext(ClassContext classContext) {
    try {
        if ((throwableClass != null) && !isPre14Class(classContext.getJavaClass())) {
            stack = new OpcodeStack();
            catchInfos = new HashSet<>();
            exReg = new HashMap<>();
            super.visitClassContext(classContext);
        }
    } finally {
        stack = null;
        catchInfos = null;
        exceptions = null;
        exReg = null;
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack)

Example 30 with OpcodeStack

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

the class MethodReturnsConstant method visitClassContext.

/**
 * implements the visitor to collect all methods that are overloads. These methods should be ignored, as you may differentiate Const based on parameter
 * type, or value.
 *
 * @param classContext
 *            the currently parsed class object
 */
@Override
public void visitClassContext(ClassContext classContext) {
    try {
        stack = new OpcodeStack();
        registerConstants = new HashMap<>();
        overloadedMethods = collectOverloadedMethods(classContext.getJavaClass());
        super.visitClassContext(classContext);
    } finally {
        stack = null;
        registerConstants = null;
        overloadedMethods = null;
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack)

Aggregations

OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)95 JavaClass (org.apache.bcel.classfile.JavaClass)20 BitSet (java.util.BitSet)15 BugInstance (edu.umd.cs.findbugs.BugInstance)7 HashMap (java.util.HashMap)4 Map (java.util.Map)3 Field (org.apache.bcel.classfile.Field)3 StopOpcodeParsingException (com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException)2 FieldAnnotation (edu.umd.cs.findbugs.FieldAnnotation)2 XField (edu.umd.cs.findbugs.ba.XField)2 AnnotationEntry (org.apache.bcel.classfile.AnnotationEntry)2 Method (org.apache.bcel.classfile.Method)2 ToString (com.mebigfatguy.fbcontrib.utils.ToString)1 ClassDescriptor (edu.umd.cs.findbugs.classfile.ClassDescriptor)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 ConstantInteger (org.apache.bcel.classfile.ConstantInteger)1