[issue #285] Compiling with Maven+Tycho should now work

This commit is contained in:
Reinier Zwitserloot committed 2020-10-30 04:48:52 +01:00
1 parent 647c36f016
commit 28fbff467b
7 files changed
+79 -35

No files matched your search

+1
View File
@@ -3,6 +3,7 @@ Lombok Changelog
### v1.18.17 "Edgy Guinea Pig"
* BUGFIX: Netbeans would not work with 1.18.16 anymore. [Issue #2612](https://github.com/rzwitserloot/lombok/issues/2612)
* (potential) BUGFIX: Using lombok with Maven Tycho now works. With assistance from [Rabea Gransberger](https://github.com/rgra). [Issue #285](https://github.com/rzwitserloot/lombok/issues/285)
### v1.18.16 (October 15th, 2020)
* BUGFIX: Version 1.18.14 could not be installed in Eclipse, it would break Eclipse.
@@ -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()
@@ -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<Extension> extensions = new ArrayList<Extension>();
TypeDeclaration decl = scope.classScope().referenceContext;
@@ -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));
@@ -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) {
+5
View File
@@ -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");
@@ -102,6 +102,16 @@ class ShadowClassLoader extends ClassLoader {
private final List<String> parentExclusion = new ArrayList<String>();
private final List<String> highlanders = new ArrayList<String>();
private final List<ClassLoader> prependedLoaders = new ArrayList<ClassLoader>();
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 {