Search in sources :

Example 6 with Field

use of org.apache.bcel.classfile.Field in project fb-contrib by mebigfatguy.

the class PossibleMemoryBloat method parseFields.

private void parseFields(ClassContext classContext) {
    JavaClass cls = classContext.getJavaClass();
    Field[] fields = cls.getFields();
    for (Field f : fields) {
        String sig = f.getSignature();
        if (f.isStatic()) {
            if (bloatableSigs.contains(sig)) {
                bloatableCandidates.put(XFactory.createXField(cls.getClassName(), f.getName(), f.getSignature(), f.isStatic()), FieldAnnotation.fromBCELField(cls, f));
            }
        } else if ("Ljava/lang/ThreadLocal;".equals(sig)) {
            threadLocalNonStaticFields.add(FieldAnnotation.fromBCELField(cls, f));
        }
    }
}
Also used : Field(org.apache.bcel.classfile.Field) XField(edu.umd.cs.findbugs.ba.XField) JavaClass(org.apache.bcel.classfile.JavaClass)

Example 7 with Field

use of org.apache.bcel.classfile.Field in project fb-contrib by mebigfatguy.

the class SillynessPotPourri method looksLikeStaticFieldValue.

private boolean looksLikeStaticFieldValue(String constant) {
    if (staticConstants == null) {
        staticConstants = new HashSet<>();
        Field[] fields = getClassContext().getJavaClass().getFields();
        for (Field f : fields) {
            if (((f.getAccessFlags() & (Const.ACC_FINAL | Const.ACC_STATIC)) == (Const.ACC_FINAL | Const.ACC_STATIC)) && Values.SIG_JAVA_LANG_STRING.equals(f.getSignature())) {
                ConstantValue cv = f.getConstantValue();
                if (cv != null) {
                    int cvIndex = cv.getConstantValueIndex();
                    staticConstants.add(getConstantPool().getConstantString(cvIndex, Const.CONSTANT_String));
                }
            }
        }
    }
    return staticConstants.contains(constant);
}
Also used : Field(org.apache.bcel.classfile.Field) XField(edu.umd.cs.findbugs.ba.XField) ConstantValue(org.apache.bcel.classfile.ConstantValue)

Example 8 with Field

use of org.apache.bcel.classfile.Field in project fb-contrib by mebigfatguy.

the class ParallelLists method visitClassContext.

@Override
public void visitClassContext(final ClassContext classContext) {
    try {
        JavaClass cls = classContext.getJavaClass();
        listFields = new HashSet<>();
        Field[] flds = cls.getFields();
        for (Field f : flds) {
            String sig = f.getSignature();
            if (sig.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX)) {
                sig = SignatureUtils.trimSignature(sig);
                if (sig.startsWith("java/util/") && sig.endsWith("List")) {
                    listFields.add(f.getName());
                }
            } else if (sig.startsWith(Values.SIG_ARRAY_PREFIX) && !sig.startsWith(Values.SIG_ARRAY_OF_ARRAYS_PREFIX)) {
                listFields.add(f.getName());
            }
        }
        if (!listFields.isEmpty()) {
            stack = new OpcodeStack();
            indexToFieldMap = new HashMap<>();
            super.visitClassContext(classContext);
        }
    } finally {
        stack = null;
        indexToFieldMap = null;
    }
}
Also used : Field(org.apache.bcel.classfile.Field) XField(edu.umd.cs.findbugs.ba.XField) JavaClass(org.apache.bcel.classfile.JavaClass) OpcodeStack(edu.umd.cs.findbugs.OpcodeStack)

Example 9 with Field

use of org.apache.bcel.classfile.Field 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 10 with Field

use of org.apache.bcel.classfile.Field in project fb-contrib by mebigfatguy.

the class LoggerOddities method isNonPrivateLogField.

/**
 * looks to see if this field is a logger, and declared non privately
 *
 * @param fieldClsName
 *            the owning class type of the field
 * @param fieldName
 *            the name of the field
 * @param fieldSig
 *            the signature of the field
 * @return if the field is a logger and not private
 */
private boolean isNonPrivateLogField(String fieldClsName, String fieldName, String fieldSig) {
    String fieldType = SignatureUtils.trimSignature(fieldSig);
    if (!SLF4J_LOGGER.equals(fieldType) && !COMMONS_LOGGER.equals(fieldType) && !LOG4J_LOGGER.equals(fieldType) && !LOG4J2_LOGGER.equals(fieldType)) {
        return false;
    }
    JavaClass cls = getClassContext().getJavaClass();
    if (!cls.getClassName().equals(fieldClsName.replace('/', '.'))) {
        return false;
    }
    for (Field f : getClassContext().getJavaClass().getFields()) {
        if (f.getName().equals(fieldName)) {
            return !f.isPrivate();
        }
    }
    return true;
}
Also used : Field(org.apache.bcel.classfile.Field) JavaClass(org.apache.bcel.classfile.JavaClass) ToString(com.mebigfatguy.fbcontrib.utils.ToString)

Aggregations

Field (org.apache.bcel.classfile.Field)23 JavaClass (org.apache.bcel.classfile.JavaClass)17 XField (edu.umd.cs.findbugs.ba.XField)6 Method (org.apache.bcel.classfile.Method)6 ToString (com.mebigfatguy.fbcontrib.utils.ToString)5 BugInstance (edu.umd.cs.findbugs.BugInstance)5 AnnotationEntry (org.apache.bcel.classfile.AnnotationEntry)4 FieldAnnotation (edu.umd.cs.findbugs.FieldAnnotation)3 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)3 FieldInfo (com.jopdesign.common.FieldInfo)2 MethodInfo (com.jopdesign.common.MethodInfo)2 EnclosingMethod (com.jopdesign.common.bcel.EnclosingMethod)2 ConstantFieldInfo (com.jopdesign.common.type.ConstantFieldInfo)2 ConstantMethodInfo (com.jopdesign.common.type.ConstantMethodInfo)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 ConstantInteger (org.apache.bcel.classfile.ConstantInteger)2 ConstantValue (org.apache.bcel.classfile.ConstantValue)2 InvokeSite (com.jopdesign.common.code.InvokeSite)1