Bug 561670 - [cleanup & saveaction] Clean lambda expression into this
method reference
Change-Id: Ibbe15ea4461db349fd27b8a3f1ef5a1105ded57e
Signed-off-by: Fabrice Tiercelin <fabrice.tiercelin@yahoo.fr>
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest18.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest18.java
index 71be2f2..ea64107 100644
--- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest18.java
+++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest18.java
@@ -58,268 +58,6 @@
}
@Test
- public void testSimplifyLambdaExpression() throws Exception {
- IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
- String sample= "" //
- + "package test1;\n" //
- + "\n" //
- + "import static java.util.Calendar.getInstance;\n" //
- + "import static java.util.Calendar.getAvailableLocales;\n" //
- + "\n" //
- + "import java.time.Instant;\n" //
- + "import java.util.ArrayList;\n" //
- + "import java.util.Calendar;\n" //
- + "import java.util.Date;\n" //
- + "import java.util.Locale;\n" //
- + "import java.util.Vector;\n" //
- + "import java.util.function.BiFunction;\n" //
- + "import java.util.function.Function;\n" //
- + "import java.util.function.Supplier;\n" //
- + "\n" //
- + "public class E extends Date {\n" //
- + " public String changeableText = \"foo\";\n" //
- + "\n" //
- + " public Function<String, String> removeParentheses() {\n" //
- + " return (someString) -> someString.trim().toLowerCase();\n" //
- + " }\n" //
- + "\n" //
- + " public Function<String, String> doNotRemoveParenthesesWithSingleVariableDeclaration() {\n" //
- + " return (String someString) -> someString.trim().toLowerCase();\n" //
- + " }\n" //
- + "\n" //
- + " public BiFunction<String, String, Integer> doNotRemoveParenthesesWithTwoParameters() {\n" //
- + " return (someString, anotherString) -> someString.trim().compareTo(anotherString.trim());\n" //
- + " }\n" //
- + "\n" //
- + " public Supplier<Boolean> doNotRemoveParenthesesWithNoParameter() {\n" //
- + " return () -> {System.out.println(\"foo\");return true;};\n" //
- + " }\n" //
- + "\n" //
- + " public Function<String, String> removeReturnAndBrackets() {\n" //
- + " return someString -> {return someString.trim().toLowerCase();};\n" //
- + " }\n" //
- + "\n" //
- + " public Function<String, String> removeReturnAndBracketsWithParentheses() {\n" //
- + " return someString -> {return someString.trim().toLowerCase() + \"bar\";};\n" //
- + " }\n" //
- + "\n" //
- + " public Function<String, String> doNotRemoveReturnWithSeveralStatements() {\n" //
- + " return someString -> {String trimmed = someString.trim();\n" //
- + " return trimmed.toLowerCase();};\n" //
- + " }\n" //
- + "\n" //
- + " public Supplier<ArrayList<String>> useCreationReference() {\n" //
- + " return () -> new ArrayList<>();\n" //
- + " }\n" //
- + "\n" //
- + " public Function<Integer, ArrayList<String>> useCreationReferenceWithParameter() {\n" //
- + " return capacity -> new ArrayList<>(capacity);\n" //
- + " }\n" //
- + "\n" //
- + " public Function<Integer, ArrayList<String>> useCreationReferenceWithParameterAndType() {\n" //
- + " // TODO this can be refactored like useCreationReferenceWithParameter\n" //
- + " return (Integer capacity) -> new ArrayList<>(capacity);\n" //
- + " }\n" //
- + "\n" //
- + " public Function<Integer, ArrayList<String>> doNotRefactorWithExpressions() {\n" //
- + " return capacity -> new ArrayList<>(capacity + 1);\n" //
- + " }\n" //
- + "\n" //
- + " public BiFunction<Integer, Integer, Vector<String>> useCreationReferenceWithParameters() {\n" //
- + " return (initialCapacity, capacityIncrement) -> new Vector<>(initialCapacity, capacityIncrement);\n" //
- + " }\n" //
- + "\n" //
- + " public BiFunction<Integer, Integer, Vector<String>> doNotRefactorShuffledParams() {\n" //
- + " return (initialCapacity, capacityIncrement) -> new Vector<>(capacityIncrement, initialCapacity);\n" //
- + " }\n" //
- + "\n" //
- + " public Function<Date, Long> useMethodReference() {\n" //
- + " return date -> date.getTime();\n" //
- + " }\n" //
- + "\n" //
- + " public BiFunction<Date, Date, Integer> useMethodReferenceWithParameter() {\n" //
- + " return (date, anotherDate) -> date.compareTo(anotherDate);\n" //
- + " }\n" //
- + "\n" //
- + " public Function<String, Long> useTypeReference() {\n" //
- + " return numberInText -> Long.getLong(numberInText);\n" //
- + " }\n" //
- + "\n" //
- + " public static Function<Instant, Date> useTypeReferenceOnClassMethod() {\n" //
- + " return instant -> Date.from(instant);\n" //
- + " }\n" //
- + "\n" //
- + " public static Function<Locale, Calendar> useTypeReferenceOnImportedMethod() {\n" //
- + " return locale -> Calendar.getInstance(locale);\n" //
- + " }\n" //
- + "\n" //
- + " public static Supplier<Locale[]> useTypeReferenceAsSupplier() {\n" //
- + " return () -> Calendar.getAvailableLocales();\n" //
- + " }\n" //
- + "\n" //
- + " public Function<String, Integer> useExpressionMethodReferenceOnLiteral() {\n" //
- + " return textToSearch -> \"AutoRefactor\".indexOf(textToSearch);\n" //
- + " }\n" //
- + "\n" //
- + " public Function<String, Integer> doNotUseExpressionMethodReferenceOnVariable() {\n" //
- + " return textToSearch -> this.changeableText.indexOf(textToSearch);\n" //
- + " }\n" //
- + "\n" //
- + " public Function<Date, Integer> useThisMethodReference() {\n" //
- + " return anotherDate -> this.compareTo(anotherDate);\n" //
- + " }\n" //
- + "\n" //
- + " public Function<Date, Integer> useThisMethodReferenceAddThis() {\n" //
- + " return anotherDate -> this.compareTo(anotherDate);\n" //
- + " }\n" //
- + "\n" //
- + " public Function<Date, Integer> useSuperMethodReference() {\n" //
- + " return anotherDate -> super.compareTo(anotherDate);\n" //
- + " }\n" //
- + "\n" //
- + " public Function<Integer, String> doNotUseConflictingMethodReference() {\n" //
- + " return numberToPrint -> numberToPrint.toString();\n" //
- + " }\n" //
- + "\n" //
- + " public Function<Integer, String> doNotUseConflictingStaticMethodReference() {\n" //
- + " return numberToPrint -> Integer.toString(numberToPrint);\n" //
- + " }\n" //
- + "}\n";
- ICompilationUnit cu1= pack1.createCompilationUnit("E.java", sample, false, null);
-
- enable(CleanUpConstants.SIMPLIFY_LAMBDA_EXPRESSION_AND_METHOD_REF);
-
- sample= "" //
- + "package test1;\n" //
- + "\n" //
- + "import static java.util.Calendar.getInstance;\n" //
- + "import static java.util.Calendar.getAvailableLocales;\n" //
- + "\n" //
- + "import java.time.Instant;\n" //
- + "import java.util.ArrayList;\n" //
- + "import java.util.Calendar;\n" //
- + "import java.util.Date;\n" //
- + "import java.util.Locale;\n" //
- + "import java.util.Vector;\n" //
- + "import java.util.function.BiFunction;\n" //
- + "import java.util.function.Function;\n" //
- + "import java.util.function.Supplier;\n" //
- + "\n" //
- + "public class E extends Date {\n" //
- + " public String changeableText = \"foo\";\n" //
- + "\n" //
- + " public Function<String, String> removeParentheses() {\n" //
- + " return someString -> someString.trim().toLowerCase();\n" //
- + " }\n" //
- + "\n" //
- + " public Function<String, String> doNotRemoveParenthesesWithSingleVariableDeclaration() {\n" //
- + " return (String someString) -> someString.trim().toLowerCase();\n" //
- + " }\n" //
- + "\n" //
- + " public BiFunction<String, String, Integer> doNotRemoveParenthesesWithTwoParameters() {\n" //
- + " return (someString, anotherString) -> someString.trim().compareTo(anotherString.trim());\n" //
- + " }\n" //
- + "\n" //
- + " public Supplier<Boolean> doNotRemoveParenthesesWithNoParameter() {\n" //
- + " return () -> {System.out.println(\"foo\");return true;};\n" //
- + " }\n" //
- + "\n" //
- + " public Function<String, String> removeReturnAndBrackets() {\n" //
- + " return someString -> someString.trim().toLowerCase();\n" //
- + " }\n" //
- + "\n" //
- + " public Function<String, String> removeReturnAndBracketsWithParentheses() {\n" //
- + " return someString -> (someString.trim().toLowerCase() + \"bar\");\n" //
- + " }\n" //
- + "\n" //
- + " public Function<String, String> doNotRemoveReturnWithSeveralStatements() {\n" //
- + " return someString -> {String trimmed = someString.trim();\n" //
- + " return trimmed.toLowerCase();};\n" //
- + " }\n" //
- + "\n" //
- + " public Supplier<ArrayList<String>> useCreationReference() {\n" //
- + " return ArrayList::new;\n" //
- + " }\n" //
- + "\n" //
- + " public Function<Integer, ArrayList<String>> useCreationReferenceWithParameter() {\n" //
- + " return ArrayList::new;\n" //
- + " }\n" //
- + "\n" //
- + " public Function<Integer, ArrayList<String>> useCreationReferenceWithParameterAndType() {\n" //
- + " // TODO this can be refactored like useCreationReferenceWithParameter\n" //
- + " return (Integer capacity) -> new ArrayList<>(capacity);\n" //
- + " }\n" //
- + "\n" //
- + " public Function<Integer, ArrayList<String>> doNotRefactorWithExpressions() {\n" //
- + " return capacity -> new ArrayList<>(capacity + 1);\n" //
- + " }\n" //
- + "\n" //
- + " public BiFunction<Integer, Integer, Vector<String>> useCreationReferenceWithParameters() {\n" //
- + " return Vector::new;\n" //
- + " }\n" //
- + "\n" //
- + " public BiFunction<Integer, Integer, Vector<String>> doNotRefactorShuffledParams() {\n" //
- + " return (initialCapacity, capacityIncrement) -> new Vector<>(capacityIncrement, initialCapacity);\n" //
- + " }\n" //
- + "\n" //
- + " public Function<Date, Long> useMethodReference() {\n" //
- + " return Date::getTime;\n" //
- + " }\n" //
- + "\n" //
- + " public BiFunction<Date, Date, Integer> useMethodReferenceWithParameter() {\n" //
- + " return Date::compareTo;\n" //
- + " }\n" //
- + "\n" //
- + " public Function<String, Long> useTypeReference() {\n" //
- + " return Long::getLong;\n" //
- + " }\n" //
- + "\n" //
- + " public static Function<Instant, Date> useTypeReferenceOnClassMethod() {\n" //
- + " return instant -> Date.from(instant);\n" //
- + " }\n" //
- + "\n" //
- + " public static Function<Locale, Calendar> useTypeReferenceOnImportedMethod() {\n" //
- + " return Calendar::getInstance;\n" //
- + " }\n" //
- + "\n" //
- + " public static Supplier<Locale[]> useTypeReferenceAsSupplier() {\n" //
- + " return Calendar::getAvailableLocales;\n" //
- + " }\n" //
- + "\n" //
- + " public Function<String, Integer> useExpressionMethodReferenceOnLiteral() {\n" //
- + " return \"AutoRefactor\"::indexOf;\n" //
- + " }\n" //
- + "\n" //
- + " public Function<String, Integer> doNotUseExpressionMethodReferenceOnVariable() {\n" //
- + " return textToSearch -> this.changeableText.indexOf(textToSearch);\n" //
- + " }\n" //
- + "\n" //
- + " public Function<Date, Integer> useThisMethodReference() {\n" //
- + " return this::compareTo;\n" //
- + " }\n" //
- + "\n" //
- + " public Function<Date, Integer> useThisMethodReferenceAddThis() {\n" //
- + " return this::compareTo;\n" //
- + " }\n" //
- + "\n" //
- + " public Function<Date, Integer> useSuperMethodReference() {\n" //
- + " return super::compareTo;\n" //
- + " }\n" //
- + "\n" //
- + " public Function<Integer, String> doNotUseConflictingMethodReference() {\n" //
- + " return numberToPrint -> numberToPrint.toString();\n" //
- + " }\n" //
- + "\n" //
- + " public Function<Integer, String> doNotUseConflictingStaticMethodReference() {\n" //
- + " return numberToPrint -> Integer.toString(numberToPrint);\n" //
- + " }\n" //
- + "}\n";
- String expected1= sample;
-
- assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
- }
-
- @Test
public void testConvertToLambda01() throws Exception {
IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
StringBuffer buf= new StringBuffer();
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 c7ed65a..c3619b2 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
@@ -36,7 +36,7 @@
@RunWith(JUnit4.class)
public class CleanUpTest1d8 extends CleanUpTestCase {
@Rule
- public ProjectTestSetup projectsetup = new Java18ProjectTestSetup();
+ public ProjectTestSetup projectsetup= new Java18ProjectTestSetup();
@Override
@Before
@@ -80,4 +80,278 @@
assertRefactoringHasNoChange(new ICompilationUnit[] { cu });
}
+
+ @Test
+ public void testSimplifyLambdaExpression() throws Exception {
+ IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
+ String sample= "" //
+ + "package test1;\n" //
+ + "\n" //
+ + "import static java.util.Calendar.getInstance;\n" //
+ + "import static java.util.Calendar.getAvailableLocales;\n" //
+ + "\n" //
+ + "import java.time.Instant;\n" //
+ + "import java.util.ArrayList;\n" //
+ + "import java.util.Calendar;\n" //
+ + "import java.util.Date;\n" //
+ + "import java.util.Locale;\n" //
+ + "import java.util.Vector;\n" //
+ + "import java.util.function.BiFunction;\n" //
+ + "import java.util.function.Function;\n" //
+ + "import java.util.function.Supplier;\n" //
+ + "\n" //
+ + "public class E extends Date {\n" //
+ + " public String changeableText = \"foo\";\n" //
+ + "\n" //
+ + " public Function<String, String> removeParentheses() {\n" //
+ + " return (someString) -> someString.trim().toLowerCase();\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<String, String> doNotRemoveParenthesesWithSingleVariableDeclaration() {\n" //
+ + " return (String someString) -> someString.trim().toLowerCase();\n" //
+ + " }\n" //
+ + "\n" //
+ + " public BiFunction<String, String, Integer> doNotRemoveParenthesesWithTwoParameters() {\n" //
+ + " return (someString, anotherString) -> someString.trim().compareTo(anotherString.trim());\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Supplier<Boolean> doNotRemoveParenthesesWithNoParameter() {\n" //
+ + " return () -> {System.out.println(\"foo\");return true;};\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<String, String> removeReturnAndBrackets() {\n" //
+ + " return someString -> {return someString.trim().toLowerCase();};\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<String, String> removeReturnAndBracketsWithParentheses() {\n" //
+ + " return someString -> {return someString.trim().toLowerCase() + \"bar\";};\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<String, String> doNotRemoveReturnWithSeveralStatements() {\n" //
+ + " return someString -> {String trimmed = someString.trim();\n" //
+ + " return trimmed.toLowerCase();};\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Supplier<ArrayList<String>> useCreationReference() {\n" //
+ + " return () -> new ArrayList<>();\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<Integer, ArrayList<String>> useCreationReferenceWithParameter() {\n" //
+ + " return capacity -> new ArrayList<>(capacity);\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<Integer, ArrayList<String>> useCreationReferenceWithParameterAndType() {\n" //
+ + " // TODO this can be refactored like useCreationReferenceWithParameter\n" //
+ + " return (Integer capacity) -> new ArrayList<>(capacity);\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<Integer, ArrayList<String>> doNotRefactorWithExpressions() {\n" //
+ + " return capacity -> new ArrayList<>(capacity + 1);\n" //
+ + " }\n" //
+ + "\n" //
+ + " public BiFunction<Integer, Integer, Vector<String>> useCreationReferenceWithParameters() {\n" //
+ + " return (initialCapacity, capacityIncrement) -> new Vector<>(initialCapacity, capacityIncrement);\n" //
+ + " }\n" //
+ + "\n" //
+ + " public BiFunction<Integer, Integer, Vector<String>> doNotRefactorShuffledParams() {\n" //
+ + " return (initialCapacity, capacityIncrement) -> new Vector<>(capacityIncrement, initialCapacity);\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<Date, Long> useMethodReference() {\n" //
+ + " return date -> date.getTime();\n" //
+ + " }\n" //
+ + "\n" //
+ + " public BiFunction<Date, Date, Integer> useMethodReferenceWithParameter() {\n" //
+ + " return (date, anotherDate) -> date.compareTo(anotherDate);\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<String, Long> useTypeReference() {\n" //
+ + " return numberInText -> Long.getLong(numberInText);\n" //
+ + " }\n" //
+ + "\n" //
+ + " public static Function<Instant, Date> useTypeReferenceOnClassMethod() {\n" //
+ + " return instant -> Date.from(instant);\n" //
+ + " }\n" //
+ + "\n" //
+ + " public static Function<Locale, Calendar> useTypeReferenceOnImportedMethod() {\n" //
+ + " return locale -> Calendar.getInstance(locale);\n" //
+ + " }\n" //
+ + "\n" //
+ + " public static Supplier<Locale[]> useTypeReferenceAsSupplier() {\n" //
+ + " return () -> Calendar.getAvailableLocales();\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<String, Integer> useExpressionMethodReferenceOnLiteral() {\n" //
+ + " return textToSearch -> \"AutoRefactor\".indexOf(textToSearch);\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<String, Integer> doNotUseExpressionMethodReferenceOnVariable() {\n" //
+ + " return textToSearch -> this.changeableText.indexOf(textToSearch);\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<Date, Integer> useThisMethodReference() {\n" //
+ + " return anotherDate -> compareTo(anotherDate);\n" //
+ + " }\n" //
+ + "\n" //
+ + " public class InnerClass {\n" //
+ + " public Function<Date, Integer> doNotUseThisMethodReferenceOnTopLevelClassMethod() {\n" //
+ + " return anotherDate -> compareTo(anotherDate);\n" //
+ + " }\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<Date, Integer> useThisMethodReferenceAddThis() {\n" //
+ + " return anotherDate -> this.compareTo(anotherDate);\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<Date, Integer> useSuperMethodReference() {\n" //
+ + " return anotherDate -> super.compareTo(anotherDate);\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<Integer, String> doNotUseConflictingMethodReference() {\n" //
+ + " return numberToPrint -> numberToPrint.toString();\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<Integer, String> doNotUseConflictingStaticMethodReference() {\n" //
+ + " return numberToPrint -> Integer.toString(numberToPrint);\n" //
+ + " }\n" //
+ + "}\n";
+ ICompilationUnit cu1= pack1.createCompilationUnit("E.java", sample, false, null);
+
+ enable(CleanUpConstants.SIMPLIFY_LAMBDA_EXPRESSION_AND_METHOD_REF);
+
+ sample= "" //
+ + "package test1;\n" //
+ + "\n" //
+ + "import static java.util.Calendar.getInstance;\n" //
+ + "import static java.util.Calendar.getAvailableLocales;\n" //
+ + "\n" //
+ + "import java.time.Instant;\n" //
+ + "import java.util.ArrayList;\n" //
+ + "import java.util.Calendar;\n" //
+ + "import java.util.Date;\n" //
+ + "import java.util.Locale;\n" //
+ + "import java.util.Vector;\n" //
+ + "import java.util.function.BiFunction;\n" //
+ + "import java.util.function.Function;\n" //
+ + "import java.util.function.Supplier;\n" //
+ + "\n" //
+ + "public class E extends Date {\n" //
+ + " public String changeableText = \"foo\";\n" //
+ + "\n" //
+ + " public Function<String, String> removeParentheses() {\n" //
+ + " return someString -> someString.trim().toLowerCase();\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<String, String> doNotRemoveParenthesesWithSingleVariableDeclaration() {\n" //
+ + " return (String someString) -> someString.trim().toLowerCase();\n" //
+ + " }\n" //
+ + "\n" //
+ + " public BiFunction<String, String, Integer> doNotRemoveParenthesesWithTwoParameters() {\n" //
+ + " return (someString, anotherString) -> someString.trim().compareTo(anotherString.trim());\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Supplier<Boolean> doNotRemoveParenthesesWithNoParameter() {\n" //
+ + " return () -> {System.out.println(\"foo\");return true;};\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<String, String> removeReturnAndBrackets() {\n" //
+ + " return someString -> someString.trim().toLowerCase();\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<String, String> removeReturnAndBracketsWithParentheses() {\n" //
+ + " return someString -> (someString.trim().toLowerCase() + \"bar\");\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<String, String> doNotRemoveReturnWithSeveralStatements() {\n" //
+ + " return someString -> {String trimmed = someString.trim();\n" //
+ + " return trimmed.toLowerCase();};\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Supplier<ArrayList<String>> useCreationReference() {\n" //
+ + " return ArrayList::new;\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<Integer, ArrayList<String>> useCreationReferenceWithParameter() {\n" //
+ + " return ArrayList::new;\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<Integer, ArrayList<String>> useCreationReferenceWithParameterAndType() {\n" //
+ + " // TODO this can be refactored like useCreationReferenceWithParameter\n" //
+ + " return (Integer capacity) -> new ArrayList<>(capacity);\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<Integer, ArrayList<String>> doNotRefactorWithExpressions() {\n" //
+ + " return capacity -> new ArrayList<>(capacity + 1);\n" //
+ + " }\n" //
+ + "\n" //
+ + " public BiFunction<Integer, Integer, Vector<String>> useCreationReferenceWithParameters() {\n" //
+ + " return Vector::new;\n" //
+ + " }\n" //
+ + "\n" //
+ + " public BiFunction<Integer, Integer, Vector<String>> doNotRefactorShuffledParams() {\n" //
+ + " return (initialCapacity, capacityIncrement) -> new Vector<>(capacityIncrement, initialCapacity);\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<Date, Long> useMethodReference() {\n" //
+ + " return Date::getTime;\n" //
+ + " }\n" //
+ + "\n" //
+ + " public BiFunction<Date, Date, Integer> useMethodReferenceWithParameter() {\n" //
+ + " return Date::compareTo;\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<String, Long> useTypeReference() {\n" //
+ + " return Long::getLong;\n" //
+ + " }\n" //
+ + "\n" //
+ + " public static Function<Instant, Date> useTypeReferenceOnClassMethod() {\n" //
+ + " return instant -> Date.from(instant);\n" //
+ + " }\n" //
+ + "\n" //
+ + " public static Function<Locale, Calendar> useTypeReferenceOnImportedMethod() {\n" //
+ + " return Calendar::getInstance;\n" //
+ + " }\n" //
+ + "\n" //
+ + " public static Supplier<Locale[]> useTypeReferenceAsSupplier() {\n" //
+ + " return Calendar::getAvailableLocales;\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<String, Integer> useExpressionMethodReferenceOnLiteral() {\n" //
+ + " return \"AutoRefactor\"::indexOf;\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<String, Integer> doNotUseExpressionMethodReferenceOnVariable() {\n" //
+ + " return textToSearch -> this.changeableText.indexOf(textToSearch);\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<Date, Integer> useThisMethodReference() {\n" //
+ + " return this::compareTo;\n" //
+ + " }\n" //
+ + "\n" //
+ + " public class InnerClass {\n" //
+ + " public Function<Date, Integer> doNotUseThisMethodReferenceOnTopLevelClassMethod() {\n" //
+ + " return anotherDate -> compareTo(anotherDate);\n" //
+ + " }\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<Date, Integer> useThisMethodReferenceAddThis() {\n" //
+ + " return this::compareTo;\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<Date, Integer> useSuperMethodReference() {\n" //
+ + " return super::compareTo;\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<Integer, String> doNotUseConflictingMethodReference() {\n" //
+ + " return numberToPrint -> numberToPrint.toString();\n" //
+ + " }\n" //
+ + "\n" //
+ + " public Function<Integer, String> doNotUseConflictingStaticMethodReference() {\n" //
+ + " return numberToPrint -> Integer.toString(numberToPrint);\n" //
+ + " }\n" //
+ + "}\n";
+ String expected1= sample;
+
+ assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
+ }
}
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/LambdaExpressionAndMethodRefCleanUp.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/LambdaExpressionAndMethodRefCleanUp.java
index 6616d6f..b6287b0 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/LambdaExpressionAndMethodRefCleanUp.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/LambdaExpressionAndMethodRefCleanUp.java
@@ -55,6 +55,7 @@
import org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext;
import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
+import org.eclipse.jdt.internal.corext.dom.Bindings;
import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFix;
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFix.CompilationUnitRewriteOperation;
@@ -206,7 +207,17 @@
return false;
}
- if (calledExpression instanceof StringLiteral || calledExpression instanceof NumberLiteral
+ if (calledExpression == null) {
+ if (methodBinding != null) {
+ ITypeBinding calledType= methodBinding.getDeclaringClass();
+ ITypeBinding enclosingType= Bindings.getBindingOfParentType(node);
+
+ if (calledType != null && Bindings.isSuperType(calledType, enclosingType)) {
+ rewriteOperations.add(new ReplaceByMethodReferenceOperation(node, methodInvocation));
+ return false;
+ }
+ }
+ } else if (calledExpression instanceof StringLiteral || calledExpression instanceof NumberLiteral
|| calledExpression instanceof ThisExpression) {
rewriteOperations.add(new ReplaceByMethodReferenceOperation(node, methodInvocation));
return false;