From 7830c738224b96833907cdfd5db4017bad0613f3 Mon Sep 17 00:00:00 2001 From: Rob Stryker Date: Tue, 5 Dec 2023 10:34:23 -0500 Subject: [PATCH] Fixes #3564 - Duplicate StringBuffer calls to StringBuilder calls Signed-off-by: Rob Stryker --- .../core/configuration/ConfigurationFile.java | 2 +- .../eclipse/handlers/EclipseHandlerUtil.java | 4 +--- .../lombok/eclipse/agent/EclipsePatcher.java | 9 ++++++++- .../lombok/eclipse/agent/PatchJavadoc.java | 15 ++++++++++++++ .../lombok/launch/PatchFixesHider.java | 20 +++++++++++++++---- 5 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/core/lombok/core/configuration/ConfigurationFile.java b/src/core/lombok/core/configuration/ConfigurationFile.java index 8e4e687c..121ad301 100644 --- a/src/core/lombok/core/configuration/ConfigurationFile.java +++ b/src/core/lombok/core/configuration/ConfigurationFile.java @@ -183,7 +183,7 @@ public abstract class ConfigurationFile { 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", "~")); diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index dd646e57..a6df2e12 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -1331,9 +1331,7 @@ public class EclipseHandlerUtil { 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)); } diff --git a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java index 52e62342..09c86e0d 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java +++ b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java @@ -986,6 +986,13 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable { .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")) @@ -1050,7 +1057,7 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable { 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()); diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java b/src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java index 673c30fd..54c08ec0 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java @@ -65,6 +65,21 @@ public class PatchJavadoc { return null; } + public static StringBuilder printMethod(AbstractMethodDeclaration methodDeclaration, Integer tab, StringBuilder output, TypeDeclaration type) { + Map 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 docs = CompilationUnit_javadoc.get(methodDeclaration.compilationResult.compilationUnit); if (docs != null) { diff --git a/src/eclipseAgent/lombok/launch/PatchFixesHider.java b/src/eclipseAgent/lombok/launch/PatchFixesHider.java index a9db91f5..1a42f18a 100755 --- a/src/eclipseAgent/lombok/launch/PatchFixesHider.java +++ b/src/eclipseAgent/lombok/launch/PatchFixesHider.java @@ -427,20 +427,28 @@ final class PatchFixesHider { /** 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); } } @@ -977,7 +985,11 @@ final class PatchFixesHider { 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; } }