Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 3564 - Replace StringBuffer with StringBuilder to run against new eclipse ibuilds #3565

Merged
merged 8 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Pim van der Loos <[email protected]>
Rabea Gransberger <[email protected]>
Raul Wißfeld <[email protected]>
Reinier Zwitserloot <[email protected]>
Rob Stryker <[email protected]>
Robbert Jan Grootjans <[email protected]>
Robert Wertman <[email protected]>
Roel Spilker <[email protected]>
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
39 changes: 29 additions & 10 deletions src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -985,13 +985,6 @@ private static void patchJavadoc(ScriptManager sm) {
.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.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")
Expand Down Expand Up @@ -1041,14 +1034,40 @@ private static void patchCrossModuleClassLoading(ScriptManager sm) {
}

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)
Expand Down
17 changes: 2 additions & 15 deletions src/eclipseAgent/lombok/eclipse/agent/PatchJavadoc.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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;
Expand Down Expand Up @@ -64,21 +65,7 @@ public static String getHTMLContentFromSource(String original, Object member) {

return null;
}

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) {
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);
}


private static class Signature {
static final String getSignature(SourceMethod sourceMethod) {
StringBuilder sb = new StringBuilder();
Expand Down
49 changes: 45 additions & 4 deletions src/eclipseAgent/lombok/launch/PatchFixesHider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,6 +33,7 @@
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;
Expand Down Expand Up @@ -70,6 +71,7 @@
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:
Expand Down Expand Up @@ -427,20 +429,25 @@ 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);
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) {
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 @@ -951,6 +958,36 @@ public static String[] getRealCodeBlocks(String[] blocks, SourceProvider sourceP
}

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<String, String> 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;
Expand Down Expand Up @@ -980,5 +1017,9 @@ public static boolean isImplicitCanonicalConstructor(AbstractMethodDeclaration m
public static StringBuffer returnStringBuffer(Object p1, StringBuffer buffer) {
return buffer;
}

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