From 7830c738224b96833907cdfd5db4017bad0613f3 Mon Sep 17 00:00:00 2001 From: Rob Stryker Date: Tue, 5 Dec 2023 10:34:23 -0500 Subject: [PATCH 01/10] 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; } } From bd1f2adad7382cc976a38dcfc4043099e8df9141 Mon Sep 17 00:00:00 2001 From: Rob Stryker Date: Tue, 5 Dec 2023 16:41:04 -0500 Subject: [PATCH 02/10] Fix some compilation errors Signed-off-by: Rob Stryker --- src/eclipseAgent/lombok/launch/PatchFixesHider.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/eclipseAgent/lombok/launch/PatchFixesHider.java b/src/eclipseAgent/lombok/launch/PatchFixesHider.java index 1a42f18a..46bb59ec 100755 --- a/src/eclipseAgent/lombok/launch/PatchFixesHider.java +++ b/src/eclipseAgent/lombok/launch/PatchFixesHider.java @@ -433,11 +433,8 @@ final class PatchFixesHider { static { Class shadowed = Util.shadowLoadClass("lombok.eclipse.agent.PatchJavadoc"); GET_HTML = Util.findMethod(shadowed, "getHTMLContentFromSource", String.class, Object.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); - } + PRINT_METHOD_NEW = Util.findMethod(shadowed, "printMethod", AbstractMethodDeclaration.class, Integer.class, StringBuilder.class, TypeDeclaration.class); + PRINT_METHOD_OLD = Util.findMethod(shadowed, "printMethod", AbstractMethodDeclaration.class, Integer.class, StringBuffer.class, TypeDeclaration.class); } public static String getHTMLContentFromSource(String original, IJavaElement member) { @@ -985,7 +982,7 @@ final class PatchFixesHider { return (method.bits & IsCanonicalConstructor) != 0 && (method.bits & IsImplicit) != 0; } - public static StringBuilder returnStringBuffer(Object p1, StringBuffer buffer) { + public static StringBuffer returnStringBuffer(Object p1, StringBuffer buffer) { return buffer; } From 75116df7f560c4e13c10c2f6685813113c73fd1b Mon Sep 17 00:00:00 2001 From: Rob Stryker Date: Tue, 5 Dec 2023 16:45:07 -0500 Subject: [PATCH 03/10] Add TODO with where to add reflection Signed-off-by: Rob Stryker --- src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java b/src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java index 54c08ec0..c2618d0b 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java @@ -72,10 +72,12 @@ public class PatchJavadoc { String rawJavadoc = docs.get(signature); if (rawJavadoc != null) { for (String line : rawJavadoc.split("\r?\n")) { + // TODO call this via reflection ASTNode.printIndent(tab, output).append(line).append("\n"); } } } + // TODO call this via reflection return methodDeclaration.print(tab, output); } @@ -87,10 +89,12 @@ public class PatchJavadoc { String rawJavadoc = docs.get(signature); if (rawJavadoc != null) { for (String line : rawJavadoc.split("\r?\n")) { + // TODO call this via reflection ASTNode.printIndent(tab, output).append(line).append("\n"); } } } + // TODO call this via reflection return methodDeclaration.print(tab, output); } From bd03b512ba45bc0d6b99e118787b8b0f4b706d9e Mon Sep 17 00:00:00 2001 From: Rob Stryker Date: Tue, 5 Dec 2023 16:55:27 -0500 Subject: [PATCH 04/10] Copy the indentation code instead of requiring dep on jdt internal Signed-off-by: Rob Stryker --- .../lombok/eclipse/agent/PatchJavadoc.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java b/src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java index c2618d0b..fb68b483 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java @@ -72,8 +72,7 @@ public class PatchJavadoc { String rawJavadoc = docs.get(signature); if (rawJavadoc != null) { for (String line : rawJavadoc.split("\r?\n")) { - // TODO call this via reflection - ASTNode.printIndent(tab, output).append(line).append("\n"); + printIndent(tab, output).append(line).append("\n"); } } } @@ -89,8 +88,7 @@ public class PatchJavadoc { String rawJavadoc = docs.get(signature); if (rawJavadoc != null) { for (String line : rawJavadoc.split("\r?\n")) { - // TODO call this via reflection - ASTNode.printIndent(tab, output).append(line).append("\n"); + printIndent(tab, output).append(line).append("\n"); } } } @@ -98,6 +96,16 @@ public class PatchJavadoc { return methodDeclaration.print(tab, output); } + private static StringBuilder printIndent(int indent, StringBuilder output) { + for (int i = indent; i > 0; i--) output.append(" "); //$NON-NLS-1$ + return output; + } + + private static StringBuffer printIndent(int indent, StringBuffer output) { + for (int i = indent; i > 0; i--) output.append(" "); //$NON-NLS-1$ + return output; + } + private static class Signature { static final String getSignature(SourceMethod sourceMethod) { StringBuilder sb = new StringBuilder(); From c784c7bb7ab80bbc5f78abae60e4f203742d74f3 Mon Sep 17 00:00:00 2001 From: Rob Stryker Date: Tue, 5 Dec 2023 16:57:54 -0500 Subject: [PATCH 05/10] One compilation error left Signed-off-by: Rob Stryker --- src/core/lombok/core/configuration/ConfigurationFile.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/lombok/core/configuration/ConfigurationFile.java b/src/core/lombok/core/configuration/ConfigurationFile.java index 121ad301..8e4e687c 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; - StringBuilder result = new StringBuilder(); + StringBuffer result = new StringBuffer(); if (fileName.startsWith("~")) { start = 1; result.append(System.getProperty("user.home", "~")); From c64c5a93110b17662641a5071b5efffe1d41c348 Mon Sep 17 00:00:00 2001 From: Rob Stryker Date: Wed, 6 Dec 2023 02:01:40 -0500 Subject: [PATCH 06/10] Get patch to compile Signed-off-by: Rob Stryker --- .../lombok/eclipse/agent/PatchJavadoc.java | 70 +++++++++++++++++-- 1 file changed, 66 insertions(+), 4 deletions(-) diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java b/src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java index fb68b483..077d748a 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java @@ -24,6 +24,7 @@ package lombok.eclipse.agent; import static lombok.eclipse.EcjAugments.CompilationUnit_javadoc; import java.lang.reflect.Method; +import java.lang.reflect.InvocationTargetException; import java.util.Map; import org.eclipse.jdt.core.ICompilationUnit; @@ -76,8 +77,34 @@ public class PatchJavadoc { } } } - // TODO call this via reflection - return methodDeclaration.print(tab, output); + String methodName = "print"; + Method builderMethod = findMethodOrNull(AbstractMethodDeclaration.class, methodName, int.class, StringBuilder.class); + if( builderMethod != null ) { + try { + return (StringBuilder)builderMethod.invoke(methodDeclaration, tab, output); + } catch( IllegalAccessException e ) { + // ignore + } catch( InvocationTargetException f ) { + // ignore + } + } + + // Should never happen + Method bufferMethod = findMethodOrNull(AbstractMethodDeclaration.class, methodName, int.class, StringBuffer.class); + if( bufferMethod != null ) { + try { + StringBuffer sb = new StringBuffer(); + StringBuffer out = (StringBuffer)bufferMethod.invoke(methodDeclaration, tab, sb); + output.append(out.toString()); + return output; + } catch( IllegalAccessException e ) { + // ignore + } catch( InvocationTargetException f ) { + // ignore + } + } + // Should really never happen + return output; } @@ -92,8 +119,43 @@ public class PatchJavadoc { } } } - // TODO call this via reflection - return methodDeclaration.print(tab, output); + + String methodName = "print"; + Method bufferMethod = findMethodOrNull(AbstractMethodDeclaration.class, methodName, int.class, StringBuffer.class); + if( bufferMethod != null ) { + try { + return (StringBuffer)bufferMethod.invoke(methodDeclaration, tab, output); + } catch( IllegalAccessException e ) { + // ignore + } catch( InvocationTargetException f ) { + // ignore + } + } + + // Should never happen + Method builderMethod = findMethodOrNull(AbstractMethodDeclaration.class, methodName, int.class, StringBuilder.class); + if( builderMethod != null ) { + try { + StringBuilder sb = new StringBuilder(); + StringBuilder out = (StringBuilder)builderMethod.invoke(methodDeclaration, tab, sb); + output.append(out.toString()); + return output; + } catch( IllegalAccessException e ) { + // ignore + } catch( InvocationTargetException f ) { + // ignore + } + } + // Should really never happen + return output; + } + + public static Method findMethodOrNull(Class type, String name, Class... parameterTypes) { + try { + return type.getDeclaredMethod(name, parameterTypes); + } catch (NoSuchMethodException e) { + return null; + } } private static StringBuilder printIndent(int indent, StringBuilder output) { From 9d55da8e6d9cdbec1f00c15c739c6654ec1c6946 Mon Sep 17 00:00:00 2001 From: Rawi01 Date: Mon, 11 Dec 2023 23:46:23 +0100 Subject: [PATCH 07/10] Simplify, move and fix patches --- .../lombok/eclipse/agent/EclipsePatcher.java | 46 +++++--- .../lombok/eclipse/agent/PatchJavadoc.java | 102 ------------------ .../lombok/launch/PatchFixesHider.java | 34 +++++- 3 files changed, 62 insertions(+), 120 deletions(-) diff --git a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java index 09c86e0d..1616d956 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java +++ b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java @@ -985,20 +985,6 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable { .wrapMethod(new Hook("lombok.launch.PatchFixesHider$Javadoc", "getHTMLContentFromSource", "java.lang.String", "java.lang.String", "org.eclipse.jdt.core.IJavaElement")) .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")) - .replacementMethod(new Hook("lombok.launch.PatchFixesHider$Javadoc", "printMethod", "java.lang.StringBuffer", "org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration", "int", "java.lang.StringBuffer", "org.eclipse.jdt.internal.compiler.ast.TypeDeclaration")) - .requestExtra(StackRequest.THIS) - .build()); sm.addScriptIfWitness(OSGI_TYPES, ScriptBuilder.addField() .fieldName("$javadoc") @@ -1048,19 +1034,45 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable { } private static void patchForTests(ScriptManager sm) { - sm.addScriptIfWitness(new String[] {"lombok/eclipse/EclipseRunner"}, ScriptBuilder.wrapReturnValue() + String[] ECLIPSE_TEST_CLASSES = new String[] {"lombok/transform/TestWithEcj", "lombok/eclipse/EclipseRunner"}; + + // Add support for javadoc in tests + sm.addScriptIfWitness(ECLIPSE_TEST_CLASSES, 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$Tests", "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.addScriptIfWitness(ECLIPSE_TEST_CLASSES, 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")) + .replacementMethod(new Hook("lombok.launch.PatchFixesHider$Tests", "printMethod", "java.lang.StringBuffer", "org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration", "int", "java.lang.StringBuffer", "org.eclipse.jdt.internal.compiler.ast.TypeDeclaration")) + .requestExtra(StackRequest.THIS) + .build()); + + sm.addScriptIfWitness(ECLIPSE_TEST_CLASSES, ScriptBuilder.wrapReturnValue() .target(new MethodTarget("org.osgi.framework.FrameworkUtil", "getBundle", "org.osgi.framework.Bundle", "java.lang.Class")) .request(StackRequest.RETURN_VALUE, StackRequest.PARAM1) .wrapMethod(new Hook("lombok.launch.PatchFixesHider$Tests", "getBundle", "java.lang.Object", "java.lang.Object", "java.lang.Class")) .build()); - sm.addScriptIfWitness(new String[] {"lombok/transform/TestWithEcj"}, ScriptBuilder.exitEarly() - .target(new MethodTarget("org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration", "print")) + // Remove implicit canonical constructors in tests + sm.addScriptIfWitness(ECLIPSE_TEST_CLASSES, ScriptBuilder.exitEarly() + .target(new MethodTarget("org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration", "print", "java.lang.StringBuilder", "int", "java.lang.StringBuilder")) .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", "returnStringBuilder", "java.lang.StringBuilder", "java.lang.Object", "java.lang.StringBuilder")) .request(StackRequest.THIS, StackRequest.PARAM2) .transplant() .build()); + + sm.addScriptIfWitness(ECLIPSE_TEST_CLASSES, ScriptBuilder.exitEarly() + .target(new MethodTarget("org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration", "print", "java.lang.StringBuffer", "int", "java.lang.StringBuffer")) + .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")) + .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 077d748a..f270e19f 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java @@ -65,108 +65,6 @@ 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")) { - printIndent(tab, output).append(line).append("\n"); - } - } - } - String methodName = "print"; - Method builderMethod = findMethodOrNull(AbstractMethodDeclaration.class, methodName, int.class, StringBuilder.class); - if( builderMethod != null ) { - try { - return (StringBuilder)builderMethod.invoke(methodDeclaration, tab, output); - } catch( IllegalAccessException e ) { - // ignore - } catch( InvocationTargetException f ) { - // ignore - } - } - - // Should never happen - Method bufferMethod = findMethodOrNull(AbstractMethodDeclaration.class, methodName, int.class, StringBuffer.class); - if( bufferMethod != null ) { - try { - StringBuffer sb = new StringBuffer(); - StringBuffer out = (StringBuffer)bufferMethod.invoke(methodDeclaration, tab, sb); - output.append(out.toString()); - return output; - } catch( IllegalAccessException e ) { - // ignore - } catch( InvocationTargetException f ) { - // ignore - } - } - // Should really never happen - return output; - } - - - public static StringBuffer printMethod(AbstractMethodDeclaration methodDeclaration, Integer tab, StringBuffer 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")) { - printIndent(tab, output).append(line).append("\n"); - } - } - } - - String methodName = "print"; - Method bufferMethod = findMethodOrNull(AbstractMethodDeclaration.class, methodName, int.class, StringBuffer.class); - if( bufferMethod != null ) { - try { - return (StringBuffer)bufferMethod.invoke(methodDeclaration, tab, output); - } catch( IllegalAccessException e ) { - // ignore - } catch( InvocationTargetException f ) { - // ignore - } - } - - // Should never happen - Method builderMethod = findMethodOrNull(AbstractMethodDeclaration.class, methodName, int.class, StringBuilder.class); - if( builderMethod != null ) { - try { - StringBuilder sb = new StringBuilder(); - StringBuilder out = (StringBuilder)builderMethod.invoke(methodDeclaration, tab, sb); - output.append(out.toString()); - return output; - } catch( IllegalAccessException e ) { - // ignore - } catch( InvocationTargetException f ) { - // ignore - } - } - // Should really never happen - return output; - } - - public static Method findMethodOrNull(Class type, String name, Class... parameterTypes) { - try { - return type.getDeclaredMethod(name, parameterTypes); - } catch (NoSuchMethodException e) { - return null; - } - } - - private static StringBuilder printIndent(int indent, StringBuilder output) { - for (int i = indent; i > 0; i--) output.append(" "); //$NON-NLS-1$ - return output; - } - - private static StringBuffer printIndent(int indent, StringBuffer output) { - for (int i = indent; i > 0; i--) output.append(" "); //$NON-NLS-1$ - return output; - } private static class Signature { static final String getSignature(SourceMethod sourceMethod) { diff --git a/src/eclipseAgent/lombok/launch/PatchFixesHider.java b/src/eclipseAgent/lombok/launch/PatchFixesHider.java index 46bb59ec..6c3459bf 100755 --- a/src/eclipseAgent/lombok/launch/PatchFixesHider.java +++ b/src/eclipseAgent/lombok/launch/PatchFixesHider.java @@ -21,7 +21,7 @@ */ package lombok.launch; -import static lombok.eclipse.EcjAugments.ASTNode_generatedBy; +import static lombok.eclipse.EcjAugments.*; import static lombok.eclipse.Eclipse.*; import java.io.BufferedOutputStream; @@ -33,6 +33,7 @@ import java.lang.reflect.Method; import java.security.CodeSource; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Stack; import org.eclipse.core.runtime.CoreException; @@ -70,6 +71,7 @@ import org.eclipse.jdt.internal.corext.refactoring.code.CallContext; import org.eclipse.jdt.internal.corext.refactoring.code.SourceProvider; import org.eclipse.jdt.internal.corext.refactoring.structure.MemberVisibilityAdjustor.IncomingMemberVisibilityAdjustment; +import lombok.eclipse.handlers.EclipseHandlerUtil; import lombok.permit.Permit; /** These contain a mix of the following: @@ -956,6 +958,36 @@ final class PatchFixesHider { } public static class Tests { + public static StringBuffer printMethod(AbstractMethodDeclaration methodDeclaration, int tab, StringBuffer output, TypeDeclaration type) { + return (StringBuffer) printMethod(methodDeclaration, tab, (Object) output, type); + } + + public static StringBuilder printMethod(AbstractMethodDeclaration methodDeclaration, int tab, StringBuilder output, TypeDeclaration type) { + return (StringBuilder) printMethod(methodDeclaration, tab, (Object) output, type); + } + + public static Object printMethod(AbstractMethodDeclaration methodDeclaration, int tab, Object output, TypeDeclaration type) { + Map docs = CompilationUnit_javadoc.get(methodDeclaration.compilationResult.compilationUnit); + Method printIndent = Permit.permissiveGetMethod(org.eclipse.jdt.internal.compiler.ast.ASTNode.class, "printIndent", int.class, output.getClass()); + if (docs != null) { + String signature = EclipseHandlerUtil.getSignature(type, methodDeclaration); + String rawJavadoc = docs.get(signature); + if (rawJavadoc != null) { + for (String line : rawJavadoc.split("\r?\n")) { + try { + Appendable sb = (Appendable) Permit.invoke(printIndent, null, tab, output); + sb.append(line).append("\n"); + } catch (Throwable e) { + // Ignore + } + } + } + } + Method printMethodDeclaration = Permit.permissiveGetMethod(AbstractMethodDeclaration.class, "print", int.class, output.getClass()); + Permit.invokeSneaky(printMethodDeclaration, methodDeclaration, tab, output); + return output; + } + public static Object getBundle(Object original, Class c) { if (original != null) { return original; From 3d9018a4789d2903f389e164545ffc8adbca0e2d Mon Sep 17 00:00:00 2001 From: Rob Stryker Date: Tue, 19 Dec 2023 01:32:11 -0500 Subject: [PATCH 08/10] Update authors file Signed-off-by: Rob Stryker --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index bbbebb7c..a3e5fcf1 100755 --- a/AUTHORS +++ b/AUTHORS @@ -41,6 +41,7 @@ Pim van der Loos Rabea Gransberger Raul Wißfeld Reinier Zwitserloot +Rob Stryker Robbert Jan Grootjans Robert Wertman Roel Spilker From f704aba40d7a18edbd21c71f220bcc88f12b2809 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Fri, 12 Jan 2024 00:26:46 +0100 Subject: [PATCH 09/10] [trivial] style --- src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java index 1616d956..4d658516 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java +++ b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java @@ -985,7 +985,7 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable { .wrapMethod(new Hook("lombok.launch.PatchFixesHider$Javadoc", "getHTMLContentFromSource", "java.lang.String", "java.lang.String", "org.eclipse.jdt.core.IJavaElement")) .requestExtra(StackRequest.PARAM1) .build()); - + sm.addScriptIfWitness(OSGI_TYPES, ScriptBuilder.addField() .fieldName("$javadoc") .fieldType("Ljava/util/Map;") @@ -1043,7 +1043,7 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable { .replacementMethod(new Hook("lombok.launch.PatchFixesHider$Tests", "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.addScriptIfWitness(ECLIPSE_TEST_CLASSES, 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")) From e5e2e38f45f38773ecf61130b59b0e4c20836703 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Fri, 12 Jan 2024 00:38:11 +0100 Subject: [PATCH 10/10] [changelog] [#3564] Update changelog to reflect eclipse StringBuffer/StringBuilder fix --- doc/changelog.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/changelog.markdown b/doc/changelog.markdown index d8828870..0b0802d7 100644 --- a/doc/changelog.markdown +++ b/doc/changelog.markdown @@ -2,6 +2,7 @@ Lombok Changelog ---------------- ### v1.18.31 "Edgy Guinea Pig" +* PLATFORM: Added support for recent versions of eclipse (released Q4 2023 or later or so) which would cause failures in the eclipse logs such as `java.lang.NoSuchMethodError: 'java.lang.StringBuffer org.eclipse.jdt…`. [Issue #3564](https://github.com/projectlombok/lombok/issues/3564). * FEATURE: `@Locked` has been introduced. Like `@Synchronized` but with `java.util.concurrent.locks` locks instead of the `synchronized` primitive. Thanks, Pim van der Loos for the PR! [Issue #3506](https://github.com/projectlombok/lombok/issues/3506). * We recently released v1.18.30; there is no edge release since then.