From 28fbff467b3f3f3f5a4128313de02c9b1b2cb41d Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Fri, 30 Oct 2020 04:48:03 +0100 Subject: [issue #285] Compiling with Maven+Tycho should now work --- .../lombok/eclipse/agent/EclipsePatcher.java | 9 ++-- .../lombok/eclipse/agent/PatchExtensionMethod.java | 2 +- .../eclipse/agent/PatchExtensionMethodPortal.java | 12 ++--- .../lombok/launch/PatchFixesHider.java | 61 +++++++++++++--------- src/launch/lombok/launch/Main.java | 5 ++ src/launch/lombok/launch/ShadowClassLoader.java | 24 +++++++++ 6 files changed, 78 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java index 13a7639d..d5077b86 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java +++ b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java @@ -669,15 +669,16 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable { final String PARSER_SIG = "org.eclipse.jdt.internal.compiler.parser.Parser"; final String CUD_SIG = "org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration"; + final String OBJECT_SIG = "java.lang.Object"; sm.addScript(ScriptBuilder.wrapReturnValue() .target(new MethodTarget(PARSER_SIG, "getMethodBodies", "void", CUD_SIG)) - .wrapMethod(new Hook("lombok.launch.PatchFixesHider$Transform", "transform", "void", PARSER_SIG, CUD_SIG)) + .wrapMethod(new Hook("lombok.launch.PatchFixesHider$Transform", "transform", "void", OBJECT_SIG, OBJECT_SIG)) .request(StackRequest.THIS, StackRequest.PARAM1).build()); sm.addScript(ScriptBuilder.wrapReturnValue() .target(new MethodTarget(PARSER_SIG, "endParse", CUD_SIG, "int")) - .wrapMethod(new Hook("lombok.launch.PatchFixesHider$Transform", "transform_swapped", "void", CUD_SIG, PARSER_SIG)) + .wrapMethod(new Hook("lombok.launch.PatchFixesHider$Transform", "transform_swapped", "void", OBJECT_SIG, OBJECT_SIG)) .request(StackRequest.THIS, StackRequest.RETURN_VALUE).build()); } @@ -835,13 +836,15 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable { final String COMPLETION_PROPOSAL_COLLECTOR_SIG = "org.eclipse.jdt.ui.text.java.CompletionProposalCollector"; final String I_JAVA_COMPLETION_PROPOSAL_SIG = "org.eclipse.jdt.ui.text.java.IJavaCompletionProposal[]"; final String AST_NODE = "org.eclipse.jdt.internal.compiler.ast.ASTNode"; + final String OBJECT_SIG = "java.lang.Object"; sm.addScript(wrapReturnValue() .target(new MethodTarget(MESSAGE_SEND_SIG, "resolveType", TYPE_BINDING_SIG, BLOCK_SCOPE_SIG)) .request(StackRequest.RETURN_VALUE) .request(StackRequest.THIS) .request(StackRequest.PARAM1) - .wrapMethod(new Hook(PATCH_EXTENSIONMETHOD, "resolveType", TYPE_BINDING_SIG, TYPE_BINDING_SIG, MESSAGE_SEND_SIG, BLOCK_SCOPE_SIG)) + .wrapMethod(new Hook(PATCH_EXTENSIONMETHOD, "resolveType", OBJECT_SIG, OBJECT_SIG, MESSAGE_SEND_SIG, BLOCK_SCOPE_SIG)) + .cast() .build()); sm.addScript(replaceMethodCall() diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java index 18e2a8db..0f602671 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java @@ -246,7 +246,7 @@ public class PatchExtensionMethod { MessageSend_postponedErrors.set(messageSend, new PostponedNonStaticAccessToStaticMethodError(problemReporter, location, method)); } - public static TypeBinding resolveType(TypeBinding resolvedType, MessageSend methodCall, BlockScope scope) { + public static Object resolveType(Object resolvedType, MessageSend methodCall, BlockScope scope) { List extensions = new ArrayList(); TypeDeclaration decl = scope.classScope().referenceContext; diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethodPortal.java b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethodPortal.java index b66bafbb..90a23c20 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethodPortal.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethodPortal.java @@ -5,8 +5,6 @@ import java.lang.reflect.Method; import lombok.Lombok; -import org.eclipse.jdt.internal.compiler.lookup.TypeBinding; - public class PatchExtensionMethodPortal { private static final String TYPE_BINDING = "org.eclipse.jdt.internal.compiler.lookup.TypeBinding"; private static final String TYPE_BINDING_ARRAY = "[Lorg.eclipse.jdt.internal.compiler.lookup.TypeBinding;"; @@ -15,13 +13,13 @@ public class PatchExtensionMethodPortal { private static final String METHOD_BINDING = "org.eclipse.jdt.internal.compiler.lookup.MethodBinding"; private static final String PROBLEM_REPORTER = "org.eclipse.jdt.internal.compiler.problem.ProblemReporter"; - public static TypeBinding resolveType(Object resolvedType, Object methodCall, Object scope) { + public static Object resolveType(Object resolvedType, Object methodCall, Object scope) { try { - return (TypeBinding) Reflection.resolveType.invoke(null, resolvedType, methodCall, scope); + return Reflection.resolveType.invoke(null, resolvedType, methodCall, scope); } catch (NoClassDefFoundError e) { //ignore, we don't have access to the correct ECJ classes, so lombok can't possibly //do anything useful here. - return (TypeBinding) resolvedType; + return resolvedType; } catch (IllegalAccessException e) { throw Lombok.sneakyThrow(e); } catch (InvocationTargetException e) { @@ -33,7 +31,7 @@ public class PatchExtensionMethodPortal { } //ignore, we don't have access to the correct ECJ classes, so lombok can't possibly //do anything useful here. - return (TypeBinding)resolvedType; + return resolvedType; } } @@ -85,7 +83,7 @@ public class PatchExtensionMethodPortal { Method m = null, n = null, o = null; Throwable problem_ = null; try { - m = PatchExtensionMethod.class.getMethod("resolveType", Class.forName(TYPE_BINDING), Class.forName(MESSAGE_SEND), Class.forName(BLOCK_SCOPE)); + m = PatchExtensionMethod.class.getMethod("resolveType", Object.class, Class.forName(MESSAGE_SEND), Class.forName(BLOCK_SCOPE)); n = PatchExtensionMethod.class.getMethod("errorNoMethodFor", Class.forName(PROBLEM_REPORTER), Class.forName(MESSAGE_SEND), Class.forName(TYPE_BINDING), Class.forName(TYPE_BINDING_ARRAY)); o = PatchExtensionMethod.class.getMethod("invalidMethod", Class.forName(PROBLEM_REPORTER), Class.forName(MESSAGE_SEND), Class.forName(METHOD_BINDING)); diff --git a/src/eclipseAgent/lombok/launch/PatchFixesHider.java b/src/eclipseAgent/lombok/launch/PatchFixesHider.java index 55e8e0bf..503b2b4a 100755 --- a/src/eclipseAgent/lombok/launch/PatchFixesHider.java +++ b/src/eclipseAgent/lombok/launch/PatchFixesHider.java @@ -46,7 +46,6 @@ import org.eclipse.jdt.core.search.SearchMatch; import org.eclipse.jdt.internal.compiler.ast.ASTNode; import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration; import org.eclipse.jdt.internal.compiler.ast.Annotation; -import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; import org.eclipse.jdt.internal.compiler.ast.Expression; import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; import org.eclipse.jdt.internal.compiler.ast.ForeachStatement; @@ -57,7 +56,6 @@ import org.eclipse.jdt.internal.compiler.lookup.BlockScope; import org.eclipse.jdt.internal.compiler.lookup.MethodBinding; import org.eclipse.jdt.internal.compiler.lookup.Scope; import org.eclipse.jdt.internal.compiler.lookup.TypeBinding; -import org.eclipse.jdt.internal.compiler.parser.Parser; import org.eclipse.jdt.internal.compiler.problem.ProblemReporter; import org.eclipse.jdt.internal.core.SourceField; import org.eclipse.jdt.internal.core.dom.rewrite.NodeRewriteEvent; @@ -87,20 +85,24 @@ final class PatchFixesHider { public static final class Util { private static ClassLoader shadowLoader; + public static ClassLoader getShadowLoader() { + if (shadowLoader == null) { + try { + Class.forName("lombok.core.LombokNode"); + // If we get here, then lombok is already available. + shadowLoader = Util.class.getClassLoader(); + } catch (ClassNotFoundException e) { + // If we get here, it isn't, and we should use the shadowloader. + shadowLoader = Main.getShadowClassLoader(); + } + } + + return shadowLoader; + } + public static Class shadowLoadClass(String name) { try { - if (shadowLoader == null) { - try { - Class.forName("lombok.core.LombokNode"); - // If we get here, then lombok is already available. - shadowLoader = Util.class.getClassLoader(); - } catch (ClassNotFoundException e) { - // If we get here, it isn't, and we should use the shadowloader. - shadowLoader = Main.getShadowClassLoader(); - } - } - - return Class.forName(name, true, shadowLoader); + return Class.forName(name, true, getShadowLoader()); } catch (ClassNotFoundException e) { throw sneakyThrow(e); } @@ -114,6 +116,11 @@ final class PatchFixesHider { } } + public static Method findMethodAnyArgs(Class type, String name) { + for (Method m : type.getDeclaredMethods()) if (name.equals(m.getName())) return m; + throw sneakyThrow(new NoSuchMethodException(type.getName() + "::" + name)); + } + public static Object invokeMethod(Method method, Object... args) { try { return method.invoke(null, args); @@ -173,20 +180,26 @@ final class PatchFixesHider { } public static final class Transform { - private static final Method TRANSFORM; - private static final Method TRANSFORM_SWAPPED; + private static Method TRANSFORM; + private static Method TRANSFORM_SWAPPED; - static { + private static synchronized void init(ClassLoader prepend) { + if (TRANSFORM != null) return; + + Main.prependClassLoader(prepend); Class shadowed = Util.shadowLoadClass("lombok.eclipse.TransformEclipseAST"); - TRANSFORM = Util.findMethod(shadowed, "transform", Parser.class, CompilationUnitDeclaration.class); - TRANSFORM_SWAPPED = Util.findMethod(shadowed, "transform_swapped", CompilationUnitDeclaration.class, Parser.class); + TRANSFORM = Util.findMethodAnyArgs(shadowed, "transform"); + TRANSFORM_SWAPPED = Util.findMethodAnyArgs(shadowed, "transform_swapped"); } - public static void transform(Parser parser, CompilationUnitDeclaration ast) throws IOException { + public static void transform(Object parser, Object ast) throws IOException { + Main.prependClassLoader(parser.getClass().getClassLoader()); + init(parser.getClass().getClassLoader()); Util.invokeMethod(TRANSFORM, parser, ast); } - public static void transform_swapped(CompilationUnitDeclaration ast, Parser parser) throws IOException { + public static void transform_swapped(Object ast, Object parser) throws IOException { + init(parser.getClass().getClassLoader()); Util.invokeMethod(TRANSFORM_SWAPPED, ast, parser); } } @@ -284,15 +297,15 @@ final class PatchFixesHider { static { Class shadowed = Util.shadowLoadClass("lombok.eclipse.agent.PatchExtensionMethod"); - RESOLVE_TYPE = Util.findMethod(shadowed, "resolveType", TypeBinding.class, MessageSend.class, BlockScope.class); + RESOLVE_TYPE = Util.findMethod(shadowed, "resolveType", Object.class, MessageSend.class, BlockScope.class); ERROR_NO_METHOD_FOR = Util.findMethod(shadowed, "errorNoMethodFor", ProblemReporter.class, MessageSend.class, TypeBinding.class, TypeBinding[].class); INVALID_METHOD = Util.findMethod(shadowed, "invalidMethod", ProblemReporter.class, MessageSend.class, MethodBinding.class); INVALID_METHOD2 = Util.findMethod(shadowed, "invalidMethod", ProblemReporter.class, MessageSend.class, MethodBinding.class, Scope.class); NON_STATIC_ACCESS_TO_STATIC_METHOD = Util.findMethod(shadowed, "nonStaticAccessToStaticMethod", ProblemReporter.class, ASTNode.class, MethodBinding.class, MessageSend.class); } - public static TypeBinding resolveType(TypeBinding resolvedType, MessageSend methodCall, BlockScope scope) { - return (TypeBinding) Util.invokeMethod(RESOLVE_TYPE, resolvedType, methodCall, scope); + public static Object resolveType(Object resolvedType, MessageSend methodCall, BlockScope scope) { + return Util.invokeMethod(RESOLVE_TYPE, resolvedType, methodCall, scope); } public static void errorNoMethodFor(ProblemReporter problemReporter, MessageSend messageSend, TypeBinding recType, TypeBinding[] params) { diff --git a/src/launch/lombok/launch/Main.java b/src/launch/lombok/launch/Main.java index 08298cc2..ff539704 100644 --- a/src/launch/lombok/launch/Main.java +++ b/src/launch/lombok/launch/Main.java @@ -34,6 +34,11 @@ class Main { return classLoader; } + static synchronized void prependClassLoader(ClassLoader loader) { + getShadowClassLoader(); + classLoader.prepend(loader); + } + public static void main(String[] args) throws Throwable { ClassLoader cl = getShadowClassLoader(); Class mc = cl.loadClass("lombok.core.Main"); diff --git a/src/launch/lombok/launch/ShadowClassLoader.java b/src/launch/lombok/launch/ShadowClassLoader.java index da377ae4..e75c300e 100644 --- a/src/launch/lombok/launch/ShadowClassLoader.java +++ b/src/launch/lombok/launch/ShadowClassLoader.java @@ -102,6 +102,16 @@ class ShadowClassLoader extends ClassLoader { private final List parentExclusion = new ArrayList(); private final List highlanders = new ArrayList(); + private final List prependedLoaders = new ArrayList(); + + public void prepend(ClassLoader loader) { + if (loader == null) return; + for (ClassLoader cl : prependedLoaders) { + if (cl == loader) return; + } + prependedLoaders.add(loader); + } + /** * @param source The 'parent' classloader. * @param sclSuffix The suffix of the shadowed class files in our own jar. For example, if this is {@code lombok}, then the class files in your jar should be {@code foo/Bar.SCL.lombok} and not {@code foo/Bar.class}. @@ -529,6 +539,16 @@ class ShadowClassLoader extends ClassLoader { } String fileNameOfClass = name.replace(".", "/") + ".class"; + for (ClassLoader pre : prependedLoaders) { + try { + URL res = pre.getResource(fileNameOfClass); + if (res == null) continue; + return urlToDefineClass(name, res, resolve); + } catch (Exception e) { + continue; + } + } + URL res = getResource_(fileNameOfClass, true); if (res == null) { if (!exclusionListMatch(fileNameOfClass)) try { @@ -540,6 +560,10 @@ class ShadowClassLoader extends ClassLoader { } if (res == null) throw new ClassNotFoundException(name); + return urlToDefineClass(name, res, resolve); + } + + private Class urlToDefineClass(String name, URL res, boolean resolve) throws ClassNotFoundException { byte[] b; int p = 0; try { -- cgit