aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRawi01 <Rawi01@users.noreply.github.com>2020-08-20 09:27:11 +0200
committerRawi01 <Rawi01@users.noreply.github.com>2020-08-20 09:29:49 +0200
commit4d2ae4ba3261ab2963c13649b3ba8f4b3881e9c3 (patch)
treeedf633e80da8b2e26974fd49d5e66572575bd0cc
parent7dfbe4323c15cbd88983380b27a250d2a381d148 (diff)
downloadlombok-4d2ae4ba3261ab2963c13649b3ba8f4b3881e9c3.tar.gz
lombok-4d2ae4ba3261ab2963c13649b3ba8f4b3881e9c3.tar.bz2
lombok-4d2ae4ba3261ab2963c13649b3ba8f4b3881e9c3.zip
[bugfix] Fix suppressBaseMethods in ecj/eclipse
-rw-r--r--src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java8
-rw-r--r--src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java24
-rwxr-xr-xsrc/eclipseAgent/lombok/launch/PatchFixesHider.java7
-rw-r--r--test/transform/resource/after-delombok/ExtensionMethodSuppress.java33
-rw-r--r--test/transform/resource/after-ecj/ExtensionMethodSuppress.java41
-rw-r--r--test/transform/resource/before/ExtensionMethodSuppress.java43
-rw-r--r--test/transform/resource/messages-ecj/ExtensionMethodSuppress.java.messages1
7 files changed, 153 insertions, 4 deletions
diff --git a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java
index c560f002..d57ab549 100644
--- a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java
+++ b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java
@@ -774,6 +774,7 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable {
final String METHOD_BINDING_SIG = "org.eclipse.jdt.internal.compiler.lookup.MethodBinding";
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";
sm.addScript(wrapReturnValue()
.target(new MethodTarget(MESSAGE_SEND_SIG, "resolveType", TYPE_BINDING_SIG, BLOCK_SCOPE_SIG))
@@ -802,6 +803,13 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable {
.replacementMethod(new Hook(PATCH_EXTENSIONMETHOD, "invalidMethod", "void", PROBLEM_REPORTER_SIG, MESSAGE_SEND_SIG, METHOD_BINDING_SIG, SCOPE_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))
+ .requestExtra(StackRequest.THIS)
+ .build());
+
if (!ecj) {
sm.addScript(wrapReturnValue()
.target(new MethodTarget(COMPLETION_PROPOSAL_COLLECTOR_SIG, "getJavaCompletionProposals", I_JAVA_COMPLETION_PROPOSAL_SIG))
diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java
index fcc76059..8638ff47 100644
--- a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java
+++ b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java
@@ -56,7 +56,6 @@ import org.eclipse.jdt.internal.compiler.ast.ThisReference;
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.lookup.Binding;
import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
-import org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope;
import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
import org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding;
import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
@@ -138,6 +137,22 @@ public class PatchExtensionMethod {
}
}
+ private static class PostponedNonStaticAccessToStaticMethodError implements PostponedError {
+ private final ProblemReporter problemReporter;
+ private ASTNode location;
+ private MethodBinding method;
+
+ PostponedNonStaticAccessToStaticMethodError(ProblemReporter problemReporter, ASTNode location, MethodBinding method) {
+ this.problemReporter = problemReporter;
+ this.location = location;
+ this.method = method;
+ }
+
+ public void fire() {
+ problemReporter.nonStaticAccessToStaticMethod(location, method);
+ }
+ }
+
private static interface PostponedError {
public void fire();
}
@@ -200,15 +215,12 @@ public class PatchExtensionMethod {
TypeBinding receiverType) {
List<MethodBinding> extensionMethods = new ArrayList<MethodBinding>();
- CompilationUnitScope cuScope = ((CompilationUnitDeclaration) typeNode.top().get()).scope;
for (MethodBinding method : extensionMethodProviderBinding.methods()) {
if (!method.isStatic()) continue;
if (!method.isPublic()) continue;
if (method.parameters == null || method.parameters.length == 0) continue;
TypeBinding firstArgType = method.parameters[0];
if (receiverType.isProvablyDistinct(firstArgType) && !receiverType.isCompatibleWith(firstArgType.erasure())) continue;
- TypeBinding[] argumentTypes = Arrays.copyOfRange(method.parameters, 1, method.parameters.length);
- if ((receiverType instanceof ReferenceBinding) && ((ReferenceBinding) receiverType).getExactMethod(method.selector, argumentTypes, cuScope) != null) continue;
extensionMethods.add(method);
}
return extensionMethods;
@@ -228,6 +240,10 @@ public class PatchExtensionMethod {
MessageSend_postponedErrors.set(messageSend, new PostponedInvalidMethodError(problemReporter, messageSend, method, scope));
}
+ public static void nonStaticAccessToStaticMethod(ProblemReporter problemReporter, ASTNode location, MethodBinding method, MessageSend messageSend) {
+ MessageSend_postponedErrors.set(messageSend, new PostponedNonStaticAccessToStaticMethodError(problemReporter, location, method));
+ }
+
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/launch/PatchFixesHider.java b/src/eclipseAgent/lombok/launch/PatchFixesHider.java
index deab0be1..64e90db5 100755
--- a/src/eclipseAgent/lombok/launch/PatchFixesHider.java
+++ b/src/eclipseAgent/lombok/launch/PatchFixesHider.java
@@ -42,6 +42,7 @@ import org.eclipse.jdt.core.dom.ArrayType;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.SimpleName;
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;
@@ -271,6 +272,7 @@ final class PatchFixesHider {
private static final Method RESOLVE_TYPE;
private static final Method ERROR_NO_METHOD_FOR;
private static final Method INVALID_METHOD, INVALID_METHOD2;
+ private static final Method NON_STATIC_ACCESS_TO_STATIC_METHOD;
static {
Class<?> shadowed = Util.shadowLoadClass("lombok.eclipse.agent.PatchExtensionMethod");
@@ -278,6 +280,7 @@ final class PatchFixesHider {
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) {
@@ -295,6 +298,10 @@ final class PatchFixesHider {
public static void invalidMethod(ProblemReporter problemReporter, MessageSend messageSend, MethodBinding method, Scope scope) {
Util.invokeMethod(INVALID_METHOD2, problemReporter, messageSend, method, scope);
}
+
+ public static void nonStaticAccessToStaticMethod(ProblemReporter problemReporter, ASTNode location, MethodBinding method, MessageSend messageSend) {
+ Util.invokeMethod(NON_STATIC_ACCESS_TO_STATIC_METHOD, problemReporter, location, method, messageSend);
+ }
}
/**
diff --git a/test/transform/resource/after-delombok/ExtensionMethodSuppress.java b/test/transform/resource/after-delombok/ExtensionMethodSuppress.java
new file mode 100644
index 00000000..d9e7cb28
--- /dev/null
+++ b/test/transform/resource/after-delombok/ExtensionMethodSuppress.java
@@ -0,0 +1,33 @@
+class ExtensionMethodSuppress {
+ public void test() {
+ Test.staticMethod();
+ Test test = new Test();
+ Extensions.instanceMethod(test);
+ Extensions.staticMethod(test);
+ }
+}
+
+class ExtensionMethodKeep {
+ public void test() {
+ Test.staticMethod();
+ Test test = new Test();
+ test.instanceMethod();
+ test.staticMethod();
+ }
+}
+
+class Test {
+ public static void staticMethod() {
+ }
+
+ public void instanceMethod() {
+ }
+}
+
+class Extensions {
+ public static void staticMethod(Test test) {
+ }
+
+ public static void instanceMethod(Test test) {
+ }
+} \ No newline at end of file
diff --git a/test/transform/resource/after-ecj/ExtensionMethodSuppress.java b/test/transform/resource/after-ecj/ExtensionMethodSuppress.java
new file mode 100644
index 00000000..83e4b57f
--- /dev/null
+++ b/test/transform/resource/after-ecj/ExtensionMethodSuppress.java
@@ -0,0 +1,41 @@
+import lombok.experimental.ExtensionMethod;
+@ExtensionMethod(Extensions.class) class ExtensionMethodSuppress {
+ ExtensionMethodSuppress() {
+ super();
+ }
+ public void test() {
+ Test.staticMethod();
+ Test test = new Test();
+ Extensions.instanceMethod(test);
+ Extensions.staticMethod(test);
+ }
+}
+@ExtensionMethod(value = Extensions.class,suppressBaseMethods = false) class ExtensionMethodKeep {
+ ExtensionMethodKeep() {
+ super();
+ }
+ public void test() {
+ Test.staticMethod();
+ Test test = new Test();
+ test.instanceMethod();
+ test.staticMethod();
+ }
+}
+class Test {
+ Test() {
+ super();
+ }
+ public static void staticMethod() {
+ }
+ public void instanceMethod() {
+ }
+}
+class Extensions {
+ Extensions() {
+ super();
+ }
+ public static void staticMethod(Test test) {
+ }
+ public static void instanceMethod(Test test) {
+ }
+} \ No newline at end of file
diff --git a/test/transform/resource/before/ExtensionMethodSuppress.java b/test/transform/resource/before/ExtensionMethodSuppress.java
new file mode 100644
index 00000000..d30652d3
--- /dev/null
+++ b/test/transform/resource/before/ExtensionMethodSuppress.java
@@ -0,0 +1,43 @@
+import lombok.experimental.ExtensionMethod;
+
+@ExtensionMethod(Extensions.class)
+class ExtensionMethodSuppress {
+ public void test() {
+ Test.staticMethod();
+
+ Test test = new Test();
+ test.instanceMethod();
+ test.staticMethod();
+ }
+}
+
+@ExtensionMethod(value = Extensions.class, suppressBaseMethods = false)
+class ExtensionMethodKeep {
+ public void test() {
+ Test.staticMethod();
+
+ Test test = new Test();
+ test.instanceMethod();
+ test.staticMethod();
+ }
+}
+
+class Test {
+ public static void staticMethod() {
+
+ }
+
+ public void instanceMethod() {
+
+ }
+}
+
+class Extensions {
+ public static void staticMethod(Test test) {
+
+ }
+
+ public static void instanceMethod(Test test) {
+
+ }
+} \ No newline at end of file
diff --git a/test/transform/resource/messages-ecj/ExtensionMethodSuppress.java.messages b/test/transform/resource/messages-ecj/ExtensionMethodSuppress.java.messages
new file mode 100644
index 00000000..dfa778a4
--- /dev/null
+++ b/test/transform/resource/messages-ecj/ExtensionMethodSuppress.java.messages
@@ -0,0 +1 @@
+21 The static method staticMethod() from the type Test should be accessed in a static way \ No newline at end of file