From 9d55da8e6d9cdbec1f00c15c739c6654ec1c6946 Mon Sep 17 00:00:00 2001 From: Rawi01 Date: Mon, 11 Dec 2023 23:46:23 +0100 Subject: [PATCH] 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;