Skip to content

Commit

Permalink
Fixes #3564 - Duplicate StringBuffer calls to StringBuilder calls
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Stryker <[email protected]>
  • Loading branch information
robstryker committed Dec 5, 2023
1 parent d6ee06c commit 7830c73
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/core/lombok/core/configuration/ConfigurationFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ CharSequence contents() throws IOException {

private static String replaceEnvironmentVariables(String fileName) {
int start = 0;
StringBuffer result = new StringBuffer();
StringBuilder result = new StringBuilder();
if (fileName.startsWith("~")) {
start = 1;
result.append(System.getProperty("user.home", "~"));
Expand Down
4 changes: 1 addition & 3 deletions src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1331,9 +1331,7 @@ public static TypeReference makeType(TypeBinding binding, ASTNode pos, boolean a
expressions = new Expression[] { rhs };
}
if (expressions != null) for (Expression ex : expressions) {
StringBuffer sb = new StringBuffer();
ex.print(0, sb);
raws.add(sb.toString());
raws.add(ex.toString());
expressionValues.add(ex);
guesses.add(calculateValue(ex));
}
Expand Down
9 changes: 8 additions & 1 deletion src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,13 @@ private static void patchJavadoc(ScriptManager sm) {
.requestExtra(StackRequest.PARAM1)
.build());

sm.addScript(ScriptBuilder.replaceMethodCall()
.target(new MethodTarget("org.eclipse.jdt.internal.compiler.ast.TypeDeclaration", "printBody", "java.lang.StringBuilder", "int", "java.lang.StringBuilder"))
.methodToReplace(new Hook("org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration", "print", "java.lang.StringBuilder", "int", "java.lang.StringBuilder"))
.replacementMethod(new Hook("lombok.launch.PatchFixesHider$Javadoc", "printMethod", "java.lang.StringBuilder", "org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration", "int", "java.lang.StringBuilder", "org.eclipse.jdt.internal.compiler.ast.TypeDeclaration"))
.requestExtra(StackRequest.THIS)
.build());

sm.addScript(ScriptBuilder.replaceMethodCall()
.target(new MethodTarget("org.eclipse.jdt.internal.compiler.ast.TypeDeclaration", "printBody", "java.lang.StringBuffer", "int", "java.lang.StringBuffer"))
.methodToReplace(new Hook("org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration", "print", "java.lang.StringBuffer", "int", "java.lang.StringBuffer"))
Expand Down Expand Up @@ -1050,7 +1057,7 @@ private static void patchForTests(ScriptManager sm) {
sm.addScriptIfWitness(new String[] {"lombok/transform/TestWithEcj"}, ScriptBuilder.exitEarly()
.target(new MethodTarget("org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration", "print"))
.decisionMethod(new Hook("lombok.launch.PatchFixesHider$Tests", "isImplicitCanonicalConstructor", "boolean", "org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration", "java.lang.Object"))
.valueMethod(new Hook("lombok.launch.PatchFixesHider$Tests", "returnStringBuffer", "java.lang.StringBuffer", "java.lang.Object", "java.lang.StringBuffer"))
.valueMethod(new Hook("lombok.launch.PatchFixesHider$Tests", "returnStringBuilder", "java.lang.StringBuilder", "java.lang.Object", "java.lang.StringBuilder"))
.request(StackRequest.THIS, StackRequest.PARAM2)
.transplant()
.build());
Expand Down
15 changes: 15 additions & 0 deletions src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ public static String getHTMLContentFromSource(String original, Object member) {
return null;
}

public static StringBuilder printMethod(AbstractMethodDeclaration methodDeclaration, Integer tab, StringBuilder output, TypeDeclaration type) {
Map<String, String> docs = CompilationUnit_javadoc.get(methodDeclaration.compilationResult.compilationUnit);
if (docs != null) {
String signature = EclipseHandlerUtil.getSignature(type, methodDeclaration);
String rawJavadoc = docs.get(signature);
if (rawJavadoc != null) {
for (String line : rawJavadoc.split("\r?\n")) {
ASTNode.printIndent(tab, output).append(line).append("\n");
}
}
}
return methodDeclaration.print(tab, output);
}


public static StringBuffer printMethod(AbstractMethodDeclaration methodDeclaration, Integer tab, StringBuffer output, TypeDeclaration type) {
Map<String, String> docs = CompilationUnit_javadoc.get(methodDeclaration.compilationResult.compilationUnit);
if (docs != null) {
Expand Down
20 changes: 16 additions & 4 deletions src/eclipseAgent/lombok/launch/PatchFixesHider.java
Original file line number Diff line number Diff line change
Expand Up @@ -427,20 +427,28 @@ public static Object modifyMethodPattern(Object original) {
/** Contains patch code to support Javadoc for generated methods */
public static final class Javadoc {
private static final Method GET_HTML;
private static final Method PRINT_METHOD;
private static final Method PRINT_METHOD_OLD;
private static final Method PRINT_METHOD_NEW;

static {
Class<?> shadowed = Util.shadowLoadClass("lombok.eclipse.agent.PatchJavadoc");
GET_HTML = Util.findMethod(shadowed, "getHTMLContentFromSource", String.class, Object.class);
PRINT_METHOD = Util.findMethod(shadowed, "printMethod", AbstractMethodDeclaration.class, Integer.class, StringBuffer.class, TypeDeclaration.class);
try {
PRINT_METHOD_NEW = Util.findMethod(shadowed, "printMethod", AbstractMethodDeclaration.class, Integer.class, StringBuilder.class, TypeDeclaration.class);
} catch( RuntimeException e ) {
PRINT_METHOD_OLD = Util.findMethod(shadowed, "printMethod", AbstractMethodDeclaration.class, Integer.class, StringBuffer.class, TypeDeclaration.class);
}
}

public static String getHTMLContentFromSource(String original, IJavaElement member) {
return (String) Util.invokeMethod(GET_HTML, original, member);
}

public static StringBuilder printMethod(AbstractMethodDeclaration methodDeclaration, int tab, StringBuilder output, TypeDeclaration type) {
return (StringBuilder) Util.invokeMethod(PRINT_METHOD_NEW, methodDeclaration, tab, output, type);
}
public static StringBuffer printMethod(AbstractMethodDeclaration methodDeclaration, int tab, StringBuffer output, TypeDeclaration type) {
return (StringBuffer) Util.invokeMethod(PRINT_METHOD, methodDeclaration, tab, output, type);
return (StringBuffer) Util.invokeMethod(PRINT_METHOD_OLD, methodDeclaration, tab, output, type);
}
}

Expand Down Expand Up @@ -977,7 +985,11 @@ public static boolean isImplicitCanonicalConstructor(AbstractMethodDeclaration m
return (method.bits & IsCanonicalConstructor) != 0 && (method.bits & IsImplicit) != 0;
}

public static StringBuffer returnStringBuffer(Object p1, StringBuffer buffer) {
public static StringBuilder returnStringBuffer(Object p1, StringBuffer buffer) {
return buffer;
}

public static StringBuilder returnStringBuilder(Object p1, StringBuilder buffer) {
return buffer;
}
}
Expand Down

0 comments on commit 7830c73

Please sign in to comment.