Skip to content

Commit

Permalink
rule out PMB modifier calls in private method (allow addition of fals…
Browse files Browse the repository at this point in the history
…e negatives for now) #472
  • Loading branch information
mebigfatguy committed Nov 2, 2024
1 parent 591902d commit 6f850f8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public class PossibleMemoryBloat extends BytecodeScanningDetector {
private Set<FieldAnnotation> threadLocalNonStaticFields;
private Map<Integer, XField> userValues;
private Map<Integer, Integer> jaxbContextRegs;
private boolean isPrivateMethod;

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

/**
Expand All @@ -195,7 +197,7 @@ public void visitCode(Code obj) {
if (Values.STATIC_INITIALIZER.equals(methodName) || Values.CONSTRUCTOR.equals(methodName)) {
return;
}

super.visitCode(obj);

for (Integer pc : jaxbContextRegs.values()) {
Expand Down Expand Up @@ -313,7 +315,7 @@ protected void checkMethodAsDecreasingOrIncreasing(XField field) {
if (decreasingMethods.contains(mName)) {
bloatableCandidates.remove(field);
bloatableFields.remove(field);
} else if (increasingMethods.contains(mName)) {
} else if (increasingMethods.contains(mName) && !isPrivateMethod) {
FieldAnnotation fieldAn = bloatableCandidates.get(field);
if (fieldAn != null) {
bloatableFields.put(field, fieldAn);
Expand Down
9 changes: 9 additions & 0 deletions src/samples/java/ex/PMB_Sample.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class PMB_Sample {
private static Map<String, String> fp_data = new WeakHashMap<>();

private static Map<String, String> fpEmptyWithIterator = new HashMap<>();

private static Set<String> fpGrowsInPrivate = new HashSet<>();

private static final Set<String> bloatableSigs = new HashSet<>();

Expand Down Expand Up @@ -125,6 +127,13 @@ public static void fpCleanUpWithIterator276(String key) {
fpEmptyWithIterator.put(key, "foo");
}
}

private void fpGrowsInPrivate() {
String sample = "Sample";
for (int i = 0; i < sample.length(); i++) {
fpGrowsInPrivate.add(sample.substring(i, i+1));
}
}

class X {
}
Expand Down

0 comments on commit 6f850f8

Please sign in to comment.