diff options
author | Rawi01 <Rawi01@users.noreply.github.com> | 2021-02-07 12:19:56 +0100 |
---|---|---|
committer | Rawi01 <Rawi01@users.noreply.github.com> | 2021-02-07 14:21:43 +0100 |
commit | dd1f481c333901f29efc150a07695db7ae7a2e6e (patch) | |
tree | 9580023a8214d107938d1f1851f3056ddabb04bd | |
parent | a62ffaa280acd09892755897c1c97f5d06f02be5 (diff) | |
download | lombok-dd1f481c333901f29efc150a07695db7ae7a2e6e.tar.gz lombok-dd1f481c333901f29efc150a07695db7ae7a2e6e.tar.bz2 lombok-dd1f481c333901f29efc150a07695db7ae7a2e6e.zip |
Fix ecj classloading
7 files changed, 108 insertions, 205 deletions
diff --git a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java index 893e49c5..ce26c892 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java +++ b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java @@ -772,11 +772,12 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable { final String EXPRESSION_SIG = "org.eclipse.jdt.internal.compiler.ast.Expression"; final String BLOCKSCOPE_SIG = "org.eclipse.jdt.internal.compiler.lookup.BlockScope"; final String TYPEBINDING_SIG = "org.eclipse.jdt.internal.compiler.lookup.TypeBinding"; + final String OBJECT_SIG = "java.lang.Object"; sm.addScript(ScriptBuilder.exitEarly() .target(new MethodTarget(LOCALDECLARATION_SIG, "resolve", "void", BLOCKSCOPE_SIG)) .request(StackRequest.THIS, StackRequest.PARAM1) - .decisionMethod(new Hook("lombok.launch.PatchFixesHider$Val", "handleValForLocalDeclaration", "boolean", LOCALDECLARATION_SIG, BLOCKSCOPE_SIG)) + .decisionMethod(new Hook("lombok.launch.PatchFixesHider$Val", "handleValForLocalDeclaration", "boolean", OBJECT_SIG, OBJECT_SIG)) .build()); sm.addScript(ScriptBuilder.replaceMethodCall() @@ -784,18 +785,20 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable { .methodToReplace(new Hook(EXPRESSION_SIG, "resolveType", TYPEBINDING_SIG, BLOCKSCOPE_SIG)) .requestExtra(StackRequest.THIS) .replacementMethod(new Hook("lombok.launch.PatchFixesHider$Val", "skipResolveInitializerIfAlreadyCalled2", TYPEBINDING_SIG, EXPRESSION_SIG, BLOCKSCOPE_SIG, LOCALDECLARATION_SIG)) + .transplant() .build()); sm.addScript(ScriptBuilder.replaceMethodCall() .target(new MethodTarget(FOREACHSTATEMENT_SIG, "resolve", "void", BLOCKSCOPE_SIG)) .methodToReplace(new Hook(EXPRESSION_SIG, "resolveType", TYPEBINDING_SIG, BLOCKSCOPE_SIG)) .replacementMethod(new Hook("lombok.launch.PatchFixesHider$Val", "skipResolveInitializerIfAlreadyCalled", TYPEBINDING_SIG, EXPRESSION_SIG, BLOCKSCOPE_SIG)) + .transplant() .build()); sm.addScript(ScriptBuilder.exitEarly() .target(new MethodTarget(FOREACHSTATEMENT_SIG, "resolve", "void", BLOCKSCOPE_SIG)) .request(StackRequest.THIS, StackRequest.PARAM1) - .decisionMethod(new Hook("lombok.launch.PatchFixesHider$Val", "handleValForForEach", "boolean", FOREACHSTATEMENT_SIG, BLOCKSCOPE_SIG)) + .decisionMethod(new Hook("lombok.launch.PatchFixesHider$Val", "handleValForForEach", "boolean", OBJECT_SIG, OBJECT_SIG)) .build()); } @@ -843,33 +846,33 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable { .request(StackRequest.RETURN_VALUE) .request(StackRequest.THIS) .request(StackRequest.PARAM1) - .wrapMethod(new Hook(PATCH_EXTENSIONMETHOD, "resolveType", OBJECT_SIG, OBJECT_SIG, MESSAGE_SEND_SIG, BLOCK_SCOPE_SIG)) + .wrapMethod(new Hook(PATCH_EXTENSIONMETHOD, "resolveType", OBJECT_SIG, OBJECT_SIG, OBJECT_SIG, OBJECT_SIG)) .cast() .build()); sm.addScript(replaceMethodCall() .target(new MethodTarget(MESSAGE_SEND_SIG, "resolveType", TYPE_BINDING_SIG, BLOCK_SCOPE_SIG)) .methodToReplace(new Hook(PROBLEM_REPORTER_SIG, "errorNoMethodFor", "void", MESSAGE_SEND_SIG, TYPE_BINDING_SIG, TYPE_BINDINGS_SIG)) - .replacementMethod(new Hook(PATCH_EXTENSIONMETHOD, "errorNoMethodFor", "void", PROBLEM_REPORTER_SIG, MESSAGE_SEND_SIG, TYPE_BINDING_SIG, TYPE_BINDINGS_SIG)) + .replacementMethod(new Hook(PATCH_EXTENSIONMETHOD, "errorNoMethodFor", "void", OBJECT_SIG, OBJECT_SIG, OBJECT_SIG, OBJECT_SIG)) .build()); sm.addScript(replaceMethodCall() .target(new MethodTarget(MESSAGE_SEND_SIG, "resolveType", TYPE_BINDING_SIG, BLOCK_SCOPE_SIG)) .methodToReplace(new Hook(PROBLEM_REPORTER_SIG, "invalidMethod", "void", MESSAGE_SEND_SIG, METHOD_BINDING_SIG)) - .replacementMethod(new Hook(PATCH_EXTENSIONMETHOD, "invalidMethod", "void", PROBLEM_REPORTER_SIG, MESSAGE_SEND_SIG, METHOD_BINDING_SIG)) + .replacementMethod(new Hook(PATCH_EXTENSIONMETHOD, "invalidMethod", "void", OBJECT_SIG, OBJECT_SIG, OBJECT_SIG)) .build()); // Since eclipse mars; they added a param. sm.addScript(replaceMethodCall() .target(new MethodTarget(MESSAGE_SEND_SIG, "resolveType", TYPE_BINDING_SIG, BLOCK_SCOPE_SIG)) .methodToReplace(new Hook(PROBLEM_REPORTER_SIG, "invalidMethod", "void", MESSAGE_SEND_SIG, METHOD_BINDING_SIG, SCOPE_SIG)) - .replacementMethod(new Hook(PATCH_EXTENSIONMETHOD, "invalidMethod", "void", PROBLEM_REPORTER_SIG, MESSAGE_SEND_SIG, METHOD_BINDING_SIG, SCOPE_SIG)) + .replacementMethod(new Hook(PATCH_EXTENSIONMETHOD, "invalidMethod", "void", OBJECT_SIG, OBJECT_SIG, OBJECT_SIG, OBJECT_SIG)) .build()); sm.addScript(replaceMethodCall() .target(new MethodTarget(MESSAGE_SEND_SIG, "resolveType", TYPE_BINDING_SIG, BLOCK_SCOPE_SIG)) .methodToReplace(new Hook(PROBLEM_REPORTER_SIG, "nonStaticAccessToStaticMethod", "void", AST_NODE, METHOD_BINDING_SIG)) - .replacementMethod(new Hook(PATCH_EXTENSIONMETHOD, "nonStaticAccessToStaticMethod", "void", PROBLEM_REPORTER_SIG, AST_NODE, METHOD_BINDING_SIG, MESSAGE_SEND_SIG)) + .replacementMethod(new Hook(PATCH_EXTENSIONMETHOD, "nonStaticAccessToStaticMethod", "void", OBJECT_SIG, OBJECT_SIG, OBJECT_SIG, OBJECT_SIG)) .requestExtra(StackRequest.THIS) .build()); diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java index 5f229955..1f2f67ec 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2012-2020 The Project Lombok Authors. + * Copyright (C) 2012-2021 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -246,7 +246,7 @@ public class PatchExtensionMethod { MessageSend_postponedErrors.set(messageSend, new PostponedNonStaticAccessToStaticMethodError(problemReporter, location, method)); } - public static Object resolveType(Object resolvedType, MessageSend methodCall, BlockScope scope) { + public static TypeBinding resolveType(TypeBinding resolvedType, MessageSend methodCall, BlockScope scope) { List<Extension> extensions = new ArrayList<Extension>(); TypeDeclaration decl = scope.classScope().referenceContext; diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethodPortal.java b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethodPortal.java deleted file mode 100644 index 82df71f6..00000000 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethodPortal.java +++ /dev/null @@ -1,115 +0,0 @@ -package lombok.eclipse.agent; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -import lombok.Lombok; - -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;"; - private static final String MESSAGE_SEND = "org.eclipse.jdt.internal.compiler.ast.MessageSend"; - private static final String BLOCK_SCOPE = "org.eclipse.jdt.internal.compiler.lookup.BlockScope"; - 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 Object resolveType(Object resolvedType, Object methodCall, Object scope) { - try { - 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 resolvedType; - } catch (IllegalAccessException e) { - throw Lombok.sneakyThrow(e); - } catch (InvocationTargetException e) { - throw Lombok.sneakyThrow(e); - } catch (NullPointerException e) { - if (!"false".equals(System.getProperty("lombok.debug.reflection", "false"))) { - e.initCause(Reflection.problem); - throw e; - } - //ignore, we don't have access to the correct ECJ classes, so lombok can't possibly - //do anything useful here. - return resolvedType; - } - } - - public static void errorNoMethodFor(Object problemReporter, Object messageSend, Object recType, Object params) { - try { - Reflection.errorNoMethodFor.invoke(null, problemReporter, messageSend, recType, params); - } catch (NoClassDefFoundError e) { - //ignore, we don't have access to the correct ECJ classes, so lombok can't possibly - //do anything useful here. - } catch (IllegalAccessException e) { - throw Lombok.sneakyThrow(e); - } catch (InvocationTargetException e) { - throw Lombok.sneakyThrow(e.getCause()); - } catch (NullPointerException e) { - if (!"false".equals(System.getProperty("lombok.debug.reflection", "false"))) { - e.initCause(Reflection.problem); - throw e; - } - //ignore, we don't have access to the correct ECJ classes, so lombok can't possibly - //do anything useful here. - } - } - - public static void invalidMethod(Object problemReporter, Object messageSend, Object method) { - try { - Reflection.invalidMethod.invoke(null, problemReporter, messageSend, method); - } catch (NoClassDefFoundError e) { - //ignore, we don't have access to the correct ECJ classes, so lombok can't possibly - //do anything useful here. - } catch (IllegalAccessException e) { - handleReflectionDebug(e); - throw Lombok.sneakyThrow(e); - } catch (InvocationTargetException e) { - handleReflectionDebug(e.getCause()); - throw Lombok.sneakyThrow(e.getCause()); - } catch (NullPointerException e) { - handleReflectionDebug(e); - //ignore, we don't have access to the correct ECJ classes, so lombok can't possibly - //do anything useful here. - } - } - - public static boolean isDebugReflection() { - return !"false".equals(System.getProperty("lombok.debug.reflection", "false")); - } - - public static void handleReflectionDebug(Throwable t) { - if (!isDebugReflection()) return; - - System.err.println("** LOMBOK REFLECTION exception: " + t.getClass() + ": " + (t.getMessage() == null ? "(no message)" : t.getMessage())); - t.printStackTrace(System.err); - if (Reflection.problem != null) { - System.err.println("*** ADDITIONALLY, exception occurred setting up reflection: "); - Reflection.problem.printStackTrace(System.err); - } - } - - private static final class Reflection { - public static final Method resolveType, errorNoMethodFor, invalidMethod; - public static final Throwable problem; - - static { - Method m = null, n = null, o = null; - Throwable problem_ = null; - try { - 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)); - } catch (Throwable t) { - // That's problematic, but as long as no local classes are used we don't actually need it. - // Better fail on local classes than crash altogether. - problem_ = t; - } - resolveType = m; - errorNoMethodFor = n; - invalidMethod = o; - problem = problem_; - } - } -} diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchVal.java b/src/eclipseAgent/lombok/eclipse/agent/PatchVal.java index f22e78a8..9663f364 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchVal.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchVal.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2019 The Project Lombok Authors. + * Copyright (C) 2010-2021 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -68,32 +68,7 @@ public class PatchVal { // This is half of the work for 'val' support - the other half is in PatchValEclipse. This half is enough for ecj. // Creates a copy of the 'initialization' field on a LocalDeclaration if the type of the LocalDeclaration is 'val', because the completion parser will null this out, // which in turn stops us from inferring the intended type for 'val x = 5;'. We look at the copy. - // Also patches local declaration to not call .resolveType() on the initializer expression if we've already done so (calling it twice causes weird errors), - // and patches .resolve() on LocalDeclaration itself to just-in-time replace the 'val' vartype with the right one. - - public static TypeBinding skipResolveInitializerIfAlreadyCalled(Expression expr, BlockScope scope) { - if (expr.resolvedType != null) return expr.resolvedType; - try { - return expr.resolveType(scope); - } catch (NullPointerException e) { - return null; - } catch (ArrayIndexOutOfBoundsException e) { - // This will occur internally due to for example 'val x = mth("X");', where mth takes 2 arguments. - return null; - } - } - - public static TypeBinding skipResolveInitializerIfAlreadyCalled2(Expression expr, BlockScope scope, LocalDeclaration decl) { - if (decl != null && LocalDeclaration.class.equals(decl.getClass()) && expr.resolvedType != null) return expr.resolvedType; - try { - return expr.resolveType(scope); - } catch (NullPointerException e) { - return null; - } catch (ArrayIndexOutOfBoundsException e) { - // This will occur internally due to for example 'val x = mth("X");', where mth takes 2 arguments. - return null; - } - } + // Also patches .resolve() on LocalDeclaration itself to just-in-time replace the 'val' vartype with the right one. public static boolean matches(String key, char[] array) { if (array == null || key.length() != array.length) return false; diff --git a/src/eclipseAgent/lombok/launch/PatchFixesHider.java b/src/eclipseAgent/lombok/launch/PatchFixesHider.java index afdfdb95..02df3f5f 100755 --- a/src/eclipseAgent/lombok/launch/PatchFixesHider.java +++ b/src/eclipseAgent/lombok/launch/PatchFixesHider.java @@ -43,20 +43,14 @@ import org.eclipse.jdt.core.dom.MethodDeclaration; import org.eclipse.jdt.core.dom.SimpleName; import org.eclipse.jdt.core.dom.Type; 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.Expression; import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; -import org.eclipse.jdt.internal.compiler.ast.ForeachStatement; import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration; -import org.eclipse.jdt.internal.compiler.ast.MessageSend; import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; 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.problem.ProblemReporter; import org.eclipse.jdt.internal.core.SourceField; import org.eclipse.jdt.internal.core.dom.rewrite.NodeRewriteEvent; import org.eclipse.jdt.internal.core.dom.rewrite.RewriteEvent; @@ -116,6 +110,15 @@ final class PatchFixesHider { } } + public static Method findMethod(Class<?> type, String name, String... parameterTypes) { + for (Method m : type.getDeclaredMethods()) { + if (name.equals(m.getName()) && sameTypes(m.getParameterTypes(), parameterTypes)) { + return m; + } + } + throw sneakyThrow(new NoSuchMethodException(type.getName() + "::" + name)); + } + 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)); @@ -141,6 +144,14 @@ final class PatchFixesHider { private static <T extends Throwable> void sneakyThrow0(Throwable t) throws T { throw (T)t; } + + private static boolean sameTypes(Class<?>[] types, String[] typeNames) { + if (types.length != typeNames.length) return false; + for (int i = 0; i < types.length; i++) { + if (!types[i].getName().equals(typeNames[i])) return false; + } + return true; + } } /** Contains patch fixes that are dependent on lombok internals. */ @@ -193,7 +204,6 @@ final class PatchFixesHider { } public static void transform(Object parser, Object ast) throws IOException { - Main.prependClassLoader(parser.getClass().getClassLoader()); init(parser.getClass().getClassLoader()); Util.invokeMethod(TRANSFORM, parser, ast); } @@ -258,38 +268,67 @@ final class PatchFixesHider { /** Contains patch code to support {@code val} (eclipse and ecj) */ public static final class Val { - private static final Method SKIP_RESOLVE_INITIALIZER_IF_ALREADY_CALLED; - private static final Method SKIP_RESOLVE_INITIALIZER_IF_ALREADY_CALLED2; + private static final String BLOCK_SCOPE_SIG = "org.eclipse.jdt.internal.compiler.lookup.BlockScope"; + private static final String LOCAL_DECLARATION_SIG = "org.eclipse.jdt.internal.compiler.ast.LocalDeclaration"; + private static final String FOREACH_STATEMENT_SIG = "org.eclipse.jdt.internal.compiler.ast.ForeachStatement"; + private static final Method HANDLE_VAL_FOR_LOCAL_DECLARATION; private static final Method HANDLE_VAL_FOR_FOR_EACH; static { Class<?> shadowed = Util.shadowLoadClass("lombok.eclipse.agent.PatchVal"); - SKIP_RESOLVE_INITIALIZER_IF_ALREADY_CALLED = Util.findMethod(shadowed, "skipResolveInitializerIfAlreadyCalled", Expression.class, BlockScope.class); - SKIP_RESOLVE_INITIALIZER_IF_ALREADY_CALLED2 = Util.findMethod(shadowed, "skipResolveInitializerIfAlreadyCalled2", Expression.class, BlockScope.class, LocalDeclaration.class); - HANDLE_VAL_FOR_LOCAL_DECLARATION = Util.findMethod(shadowed, "handleValForLocalDeclaration", LocalDeclaration.class, BlockScope.class); - HANDLE_VAL_FOR_FOR_EACH = Util.findMethod(shadowed, "handleValForForEach", ForeachStatement.class, BlockScope.class); + HANDLE_VAL_FOR_LOCAL_DECLARATION = Util.findMethod(shadowed, "handleValForLocalDeclaration", LOCAL_DECLARATION_SIG, BLOCK_SCOPE_SIG); + HANDLE_VAL_FOR_FOR_EACH = Util.findMethod(shadowed, "handleValForForEach", FOREACH_STATEMENT_SIG, BLOCK_SCOPE_SIG); } - public static TypeBinding skipResolveInitializerIfAlreadyCalled(Expression expr, BlockScope scope) { - return (TypeBinding) Util.invokeMethod(SKIP_RESOLVE_INITIALIZER_IF_ALREADY_CALLED, expr, scope); + public static boolean handleValForLocalDeclaration(Object local, Object scope) { + return (Boolean) Util.invokeMethod(HANDLE_VAL_FOR_LOCAL_DECLARATION, local, scope); } - public static TypeBinding skipResolveInitializerIfAlreadyCalled2(Expression expr, BlockScope scope, LocalDeclaration decl) { - return (TypeBinding) Util.invokeMethod(SKIP_RESOLVE_INITIALIZER_IF_ALREADY_CALLED2, expr, scope, decl); + public static boolean handleValForForEach(Object forEach, Object scope) { + return (Boolean) Util.invokeMethod(HANDLE_VAL_FOR_FOR_EACH, forEach, scope); } - public static boolean handleValForLocalDeclaration(LocalDeclaration local, BlockScope scope) { - return (Boolean) Util.invokeMethod(HANDLE_VAL_FOR_LOCAL_DECLARATION, local, scope); + /** + * Patches local declaration to not call .resolveType() on the initializer expression if we've already done so (calling it twice causes weird errors) + * This and the next method must be transplanted so that the return type is loaded in the correct class loader + */ + public static TypeBinding skipResolveInitializerIfAlreadyCalled(Expression expr, BlockScope scope) { + if (expr.resolvedType != null) return expr.resolvedType; + try { + return expr.resolveType(scope); + } catch (NullPointerException e) { + return null; + } catch (ArrayIndexOutOfBoundsException e) { + // This will occur internally due to for example 'val x = mth("X");', where mth takes 2 arguments. + return null; + } } - public static boolean handleValForForEach(ForeachStatement forEach, BlockScope scope) { - return (Boolean) Util.invokeMethod(HANDLE_VAL_FOR_FOR_EACH, forEach, scope); + public static TypeBinding skipResolveInitializerIfAlreadyCalled2(Expression expr, BlockScope scope, LocalDeclaration decl) { + if (decl != null && LocalDeclaration.class.equals(decl.getClass()) && expr.resolvedType != null) return expr.resolvedType; + try { + return expr.resolveType(scope); + } catch (NullPointerException e) { + return null; + } catch (ArrayIndexOutOfBoundsException e) { + // This will occur internally due to for example 'val x = mth("X");', where mth takes 2 arguments. + return null; + } } } /** Contains patch code to support {@code @ExtensionMethod} */ public static final class ExtensionMethod { + private static final String MESSAGE_SEND_SIG = "org.eclipse.jdt.internal.compiler.ast.MessageSend"; + private static final String TYPE_BINDING_SIG = "org.eclipse.jdt.internal.compiler.lookup.TypeBinding"; + private static final String SCOPE_SIG = "org.eclipse.jdt.internal.compiler.lookup.Scope"; + private static final String BLOCK_SCOPE_SIG = "org.eclipse.jdt.internal.compiler.lookup.BlockScope"; + private static final String TYPE_BINDINGS_SIG = "[Lorg.eclipse.jdt.internal.compiler.lookup.TypeBinding;"; + private static final String PROBLEM_REPORTER_SIG = "org.eclipse.jdt.internal.compiler.problem.ProblemReporter"; + private static final String METHOD_BINDING_SIG = "org.eclipse.jdt.internal.compiler.lookup.MethodBinding"; + private static final String AST_NODE_SIG = "org.eclipse.jdt.internal.compiler.ast.ASTNode"; + private static final Method RESOLVE_TYPE; private static final Method ERROR_NO_METHOD_FOR; private static final Method INVALID_METHOD, INVALID_METHOD2; @@ -297,30 +336,30 @@ final class PatchFixesHider { static { Class<?> shadowed = Util.shadowLoadClass("lombok.eclipse.agent.PatchExtensionMethod"); - 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); + RESOLVE_TYPE = Util.findMethod(shadowed, "resolveType", TYPE_BINDING_SIG, MESSAGE_SEND_SIG, BLOCK_SCOPE_SIG); + ERROR_NO_METHOD_FOR = Util.findMethod(shadowed, "errorNoMethodFor", PROBLEM_REPORTER_SIG, MESSAGE_SEND_SIG, TYPE_BINDING_SIG, TYPE_BINDINGS_SIG); + INVALID_METHOD = Util.findMethod(shadowed, "invalidMethod", PROBLEM_REPORTER_SIG, MESSAGE_SEND_SIG, METHOD_BINDING_SIG); + INVALID_METHOD2 = Util.findMethod(shadowed, "invalidMethod", PROBLEM_REPORTER_SIG, MESSAGE_SEND_SIG, METHOD_BINDING_SIG, SCOPE_SIG); + NON_STATIC_ACCESS_TO_STATIC_METHOD = Util.findMethod(shadowed, "nonStaticAccessToStaticMethod", PROBLEM_REPORTER_SIG, AST_NODE_SIG, METHOD_BINDING_SIG, MESSAGE_SEND_SIG); } - public static Object resolveType(Object resolvedType, MessageSend methodCall, BlockScope scope) { + public static Object resolveType(Object resolvedType, Object methodCall, Object scope) { return Util.invokeMethod(RESOLVE_TYPE, resolvedType, methodCall, scope); } - public static void errorNoMethodFor(ProblemReporter problemReporter, MessageSend messageSend, TypeBinding recType, TypeBinding[] params) { + public static void errorNoMethodFor(Object problemReporter, Object messageSend, Object recType, Object params) { Util.invokeMethod(ERROR_NO_METHOD_FOR, problemReporter, messageSend, recType, params); } - public static void invalidMethod(ProblemReporter problemReporter, MessageSend messageSend, MethodBinding method) { + public static void invalidMethod(Object problemReporter, Object messageSend, Object method) { Util.invokeMethod(INVALID_METHOD, problemReporter, messageSend, method); } - public static void invalidMethod(ProblemReporter problemReporter, MessageSend messageSend, MethodBinding method, Scope scope) { + public static void invalidMethod(Object problemReporter, Object messageSend, Object method, Object scope) { Util.invokeMethod(INVALID_METHOD2, problemReporter, messageSend, method, scope); } - public static void nonStaticAccessToStaticMethod(ProblemReporter problemReporter, ASTNode location, MethodBinding method, MessageSend messageSend) { + public static void nonStaticAccessToStaticMethod(Object problemReporter, Object location, Object method, Object messageSend) { Util.invokeMethod(NON_STATIC_ACCESS_TO_STATIC_METHOD, problemReporter, location, method, messageSend); } } diff --git a/src/launch/lombok/launch/Main.java b/src/launch/lombok/launch/Main.java index ff539704..af65bdf8 100644 --- a/src/launch/lombok/launch/Main.java +++ b/src/launch/lombok/launch/Main.java @@ -36,7 +36,7 @@ class Main { static synchronized void prependClassLoader(ClassLoader loader) { getShadowClassLoader(); - classLoader.prepend(loader); + classLoader.prependParent(loader); } public static void main(String[] args) throws Throwable { diff --git a/src/launch/lombok/launch/ShadowClassLoader.java b/src/launch/lombok/launch/ShadowClassLoader.java index e75c300e..929681ce 100644 --- a/src/launch/lombok/launch/ShadowClassLoader.java +++ b/src/launch/lombok/launch/ShadowClassLoader.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2014-2018 The Project Lombok Authors. + * Copyright (C) 2014-2021 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -38,6 +38,7 @@ import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; +import java.util.IdentityHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -102,14 +103,12 @@ 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>(); + private final Set<ClassLoader> prependedParentLoaders = Collections.newSetFromMap(new IdentityHashMap<ClassLoader, Boolean>()); - public void prepend(ClassLoader loader) { + public void prependParent(ClassLoader loader) { if (loader == null) return; - for (ClassLoader cl : prependedLoaders) { - if (cl == loader) return; - } - prependedLoaders.add(loader); + if (loader == getParent()) return; + prependedParentLoaders.add(loader); } /** @@ -539,23 +538,25 @@ 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 { - return super.loadClass(name, resolve); - } catch (ClassNotFoundException cnfe) { - res = getResource_("secondaryLoading.SCL." + sclSuffix + "/" + name.replace(".", "/") + ".SCL." + sclSuffix, true); - if (res == null) throw cnfe; + if (!exclusionListMatch(fileNameOfClass)) { + try { + // First search in the prepended classloaders, the class might be their already + for (ClassLoader pre : prependedParentLoaders) { + try { + Class<?> loadClass = pre.loadClass(name); + if (loadClass != null) return loadClass; + } catch (Throwable e) { + continue; + } + } + + return super.loadClass(name, resolve); + } catch (ClassNotFoundException cnfe) { + res = getResource_("secondaryLoading.SCL." + sclSuffix + "/" + name.replace(".", "/") + ".SCL." + sclSuffix, true); + if (res == null) throw cnfe; + } } } if (res == null) throw new ClassNotFoundException(name); |