mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-13 09:19:13 +07:00
inline to anonymous: convert to lambda when applicable (IDEA-173821)
This commit is contained in:
+11
-5
@@ -16,6 +16,7 @@
|
||||
package com.intellij.refactoring.inline;
|
||||
|
||||
import com.intellij.codeInsight.ChangeContextUtil;
|
||||
import com.intellij.codeInspection.AnonymousCanBeLambdaInspection;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Key;
|
||||
@@ -35,10 +36,7 @@ import com.intellij.util.ProcessingContext;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
import static com.intellij.patterns.PlatformPatterns.psiElement;
|
||||
import static com.intellij.patterns.PsiJavaPatterns.psiExpressionStatement;
|
||||
@@ -159,7 +157,15 @@ class InlineToAnonymousConstructorProcessor {
|
||||
}
|
||||
PsiNewExpression superNewExpression = (PsiNewExpression) myNewExpression.replace(superNewExpressionTemplate);
|
||||
superNewExpression = (PsiNewExpression)ChangeContextUtil.decodeContextInfo(superNewExpression, superNewExpression.getAnonymousClass(), null);
|
||||
JavaCodeStyleManager.getInstance(superNewExpression.getProject()).shortenClassReferences(superNewExpression);
|
||||
PsiAnonymousClass newExpressionAnonymousClass = superNewExpression.getAnonymousClass();
|
||||
if (newExpressionAnonymousClass != null &&
|
||||
AnonymousCanBeLambdaInspection.canBeConvertedToLambda(newExpressionAnonymousClass, false, Collections.emptySet())) {
|
||||
PsiExpression lambda = AnonymousCanBeLambdaInspection.replaceAnonymousWithLambda(superNewExpression, newExpressionAnonymousClass.getBaseClassType());
|
||||
JavaCodeStyleManager.getInstance(newExpressionAnonymousClass.getProject()).shortenClassReferences(superNewExpression.replace(lambda));
|
||||
}
|
||||
else {
|
||||
JavaCodeStyleManager.getInstance(superNewExpression.getProject()).shortenClassReferences(superNewExpression);
|
||||
}
|
||||
}
|
||||
|
||||
private void insertInitializerBefore(final PsiClassInitializer initializerBlock, final PsiClass anonymousClass, final PsiElement token)
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
public class <caret>MyComparator implements Comparator<String> {
|
||||
@Override
|
||||
public int compare(String s1, String s2) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void sort(List<String> scores) {
|
||||
scores.sort(new MyComparator());
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
|
||||
void sort(List<String> scores) {
|
||||
scores.sort((s1, s2) -> 0);
|
||||
}
|
||||
}
|
||||
+7
@@ -54,6 +54,10 @@ public class InlineToAnonymousClassTest extends LightRefactoringTestCase {
|
||||
doTest(false, false);
|
||||
}
|
||||
|
||||
public void testConvertToLambdaJava8() throws Exception {
|
||||
doTest(false, false);
|
||||
}
|
||||
|
||||
public void testClassInitializer() throws Exception {
|
||||
doTest(false, false);
|
||||
}
|
||||
@@ -499,6 +503,9 @@ public class InlineToAnonymousClassTest extends LightRefactoringTestCase {
|
||||
|
||||
@Override
|
||||
protected LanguageLevel getLanguageLevel() {
|
||||
if (getTestName(false).endsWith("Java8")) {
|
||||
return LanguageLevel.JDK_1_8;
|
||||
}
|
||||
return LanguageLevel.JDK_1_7;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user