From c95b473e7fc508faed72f990c85fdff65b6d090a Mon Sep 17 00:00:00 2001 From: Rawi01 Date: Sat, 4 Apr 2020 16:41:59 +0200 Subject: Improve ExtensionMethod support in Eclipse Add basic support for lambdas, add autoboxing support for varargs, code completion now works if the only matching methods are extension methods, code completion no longer shows all extension methods --- .../agent/ExtensionMethodCompletionProposal.java | 9 ++++ .../lombok/eclipse/agent/PatchExtensionMethod.java | 55 +++++++++++++++++----- .../PatchExtensionMethodCompletionProposal.java | 39 ++++++++------- 3 files changed, 73 insertions(+), 30 deletions(-) (limited to 'src/eclipseAgent') diff --git a/src/eclipseAgent/lombok/eclipse/agent/ExtensionMethodCompletionProposal.java b/src/eclipseAgent/lombok/eclipse/agent/ExtensionMethodCompletionProposal.java index 46ce63f9..e692179b 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/ExtensionMethodCompletionProposal.java +++ b/src/eclipseAgent/lombok/eclipse/agent/ExtensionMethodCompletionProposal.java @@ -22,6 +22,7 @@ package lombok.eclipse.agent; import static org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants.AccStatic; +import static org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants.AccVarargs; import java.util.Arrays; @@ -45,6 +46,10 @@ public class ExtensionMethodCompletionProposal extends InternalCompletionProposa MethodBinding original = method.original(); TypeBinding[] parameters = Arrays.copyOf(method.parameters, method.parameters.length); method.parameters = Arrays.copyOfRange(method.parameters, 1, method.parameters.length); + // Add proposal parameter names, sometimes its still empty... + if (method.parameterNames != null && method.parameterNames.length > 0) { + setParameterNames(Arrays.copyOfRange(method.parameterNames, 1, method.parameterNames.length)); + } TypeBinding[] originalParameters = null; if (original != method) { originalParameters = Arrays.copyOf(method.original().parameters, method.original().parameters.length); @@ -76,6 +81,10 @@ public class ExtensionMethodCompletionProposal extends InternalCompletionProposa setName(method.selector); setCompletion(completion); setFlags(method.modifiers & (~AccStatic)); + // Remove varargs flag if it is the only parameter + if (method.isVarargs() && length == 0) { + setFlags(getFlags() & (~AccVarargs)); + } int index = node.sourceEnd + 1; if (node instanceof CompletionOnQualifiedNameReference) { index -= ((CompletionOnQualifiedNameReference) node).completionIdentifier.length; diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java index fcc76059..964efada 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java @@ -47,6 +47,7 @@ import org.eclipse.jdt.internal.compiler.ast.Annotation; import org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess; import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; import org.eclipse.jdt.internal.compiler.ast.Expression; +import org.eclipse.jdt.internal.compiler.ast.FunctionalExpression; import org.eclipse.jdt.internal.compiler.ast.MessageSend; import org.eclipse.jdt.internal.compiler.ast.NameReference; import org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference; @@ -58,6 +59,7 @@ 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.ParameterizedTypeBinding; import org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding; import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding; import org.eclipse.jdt.internal.compiler.lookup.Scope; @@ -263,12 +265,30 @@ public class PatchExtensionMethod { arguments.add(methodCall.receiver); if (methodCall.arguments != null) arguments.addAll(Arrays.asList(methodCall.arguments)); List argumentTypes = new ArrayList(); - for (Expression argument : arguments) { - if (argument.resolvedType != null) argumentTypes.add(argument.resolvedType); - // TODO: Instead of just skipping nulls entirely, there is probably a 'unresolved type' placeholder. THAT is what we ought to be adding here! + + for (int i = 0; i < arguments.size(); i++) { + Expression argument = arguments.get(i); + TypeBinding argumentType = argument.resolvedType; + if (argumentType == null) { + // Copy unresolved lamdba types + argumentType = methodCall.argumentTypes.length >= i ? methodCall.argumentTypes[i - 1] : null; + } + if (argumentType != null) { + argumentTypes.add(argumentType); + } } Expression[] originalArgs = methodCall.arguments; methodCall.arguments = arguments.toArray(new Expression[0]); + + // Copy generic information. This one covers a few simple cases, more complex cases are still broken + int typeVariables = extensionMethod.typeVariables.length; + if (typeVariables > 0 && methodCall.receiver.resolvedType instanceof ParameterizedTypeBinding) { + ParameterizedTypeBinding parameterizedTypeBinding = (ParameterizedTypeBinding) methodCall.receiver.resolvedType; + if (parameterizedTypeBinding.arguments != null && parameterizedTypeBinding.arguments.length == typeVariables) { + methodCall.genericTypeArguments = parameterizedTypeBinding.arguments; + } + } + MethodBinding fixedBinding = scope.getMethod(extensionMethod.declaringClass, methodCall.selector, argumentTypes.toArray(new TypeBinding[0]), methodCall); if (fixedBinding instanceof ProblemMethodBinding) { methodCall.arguments = originalArgs; @@ -276,18 +296,28 @@ public class PatchExtensionMethod { PostponedInvalidMethodError.invoke(scope.problemReporter(), methodCall, fixedBinding, scope); } } else { + // If the extension method uses varargs, the last fixed binding parameter is an array but + // the method arguments are not. Even thought we already know that the method is fine we still + // have to compare each parameter with the type of the array to support autoboxing/unboxing. + boolean isVararg = false; for (int i = 0, iend = arguments.size(); i < iend; i++) { Expression arg = arguments.get(i); - if (fixedBinding.parameters[i].isArrayType() != arg.resolvedType.isArrayType()) break; - if (arg instanceof MessageSend) { - ((MessageSend) arg).valueCast = arg.resolvedType; + TypeBinding[] parameters = fixedBinding.parameters; + TypeBinding param = isVararg ? parameters[parameters.length - 1].leafComponentType() : parameters[i]; + // Resolve types for lambdas + if (arg instanceof FunctionalExpression) { + arg.setExpectedType(param); + arg.resolveType(scope); } - if (!fixedBinding.parameters[i].isBaseType() && arg.resolvedType.isBaseType()) { - int id = arg.resolvedType.id; - arg.implicitConversion = TypeIds.BOXING | (id + (id << 4)); // magic see TypeIds - } else if (fixedBinding.parameters[i].isBaseType() && !arg.resolvedType.isBaseType()) { - int id = fixedBinding.parameters[i].id; - arg.implicitConversion = TypeIds.UNBOXING | (id + (id << 4)); // magic see TypeIds + if (arg.resolvedType != null) { + isVararg |= param.isArrayType() != arg.resolvedType.isArrayType(); + if (!param.isBaseType() && arg.resolvedType.isBaseType()) { + int id = arg.resolvedType.id; + arg.implicitConversion = TypeIds.BOXING | (id + (id << 4)); // magic see TypeIds + } else if (param.isBaseType() && !arg.resolvedType.isBaseType()) { + int id = parameters[i].id; + arg.implicitConversion = TypeIds.UNBOXING | (id + (id << 4)); // magic see TypeIds + } } } @@ -295,6 +325,7 @@ public class PatchExtensionMethod { methodCall.actualReceiverType = extensionMethod.declaringClass; methodCall.binding = fixedBinding; methodCall.resolvedType = methodCall.binding.returnType; + methodCall.statementEnd = methodCall.sourceEnd; if (Reflection.argumentTypes != null) { try { Reflection.argumentTypes.set(methodCall, argumentTypes.toArray(new TypeBinding[0])); diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethodCompletionProposal.java b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethodCompletionProposal.java index 085c903f..08a42d1c 100755 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethodCompletionProposal.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethodCompletionProposal.java @@ -65,13 +65,14 @@ public class PatchExtensionMethodCompletionProposal { CompletionProposalCollector completionProposalCollector) { List proposals = new ArrayList(Arrays.asList(javaCompletionProposals)); - if (canExtendCodeAssist(proposals)) { - IJavaCompletionProposal firstProposal = proposals.get(0); - int replacementOffset = getReplacementOffset(firstProposal); + if (canExtendCodeAssist()) { for (Extension extension : getExtensionMethods(completionProposalCollector)) { for (MethodBinding method : extension.extensionMethods) { - ExtensionMethodCompletionProposal newProposal = new ExtensionMethodCompletionProposal(replacementOffset); - copyNameLookupAndCompletionEngine(completionProposalCollector, firstProposal, newProposal); + if (!isMatchingProposal(method, completionProposalCollector)) { + continue; + } + ExtensionMethodCompletionProposal newProposal = new ExtensionMethodCompletionProposal(0); + copyNameLookupAndCompletionEngine(completionProposalCollector, newProposal); ASTNode node = getAssistNode(completionProposalCollector); newProposal.setMethodBinding(method, node); createAndAddJavaCompletionProposal(completionProposalCollector, newProposal, proposals); @@ -81,7 +82,6 @@ public class PatchExtensionMethodCompletionProposal { return proposals.toArray(new IJavaCompletionProposal[0]); } - private static List getExtensionMethods(CompletionProposalCollector completionProposalCollector) { List extensions = new ArrayList(); ClassScope classScope = getClassScope(completionProposalCollector); @@ -96,6 +96,17 @@ public class PatchExtensionMethodCompletionProposal { return extensions; } + private static boolean isMatchingProposal(MethodBinding method, CompletionProposalCollector completionProposalCollector) { + try { + InternalCompletionContext context = (InternalCompletionContext) Reflection.contextField.get(completionProposalCollector); + String searchToken = new String(context.getToken()); + String extensionMethodName = new String(method.selector); + return extensionMethodName.contains(searchToken); + } catch (IllegalAccessException e) { + return true; + } + } + static TypeBinding getFirstParameterType(TypeDeclaration decl, CompletionProposalCollector completionProposalCollector) { TypeBinding firstParameterType = null; ASTNode node = getAssistNode(completionProposalCollector); @@ -149,8 +160,7 @@ public class PatchExtensionMethodCompletionProposal { return scope; } - private static void copyNameLookupAndCompletionEngine(CompletionProposalCollector completionProposalCollector, IJavaCompletionProposal proposal, - InternalCompletionProposal newProposal) { + private static void copyNameLookupAndCompletionEngine(CompletionProposalCollector completionProposalCollector, InternalCompletionProposal newProposal) { try { InternalCompletionContext context = (InternalCompletionContext) Reflection.contextField.get(completionProposalCollector); @@ -173,17 +183,10 @@ public class PatchExtensionMethodCompletionProposal { } } - private static boolean canExtendCodeAssist(List proposals) { - return !proposals.isEmpty() && Reflection.isComplete(); - } - - private static int getReplacementOffset(Object proposal) { - try { - return Reflection.replacementOffsetField.getInt(proposal); - } catch (Exception ignore) { - return 0; - } + private static boolean canExtendCodeAssist() { + return Reflection.isComplete(); } + static class Reflection { public static final Field replacementOffsetField; -- cgit From 7242b702700d0226b161eee33d4a630ccae7044d Mon Sep 17 00:00:00 2001 From: Rawi01 Date: Sat, 4 Apr 2020 22:51:43 +0200 Subject: Fix missing extension method parameters names during code completion --- .../agent/ExtensionMethodCompletionProposal.java | 75 ++++++++++++++++++++-- 1 file changed, 71 insertions(+), 4 deletions(-) (limited to 'src/eclipseAgent') diff --git a/src/eclipseAgent/lombok/eclipse/agent/ExtensionMethodCompletionProposal.java b/src/eclipseAgent/lombok/eclipse/agent/ExtensionMethodCompletionProposal.java index e692179b..833a8226 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/ExtensionMethodCompletionProposal.java +++ b/src/eclipseAgent/lombok/eclipse/agent/ExtensionMethodCompletionProposal.java @@ -26,7 +26,11 @@ import static org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants.AccV import java.util.Arrays; +import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jdt.core.CompletionProposal; +import org.eclipse.jdt.core.IMethod; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.core.Signature; import org.eclipse.jdt.core.compiler.CharOperation; import org.eclipse.jdt.internal.codeassist.CompletionEngine; import org.eclipse.jdt.internal.codeassist.InternalCompletionProposal; @@ -36,20 +40,29 @@ import org.eclipse.jdt.internal.codeassist.complete.CompletionOnSingleNameRefere import org.eclipse.jdt.internal.compiler.ast.ASTNode; import org.eclipse.jdt.internal.compiler.lookup.MethodBinding; import org.eclipse.jdt.internal.compiler.lookup.TypeBinding; +import org.eclipse.jdt.internal.core.NameLookup; public class ExtensionMethodCompletionProposal extends InternalCompletionProposal { + private char[] fullSignature; + private char[][] parameterNames; + public ExtensionMethodCompletionProposal(final int replacementOffset) { super(CompletionProposal.METHOD_REF, replacementOffset - 1); } public void setMethodBinding(final MethodBinding method, final ASTNode node) { - MethodBinding original = method.original(); - TypeBinding[] parameters = Arrays.copyOf(method.parameters, method.parameters.length); - method.parameters = Arrays.copyOfRange(method.parameters, 1, method.parameters.length); - // Add proposal parameter names, sometimes its still empty... + // Add proposal parameter names, sometimes its empty... if (method.parameterNames != null && method.parameterNames.length > 0) { setParameterNames(Arrays.copyOfRange(method.parameterNames, 1, method.parameterNames.length)); + } else { + // Copy signature for parameter name resolution, this is more reliable but slower + fullSignature = CompletionEngine.getSignature(method); } + + MethodBinding original = method.original(); + TypeBinding[] parameters = Arrays.copyOf(method.parameters, method.parameters.length); + method.parameters = Arrays.copyOfRange(method.parameters, 1, method.parameters.length); + TypeBinding[] originalParameters = null; if (original != method) { originalParameters = Arrays.copyOf(method.original().parameters, method.original().parameters.length); @@ -105,4 +118,58 @@ public class ExtensionMethodCompletionProposal extends InternalCompletionProposa method.original().parameters = originalParameters; } } + + @Override + public char[][] findParameterNames(IProgressMonitor monitor) { + if (parameterNames != null) { + return parameterNames; + } + + NameLookup.Answer answer = this.nameLookup.findType( + new String(this.declarationTypeName), + new String(this.declarationPackageName), + false, + NameLookup.ACCEPT_CLASSES & NameLookup.ACCEPT_INTERFACES, + true/* consider secondary types */, + false/* do NOT wait for indexes */, + false/*don't check restrictions*/, + null); + + if (answer != null && answer.type != null) { + char[][] parameterTypes = Signature.getParameterTypes(fullSignature); + + String[] args = new String[parameterTypes.length]; + for (int i = 0; i < parameterTypes.length; i++) { + args[i] = new String(parameterTypes[i]); + } + IMethod method = answer.type.getMethod(new String(this.getName()), args); + IMethod[] methods = answer.type.findMethods(method); + if (methods != null && methods.length > 0) { + method = methods[0]; + } + if (method != null) { + try { + String[] parameterNames = method.getParameterNames(); + char[][] parameterNamesAsChar = new char[parameterNames.length - 1][]; + for (int i = 0; i < parameterNamesAsChar.length; i++) { + parameterNamesAsChar[i] = parameterNames[i + 1].toCharArray(); + } + setParameterNames(parameterNamesAsChar); + } catch (JavaModelException e) { + // Nope + } + } + } + // Seems like we failed, fallback + if (parameterNames == null) { + parameterNames = super.findParameterNames(monitor); + } + return parameterNames; + } + + @Override + public void setParameterNames(char[][] parameterNames) { + this.parameterNames = parameterNames; + super.setParameterNames(parameterNames); + } } -- cgit From 39b733ab805f541a0834fb9cc501dc8b90567aa9 Mon Sep 17 00:00:00 2001 From: Rawi01 Date: Sat, 4 Apr 2020 22:58:52 +0200 Subject: Stop trying to resolve methods with broken arguments --- src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/eclipseAgent') diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java index 964efada..c4b0153c 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java @@ -252,6 +252,8 @@ public class PatchExtensionMethod { Binding binding = ((NameReference)methodCall.receiver).binding; if (binding instanceof TypeBinding) skip = true; } + // It's impossible to resolve the right method without types + if (methodCall.argumentsHaveErrors) skip = true; if (!skip) for (Extension extension : extensions) { if (!extension.suppressBaseMethods && !(methodCall.binding instanceof ProblemMethodBinding)) continue; -- cgit From 3d90a51163354930eeea0e26c2b0a567af8e96be Mon Sep 17 00:00:00 2001 From: Rawi01 Date: Thu, 23 Apr 2020 09:59:36 +0200 Subject: Added tests for ExtensionMethod --- .../lombok/eclipse/agent/PatchExtensionMethod.java | 11 ++++++-- .../after-delombok/ExtensionMethodAutoboxing.java | 20 +++++++++++++ .../after-delombok/ExtensionMethodFunctional.java | 28 ++++++++++++++++++ .../after-delombok/ExtensionMethodVarargs.java | 19 +++++++++++++ .../after-ecj/ExtensionMethodAutoboxing.java | 25 ++++++++++++++++ .../after-ecj/ExtensionMethodFunctional.java | 31 ++++++++++++++++++++ .../resource/after-ecj/ExtensionMethodVarargs.java | 24 ++++++++++++++++ .../resource/before/ExtensionMethodAutoboxing.java | 23 +++++++++++++++ .../resource/before/ExtensionMethodFunctional.java | 33 ++++++++++++++++++++++ .../resource/before/ExtensionMethodVarargs.java | 23 +++++++++++++++ .../ExtensionMethodVarargs.java.messages | 1 + .../ExtensionMethodVarargs.java.messages | 1 + 12 files changed, 236 insertions(+), 3 deletions(-) create mode 100644 test/transform/resource/after-delombok/ExtensionMethodAutoboxing.java create mode 100644 test/transform/resource/after-delombok/ExtensionMethodFunctional.java create mode 100644 test/transform/resource/after-delombok/ExtensionMethodVarargs.java create mode 100644 test/transform/resource/after-ecj/ExtensionMethodAutoboxing.java create mode 100644 test/transform/resource/after-ecj/ExtensionMethodFunctional.java create mode 100644 test/transform/resource/after-ecj/ExtensionMethodVarargs.java create mode 100644 test/transform/resource/before/ExtensionMethodAutoboxing.java create mode 100644 test/transform/resource/before/ExtensionMethodFunctional.java create mode 100644 test/transform/resource/before/ExtensionMethodVarargs.java create mode 100644 test/transform/resource/messages-delombok/ExtensionMethodVarargs.java.messages create mode 100644 test/transform/resource/messages-idempotent/ExtensionMethodVarargs.java.messages (limited to 'src/eclipseAgent') diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java index c4b0153c..1a1f2274 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java @@ -301,18 +301,23 @@ public class PatchExtensionMethod { // If the extension method uses varargs, the last fixed binding parameter is an array but // the method arguments are not. Even thought we already know that the method is fine we still // have to compare each parameter with the type of the array to support autoboxing/unboxing. - boolean isVararg = false; + boolean isVarargs = fixedBinding.isVarargs(); for (int i = 0, iend = arguments.size(); i < iend; i++) { Expression arg = arguments.get(i); TypeBinding[] parameters = fixedBinding.parameters; - TypeBinding param = isVararg ? parameters[parameters.length - 1].leafComponentType() : parameters[i]; + TypeBinding param; + if (isVarargs && i >= parameters.length - 1) { + // Extract the array element type for all vararg arguments + param = parameters[parameters.length - 1].leafComponentType(); + } else { + param = parameters[i]; + } // Resolve types for lambdas if (arg instanceof FunctionalExpression) { arg.setExpectedType(param); arg.resolveType(scope); } if (arg.resolvedType != null) { - isVararg |= param.isArrayType() != arg.resolvedType.isArrayType(); if (!param.isBaseType() && arg.resolvedType.isBaseType()) { int id = arg.resolvedType.id; arg.implicitConversion = TypeIds.BOXING | (id + (id << 4)); // magic see TypeIds diff --git a/test/transform/resource/after-delombok/ExtensionMethodAutoboxing.java b/test/transform/resource/after-delombok/ExtensionMethodAutoboxing.java new file mode 100644 index 00000000..f274cabb --- /dev/null +++ b/test/transform/resource/after-delombok/ExtensionMethodAutoboxing.java @@ -0,0 +1,20 @@ +class ExtensionMethodAutoboxing { + public void test() { + Long l1 = 1L; + long l2 = 1L; + Integer i1 = 1; + int i2 = 1; + String string = "test"; + ExtensionMethodAutoboxing.Extensions.boxing(string, l1, i1); + ExtensionMethodAutoboxing.Extensions.boxing(string, l1, i2); + ExtensionMethodAutoboxing.Extensions.boxing(string, l2, i1); + ExtensionMethodAutoboxing.Extensions.boxing(string, l2, i2); + } + + + static class Extensions { + public static String boxing(String string, Long a, int b) { + return string + " " + a + " " + b; + } + } +} \ No newline at end of file diff --git a/test/transform/resource/after-delombok/ExtensionMethodFunctional.java b/test/transform/resource/after-delombok/ExtensionMethodFunctional.java new file mode 100644 index 00000000..bd301543 --- /dev/null +++ b/test/transform/resource/after-delombok/ExtensionMethodFunctional.java @@ -0,0 +1,28 @@ +import java.util.function.Function; +import java.util.function.Consumer; + +class ExtensionMethodFunctional { + public void test() { + String test = "test"; + test = ExtensionMethodFunctional.Extensions.map(test, s -> ExtensionMethodFunctional.Extensions.reverse(s)); + ExtensionMethodFunctional.Extensions.consume(test, s -> System.out.println("1: " + s), s -> System.out.println("2: " + s)); + ExtensionMethodFunctional.Extensions.consume(test, System.out::println, System.out::println); + } + + static class Extensions { + public static R map(T value, Function mapper) { + return mapper.apply(value); + } + + public static String reverse(String string) { + return new StringBuilder(string).reverse().toString(); + } + + @SafeVarargs + public static void consume(T o, Consumer... consumer) { + for (int i = 0; i < consumer.length; i++) { + consumer[i].accept(o); + } + } + } +} diff --git a/test/transform/resource/after-delombok/ExtensionMethodVarargs.java b/test/transform/resource/after-delombok/ExtensionMethodVarargs.java new file mode 100644 index 00000000..237b73ef --- /dev/null +++ b/test/transform/resource/after-delombok/ExtensionMethodVarargs.java @@ -0,0 +1,19 @@ +class ExtensionMethodVarargs { + public void test() { + Long l1 = 1L; + long l2 = 1L; + Integer i1 = 1; + int i2 = 1; + ExtensionMethodVarargs.Extensions.format("%d %d %d %d", l1, l2, i1, i2); + ExtensionMethodVarargs.Extensions.format("%d", l1); + ExtensionMethodVarargs.Extensions.format("", new Integer[] {1, 2}); + ExtensionMethodVarargs.Extensions.format("", new Integer[] {1, 2}, new Integer[] {1, 2}); + } + + + static class Extensions { + public static String format(String string, Object... params) { + return String.format(string, params); + } + } +} \ No newline at end of file diff --git a/test/transform/resource/after-ecj/ExtensionMethodAutoboxing.java b/test/transform/resource/after-ecj/ExtensionMethodAutoboxing.java new file mode 100644 index 00000000..8bc2b30f --- /dev/null +++ b/test/transform/resource/after-ecj/ExtensionMethodAutoboxing.java @@ -0,0 +1,25 @@ +import lombok.experimental.ExtensionMethod; +@ExtensionMethod({ExtensionMethodAutoboxing.Extensions.class}) class ExtensionMethodAutoboxing { + static class Extensions { + Extensions() { + super(); + } + public static String boxing(String string, Long a, int b) { + return ((((string + " ") + a) + " ") + b); + } + } + ExtensionMethodAutoboxing() { + super(); + } + public void test() { + Long l1 = 1l; + long l2 = 1l; + Integer i1 = 1; + int i2 = 1; + String string = "test"; + ExtensionMethodAutoboxing.Extensions.boxing(string, l1, i1); + ExtensionMethodAutoboxing.Extensions.boxing(string, l1, i2); + ExtensionMethodAutoboxing.Extensions.boxing(string, l2, i1); + ExtensionMethodAutoboxing.Extensions.boxing(string, l2, i2); + } +} \ No newline at end of file diff --git a/test/transform/resource/after-ecj/ExtensionMethodFunctional.java b/test/transform/resource/after-ecj/ExtensionMethodFunctional.java new file mode 100644 index 00000000..0190eabb --- /dev/null +++ b/test/transform/resource/after-ecj/ExtensionMethodFunctional.java @@ -0,0 +1,31 @@ +import java.util.function.Function; +import java.util.function.Consumer; +import lombok.experimental.ExtensionMethod; +@ExtensionMethod(ExtensionMethodFunctional.Extensions.class) class ExtensionMethodFunctional { + static class Extensions { + Extensions() { + super(); + } + public static R map(T value, Function mapper) { + return mapper.apply(value); + } + public static String reverse(String string) { + return new StringBuilder(string).reverse().toString(); + } + public static @SafeVarargs void consume(T o, Consumer... consumer) { + for (int i = 0;; (i < consumer.length); i ++) + { + consumer[i].accept(o); + } + } + } + ExtensionMethodFunctional() { + super(); + } + public void test() { + String test = "test"; + test = ExtensionMethodFunctional.Extensions.map(test, ( s) -> ExtensionMethodFunctional.Extensions.reverse(s)); + ExtensionMethodFunctional.Extensions.consume(test, ( s) -> System.out.println(("1: " + s)), ( s) -> System.out.println(("2: " + s))); + ExtensionMethodFunctional.Extensions.consume(test, System.out::println, System.out::println); + } +} diff --git a/test/transform/resource/after-ecj/ExtensionMethodVarargs.java b/test/transform/resource/after-ecj/ExtensionMethodVarargs.java new file mode 100644 index 00000000..727b6494 --- /dev/null +++ b/test/transform/resource/after-ecj/ExtensionMethodVarargs.java @@ -0,0 +1,24 @@ +import lombok.experimental.ExtensionMethod; +@ExtensionMethod(ExtensionMethodVarargs.Extensions.class) class ExtensionMethodVarargs { + static class Extensions { + Extensions() { + super(); + } + public static String format(String string, Object... params) { + return String.format(string, params); + } + } + ExtensionMethodVarargs() { + super(); + } + public void test() { + Long l1 = 1l; + long l2 = 1l; + Integer i1 = 1; + int i2 = 1; + ExtensionMethodVarargs.Extensions.format("%d %d %d %d", l1, l2, i1, i2); + ExtensionMethodVarargs.Extensions.format("%d", l1); + ExtensionMethodVarargs.Extensions.format("", new Integer[]{1, 2}); + ExtensionMethodVarargs.Extensions.format("", new Integer[]{1, 2}, new Integer[]{1, 2}); + } +} \ No newline at end of file diff --git a/test/transform/resource/before/ExtensionMethodAutoboxing.java b/test/transform/resource/before/ExtensionMethodAutoboxing.java new file mode 100644 index 00000000..5e07a6b3 --- /dev/null +++ b/test/transform/resource/before/ExtensionMethodAutoboxing.java @@ -0,0 +1,23 @@ +import lombok.experimental.ExtensionMethod; + +@ExtensionMethod({ExtensionMethodAutoboxing.Extensions.class}) +class ExtensionMethodAutoboxing { + public void test() { + Long l1 = 1l; + long l2 = 1l; + Integer i1 = 1; + int i2 = 1; + + String string = "test"; + string.boxing(l1, i1); + string.boxing(l1, i2); + string.boxing(l2, i1); + string.boxing(l2, i2); + } + + static class Extensions { + public static String boxing(String string, Long a, int b) { + return string + " " + a + " " + b; + } + } +} diff --git a/test/transform/resource/before/ExtensionMethodFunctional.java b/test/transform/resource/before/ExtensionMethodFunctional.java new file mode 100644 index 00000000..19983258 --- /dev/null +++ b/test/transform/resource/before/ExtensionMethodFunctional.java @@ -0,0 +1,33 @@ +// version 8: +import java.util.function.Function; +import java.util.function.Consumer; + +import lombok.experimental.ExtensionMethod; + +@ExtensionMethod(ExtensionMethodFunctional.Extensions.class) +class ExtensionMethodFunctional { + public void test() { + String test = "test"; + test = test.map(s -> s.reverse()); + + test.consume(s -> System.out.println("1: " + s), s -> System.out.println("2: " + s)); + test.consume(System.out::println, System.out::println); + } + + static class Extensions { + public static R map(T value, Function mapper) { + return mapper.apply(value); + } + + public static String reverse(String string) { + return new StringBuilder(string).reverse().toString(); + } + + @SafeVarargs + public static void consume(T o, Consumer... consumer) { + for (int i = 0; i < consumer.length; i++) { + consumer[i].accept(o); + } + } + } +} diff --git a/test/transform/resource/before/ExtensionMethodVarargs.java b/test/transform/resource/before/ExtensionMethodVarargs.java new file mode 100644 index 00000000..a976f9e6 --- /dev/null +++ b/test/transform/resource/before/ExtensionMethodVarargs.java @@ -0,0 +1,23 @@ +// version 8: +import lombok.experimental.ExtensionMethod; + +@ExtensionMethod(ExtensionMethodVarargs.Extensions.class) +class ExtensionMethodVarargs { + public void test() { + Long l1 = 1l; + long l2 = 1l; + Integer i1 = 1; + int i2 = 1; + + "%d %d %d %d".format(l1, l2, i1, i2); + "%d".format(l1); + "".format(new Integer[]{1,2}); + "".format(new Integer[]{1,2}, new Integer[]{1,2}); + } + + static class Extensions { + public static String format(String string, Object... params) { + return String.format(string, params); + } + } +} diff --git a/test/transform/resource/messages-delombok/ExtensionMethodVarargs.java.messages b/test/transform/resource/messages-delombok/ExtensionMethodVarargs.java.messages new file mode 100644 index 00000000..587d0767 --- /dev/null +++ b/test/transform/resource/messages-delombok/ExtensionMethodVarargs.java.messages @@ -0,0 +1 @@ +14 non-varargs call of varargs method with inexact argument type for last parameter \ No newline at end of file diff --git a/test/transform/resource/messages-idempotent/ExtensionMethodVarargs.java.messages b/test/transform/resource/messages-idempotent/ExtensionMethodVarargs.java.messages new file mode 100644 index 00000000..5a1016ca --- /dev/null +++ b/test/transform/resource/messages-idempotent/ExtensionMethodVarargs.java.messages @@ -0,0 +1 @@ +9 non-varargs call of varargs method with inexact argument type for last parameter \ No newline at end of file -- cgit From f7f26a9c5671e9498ba56277bba5c3a5b63155fd Mon Sep 17 00:00:00 2001 From: Rawi01 Date: Mon, 31 Aug 2020 17:04:46 +0200 Subject: Fix ExtensionMethod for older eclipse version --- .../lombok/eclipse/agent/PatchExtensionMethod.java | 70 +++++++++++++++------- 1 file changed, 50 insertions(+), 20 deletions(-) (limited to 'src/eclipseAgent') diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java index 1a1f2274..0b33ff0a 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java @@ -24,6 +24,7 @@ package lombok.eclipse.agent; import static lombok.eclipse.handlers.EclipseHandlerUtil.createAnnotation; import java.lang.ref.WeakReference; +import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -47,7 +48,6 @@ import org.eclipse.jdt.internal.compiler.ast.Annotation; import org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess; import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; import org.eclipse.jdt.internal.compiler.ast.Expression; -import org.eclipse.jdt.internal.compiler.ast.FunctionalExpression; import org.eclipse.jdt.internal.compiler.ast.MessageSend; import org.eclipse.jdt.internal.compiler.ast.NameReference; import org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference; @@ -253,7 +253,13 @@ public class PatchExtensionMethod { if (binding instanceof TypeBinding) skip = true; } // It's impossible to resolve the right method without types - if (methodCall.argumentsHaveErrors) skip = true; + if (Reflection.argumentsHaveErrors != null) { + try { + if ((Boolean) Reflection.argumentsHaveErrors.get(methodCall)) skip = true; + } catch (IllegalAccessException ignore) { + // ignore + } + } if (!skip) for (Extension extension : extensions) { if (!extension.suppressBaseMethods && !(methodCall.binding instanceof ProblemMethodBinding)) continue; @@ -266,21 +272,20 @@ public class PatchExtensionMethod { List arguments = new ArrayList(); arguments.add(methodCall.receiver); if (methodCall.arguments != null) arguments.addAll(Arrays.asList(methodCall.arguments)); - List argumentTypes = new ArrayList(); + Expression[] originalArgs = methodCall.arguments; + methodCall.arguments = arguments.toArray(new Expression[0]); - for (int i = 0; i < arguments.size(); i++) { - Expression argument = arguments.get(i); + List argumentTypes = new ArrayList(); + for (Expression argument : arguments) { TypeBinding argumentType = argument.resolvedType; - if (argumentType == null) { - // Copy unresolved lamdba types - argumentType = methodCall.argumentTypes.length >= i ? methodCall.argumentTypes[i - 1] : null; - } - if (argumentType != null) { - argumentTypes.add(argumentType); + if (argumentType == null && Reflection.isFunctionalExpression(argument)) { + argumentType = Reflection.getPolyTypeBinding(argument); } + if (argumentType == null) { + argumentType = TypeBinding.NULL; + } + argumentTypes.add(argumentType); } - Expression[] originalArgs = methodCall.arguments; - methodCall.arguments = arguments.toArray(new Expression[0]); // Copy generic information. This one covers a few simple cases, more complex cases are still broken int typeVariables = extensionMethod.typeVariables.length; @@ -313,7 +318,7 @@ public class PatchExtensionMethod { param = parameters[i]; } // Resolve types for lambdas - if (arg instanceof FunctionalExpression) { + if (Reflection.isFunctionalExpression(arg)) { arg.setExpectedType(param); arg.resolveType(scope); } @@ -378,16 +383,41 @@ public class PatchExtensionMethod { } private static final class Reflection { - public static final Field argumentTypes; + public static final Field argumentTypes = Permit.permissiveGetField(MessageSend.class, "argumentTypes"); + public static final Field argumentsHaveErrors = Permit.permissiveGetField(MessageSend.class, "argumentsHaveErrors"); + private static final Class functionalExpression; + private static final Constructor polyTypeBindingConstructor; static { - Field a = null; + Class a = null; + Constructor b = null; try { - a = Permit.getField(MessageSend.class, "argumentTypes"); - } catch (Throwable t) { - //ignore - old eclipse versions don't know this one + a = Class.forName("org.eclipse.jdt.internal.compiler.ast.FunctionalExpression"); + } catch (Exception e) { + // Ignore + } + try { + b = Permit.getConstructor(Class.forName("org.eclipse.jdt.internal.compiler.lookup.PolyTypeBinding"), Expression.class); + } catch (Exception e) { + // Ignore + } + functionalExpression = a; + polyTypeBindingConstructor = b; + } + + public static boolean isFunctionalExpression(Expression expression) { + if (functionalExpression == null) return false; + return functionalExpression.isInstance(expression); + } + + public static TypeBinding getPolyTypeBinding(Expression expression) { + if (polyTypeBindingConstructor == null) return null; + try { + return (TypeBinding) polyTypeBindingConstructor.newInstance(expression); + } catch (Exception e) { + // Ignore } - argumentTypes = a; + return null; } } } -- cgit