Skip to content

Commit

Permalink
don't report possible bloat when done from specially annotated method…
Browse files Browse the repository at this point in the history
…s like @PostConstruct
  • Loading branch information
mebigfatguy committed Nov 16, 2024
1 parent ef15dac commit 331b97c
Showing 1 changed file with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Set;

import org.apache.bcel.Const;
import org.apache.bcel.classfile.AnnotationEntry;
import org.apache.bcel.classfile.Code;
import org.apache.bcel.classfile.Field;
import org.apache.bcel.classfile.JavaClass;
Expand Down Expand Up @@ -85,6 +86,8 @@ public class PossibleMemoryBloat extends BytecodeScanningDetector {
private static final FQMethod jaxbNewInstance = new FQMethod("javax/xml/bind/JAXBContext", "newInstance",
"([Ljava/lang/Class;)Ljavax/xml/bind/JAXBContext;");

private static final Set<String> specialAnnotations = UnmodifiableSet.create("Ljavax/annotation/PostConstruct;", "Ljakarta/annotation/PostConstruct;");

private final BugReporter bugReporter;
private Map<XField, FieldAnnotation> bloatableCandidates;
private Map<XField, FieldAnnotation> bloatableFields;
Expand All @@ -94,6 +97,7 @@ public class PossibleMemoryBloat extends BytecodeScanningDetector {
private Map<Integer, XField> userValues;
private Map<Integer, Integer> jaxbContextRegs;
private boolean isPrivateMethod;
private Boolean isSpecialAnnotationMethod;

/**
* constructs a PMB detector given the reporter to report bugs on
Expand Down Expand Up @@ -181,6 +185,7 @@ private void parseFields(ClassContext classContext) {
public void visitMethod(Method obj) {
methodName = obj.getName();
isPrivateMethod = (obj.getAccessFlags() & Const.ACC_PRIVATE) != 0;
isSpecialAnnotationMethod = null;
}

/**
Expand Down Expand Up @@ -318,8 +323,33 @@ protected void checkMethodAsDecreasingOrIncreasing(XField field) {
} else if (increasingMethods.contains(mName) && !isPrivateMethod) {
FieldAnnotation fieldAn = bloatableCandidates.get(field);
if (fieldAn != null) {
bloatableFields.put(field, fieldAn);
if (!specialAnnotationMethod()) {
bloatableFields.put(field, fieldAn);
}
}
}
}

private boolean specialAnnotationMethod() {
if (isSpecialAnnotationMethod != null) {
return isSpecialAnnotationMethod.booleanValue();
}

AnnotationEntry[] entries = getMethod().getAnnotationEntries();
if (entries == null || entries.length == 0) {
isSpecialAnnotationMethod = Boolean.FALSE;
return false;
}

for (AnnotationEntry entry : entries) {
String type = entry.getAnnotationType();
isSpecialAnnotationMethod = Boolean.valueOf(specialAnnotations.contains(type));
if (isSpecialAnnotationMethod.booleanValue()) {
return true;
}
}

isSpecialAnnotationMethod = Boolean.FALSE;
return false;
}
}

0 comments on commit 331b97c

Please sign in to comment.