diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/ConvertLambdaToMethodReferenceFixCore.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/ConvertLambdaToMethodReferenceFixCore.java index 9a847531afb..63195b7986f 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/ConvertLambdaToMethodReferenceFixCore.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/ConvertLambdaToMethodReferenceFixCore.java @@ -372,7 +372,9 @@ private ASTNode castMethodRefIfNeeded(final CompilationUnitRewrite cuRewrite, AS while (parentTypeBinding != null) { IMethodBinding[] parentTypeMethods= parentTypeBinding.getDeclaredMethods(); for (IMethodBinding parentTypeMethod : parentTypeMethods) { - if (parentTypeMethod.getName().equals(parentBinding.getName()) && !parentTypeMethod.isEqualTo(parentBinding)) { + if (parentTypeMethod.getName().equals(parentBinding.getName()) + && parentTypeMethod.getParameterTypes().length == args.size() + && !parentTypeMethod.isEqualTo(parentBinding)) { needCast= true; break; } diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/LambdaExpressionAndMethodRefFixCore.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/LambdaExpressionAndMethodRefFixCore.java index 15bdc0ea007..7e57142be1f 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/LambdaExpressionAndMethodRefFixCore.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/LambdaExpressionAndMethodRefFixCore.java @@ -479,7 +479,9 @@ private ASTNode castMethodRefIfNeeded(final CompilationUnitRewrite cuRewrite, AS while (parentTypeBinding != null) { IMethodBinding[] parentTypeMethods= parentTypeBinding.getDeclaredMethods(); for (IMethodBinding parentTypeMethod : parentTypeMethods) { - if (parentTypeMethod.getName().equals(parentBinding.getName()) && !parentTypeMethod.isEqualTo(parentBinding)) { + if (parentTypeMethod.getName().equals(parentBinding.getName()) + && parentTypeMethod.getParameterTypes().length == args.size() + && !parentTypeMethod.isEqualTo(parentBinding)) { needCast= true; break; } diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest1d8.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest1d8.java index bb50449a157..8a2cf14b372 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest1d8.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest1d8.java @@ -5349,6 +5349,59 @@ void test() { assertExpectedExistInProposals(proposals, new String[] { expected1 }); } + @Test + public void testIssue1047_5() throws Exception { + IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null); + String str= """ + package test1; + import java.util.function.Supplier; + + public class E1 { + public static void func( String ... args) { + } + private void called( Supplier r ) { + } + + } + """; + pack1.createCompilationUnit("E1.java", str, false, null); + + String str1= """ + package test1; + + public class E extends E1 { + + void called( Runnable r, int i ) { + } + + void test() { + called(() -> E1.func(), 3); + } + } + """; + ICompilationUnit cu= pack1.createCompilationUnit("E.java", str1, false, null); + + int offset= str1.indexOf("func()"); + AssistContext context= getCorrectionContext(cu, offset, 0); + assertNoErrors(context); + List proposals= collectAssists(context, false); + assertCorrectLabels(proposals); + String expected1= """ + package test1; + + public class E extends E1 { + + void called( Runnable r, int i ) { + } + + void test() { + called(E1::func, 3); + } + } + """; + assertExpectedExistInProposals(proposals, new String[] { expected1 }); + } + @Test public void testFixParenthesesInLambdaExpressionAdd() throws Exception { IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null); diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest1d8.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest1d8.java index 6d3073e17ff..62f2a0eaf1e 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest1d8.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest1d8.java @@ -959,6 +959,77 @@ default void m() { assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { original }, null); } + @Test + public void testConvertToLambdaAmbiguous04() throws Exception { + IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null); + String sample= """ + package test; + public interface E { + default void m() { + bar(0, new FI() { + @Override + public int foo(int x) { + return x++; + } + }, 3); + baz(0, new ZI() { + @Override + public int zoo() { + return 1; + } + }); + } + + void bar(int i, FI fi, int j); + void bar(int i, FV fv); + + void baz(int i, ZI zi); + void baz(int i, ZV zv); + } + + @FunctionalInterface interface FI { int foo(int a); } + @FunctionalInterface interface FV { void foo(int a); } + + @FunctionalInterface interface ZI { int zoo(); } + @FunctionalInterface interface ZV { void zoo(); } + """; + String original= sample; + ICompilationUnit cu1= pack1.createCompilationUnit("E.java", original, false, null); + + enable(CleanUpConstants.CONVERT_FUNCTIONAL_INTERFACES); + enable(CleanUpConstants.USE_LAMBDA); + + sample= """ + package test; + public interface E { + default void m() { + bar(0, x -> x++, 3); + baz(0, () -> 1); + } + + void bar(int i, FI fi, int j); + void bar(int i, FV fv); + + void baz(int i, ZI zi); + void baz(int i, ZV zv); + } + + @FunctionalInterface interface FI { int foo(int a); } + @FunctionalInterface interface FV { void foo(int a); } + + @FunctionalInterface interface ZI { int zoo(); } + @FunctionalInterface interface ZV { void zoo(); } + """; + String expected1= sample; + + assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 }, null); + + disable(CleanUpConstants.USE_LAMBDA); + enable(CleanUpConstants.USE_ANONYMOUS_CLASS_CREATION); + + assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { original }, null); + } + @Test public void testConvertToLambdaConflictingNames() throws Exception { IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null); @@ -2201,6 +2272,62 @@ void test() { new HashSet<>(Arrays.asList(MultiFixMessages.LambdaExpressionAndMethodRefCleanUp_description))); } + @Test + public void testIssue1047_5() throws Exception { + // Given + IPackageFragment pack= fSourceFolder.createPackageFragment("test1", false, null); + String given1= """ + import java.util.function.Supplier; + + public class E1 { + + static void func( String ... args) { + + } + void called( Supplier r ) { + + } + } + """; // + ICompilationUnit cu1= pack.createCompilationUnit("E1.java", given1, false, null); + + String given= """ + import java.util.function.Supplier; + + public class E extends E1 { + + void called( Runnable r, int i ) { + + } + void test() { + called(() -> E1.func(), 3); + } + } + """; // + + String expected= """ + import java.util.function.Supplier; + + public class E extends E1 { + + void called( Runnable r, int i ) { + + } + void test() { + called(E1::func, 3); + } + } + """; // + // When + ICompilationUnit cu= pack.createCompilationUnit("E.java", given, false, null); + enable(CleanUpConstants.SIMPLIFY_LAMBDA_EXPRESSION_AND_METHOD_REF); + + // Then + assertNotEquals("The class must be changed", given, expected); + assertRefactoringResultAsExpected(new ICompilationUnit[] { cu, cu1 }, new String[] { expected, given1 }, + new HashSet<>(Arrays.asList(MultiFixMessages.LambdaExpressionAndMethodRefCleanUp_description))); + } + @Test public void testBug579393() throws Exception { // Given