Skip to content

Commit

Permalink
Make change in all checks for method ref cast and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jjohnstn committed Sep 12, 2024
1 parent 3e90001 commit f6b60d4
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> 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<IJavaCompletionProposal> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<Object> 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
Expand Down

0 comments on commit f6b60d4

Please sign in to comment.