Skip to content

Commit 7db059c

Browse files
authored
Fix shared CST state for injected method (#411)
This method fixes a subtle bug that occurs when the CST for a pre-cached node is shared between codemod uses.
1 parent 35978f5 commit 7db059c

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

framework/codemodder-base/src/main/java/io/codemodder/remediation/headerinjection/DefaultHeaderInjectionRemediator.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,17 @@
2525
final class DefaultHeaderInjectionRemediator implements HeaderInjectionRemediator {
2626

2727
private static final Set<String> setHeaderNames = Set.of("setHeader", "addHeader");
28-
private final MethodDeclaration fixMethod;
2928

3029
private static final String validatorMethodName = "stripNewlines";
30+
private final String fixMethodCode;
3131

3232
DefaultHeaderInjectionRemediator() {
33-
String fixMethodCode =
33+
this.fixMethodCode =
3434
"""
3535
private static String stripNewlines(final String s) {
3636
return s.replaceAll("[\\n\\r]", "");
3737
}
3838
""";
39-
40-
this.fixMethod = StaticJavaParser.parseMethodDeclaration(fixMethodCode);
4139
}
4240

4341
@Override
@@ -89,6 +87,11 @@ public <T> CodemodFileScanningResult remediateAll(
8987
&& md.getParameters().get(0).getTypeAsString().equals("String"));
9088

9189
if (!alreadyHasResourceValidationCallPresent) {
90+
// one might be tempted to cache this result, but then it will be a shared resource with
91+
// shared CST metadata and cause bugs
92+
MethodDeclaration fixMethod = StaticJavaParser.parseMethodDeclaration(fixMethodCode);
93+
94+
// add the method to the class
9295
parentClass.addMember(fixMethod);
9396
}
9497
}

0 commit comments

Comments
 (0)