mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
introduce functional: convert to lambda when applicable (IDEA-174018)
This commit is contained in:
+13
-1
@@ -17,6 +17,7 @@ package com.intellij.refactoring.introduceVariable;
|
||||
|
||||
import com.intellij.codeInsight.FunctionalInterfaceSuggester;
|
||||
import com.intellij.codeInsight.navigation.NavigationUtil;
|
||||
import com.intellij.codeInspection.AnonymousCanBeLambdaInspection;
|
||||
import com.intellij.ide.util.PsiClassListCellRenderer;
|
||||
import com.intellij.openapi.actionSystem.DataContext;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
@@ -159,9 +160,20 @@ public class IntroduceFunctionalVariableHandler extends IntroduceVariableHandler
|
||||
}
|
||||
}
|
||||
final PsiMethodCallExpression methodCall = processor.getMethodCall();
|
||||
PsiExpression psiExpression = factory
|
||||
PsiMethodCallExpression psiExpression = (PsiMethodCallExpression)factory
|
||||
.createExpressionFromText("new " + selectedType.getCanonicalText() + "() {" + extractedMethod.getText() + "}." + methodCall.getText(),
|
||||
methodCall);
|
||||
PsiExpression qualifierExpression = psiExpression.getMethodExpression().getQualifierExpression();
|
||||
assert qualifierExpression != null;
|
||||
if (AnonymousCanBeLambdaInspection.canBeConvertedToLambda(((PsiNewExpression)qualifierExpression).getAnonymousClass(), false, Collections.emptySet())) {
|
||||
PsiExpression castExpression = JavaPsiFacade.getElementFactory(project)
|
||||
.createExpressionFromText("((" +
|
||||
selectedType.getCanonicalText() + ")" +
|
||||
AnonymousCanBeLambdaInspection.replaceAnonymousWithLambda(qualifierExpression, selectedType).getText() +
|
||||
")", qualifierExpression);
|
||||
qualifierExpression.replace(castExpression);
|
||||
}
|
||||
|
||||
processor.getExtractedMethod().delete();
|
||||
return (PsiMethodCallExpression)JavaCodeStyleManager.getInstance(project).shortenClassReferences(methodCall.replace(psiExpression));
|
||||
}
|
||||
|
||||
+2
-6
@@ -3,12 +3,8 @@ import java.util.function.Supplier;
|
||||
class Test {
|
||||
void foo() {
|
||||
if (true) {
|
||||
Supplier<String> supplier = new Supplier<String>() {
|
||||
public String get() {
|
||||
return "Hello, world";
|
||||
}
|
||||
};
|
||||
System.out.println(supplier.get());
|
||||
Supplier<String> stringSupplier = () -> "Hello, world";
|
||||
System.out.println(stringSupplier.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-6
@@ -2,11 +2,7 @@ import java.util.function.Supplier;
|
||||
|
||||
class Test {
|
||||
void foo() {
|
||||
Supplier<String> supplier = new Supplier<String>() {
|
||||
public String get() {
|
||||
return "Hello, world";
|
||||
}
|
||||
};
|
||||
System.out.println(supplier.get());
|
||||
Supplier<String> stringSupplier = () -> "Hello, world";
|
||||
System.out.println(stringSupplier.get());
|
||||
}
|
||||
}
|
||||
+2
-6
@@ -4,12 +4,8 @@ class Test {
|
||||
String myName;
|
||||
void foo() {
|
||||
if (true) {
|
||||
Consumer<String> consumer = new Consumer<String>() {
|
||||
public void accept(String myName) {
|
||||
System.out.println("Hello, world " + myName);
|
||||
}
|
||||
};
|
||||
consumer.accept(myName);
|
||||
Consumer<String> stringConsumer = myName -> System.out.println("Hello, world " + myName);
|
||||
stringConsumer.accept(myName);
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-5
@@ -1,11 +1,7 @@
|
||||
class Test {
|
||||
void foo(String name) {
|
||||
System.out.println("Hello, ");
|
||||
Runnable runnable = new Runnable() {
|
||||
public void run() {
|
||||
System.out.println(name);
|
||||
}
|
||||
};
|
||||
Runnable runnable = () -> System.out.println(name);
|
||||
runnable.run();
|
||||
}
|
||||
}
|
||||
+2
-6
@@ -3,12 +3,8 @@ import java.util.function.Consumer;
|
||||
class Test {
|
||||
void foo(String s) {
|
||||
if (true) {
|
||||
Consumer<String> consumer = new Consumer<String>() {
|
||||
public void accept(String s) {
|
||||
System.out.println("Hello, world " + s);
|
||||
}
|
||||
};
|
||||
consumer.accept(s);
|
||||
Consumer<String> stringConsumer = s1 -> System.out.println("Hello, world " + s1);
|
||||
stringConsumer.accept(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
-6
@@ -3,13 +3,11 @@ import java.util.function.Consumer;
|
||||
class Test {
|
||||
void foo(String s) {
|
||||
if (true) {
|
||||
Consumer<String> consumer = new Consumer<String>() {
|
||||
public void accept(String s) {
|
||||
System.out.println("Hello, world " + s);
|
||||
System.out.println();
|
||||
}
|
||||
Consumer<String> stringConsumer = s1 -> {
|
||||
System.out.println("Hello, world " + s1);
|
||||
System.out.println();
|
||||
};
|
||||
consumer.accept(s);
|
||||
stringConsumer.accept(s);
|
||||
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user