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));
}
}
}
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);
}
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;
}
}
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;
}
}
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;
}
Aggregations