mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
guava inspection: conversion for FluentIterable.last (IDEA-160221)
This commit is contained in:
+18
@@ -229,6 +229,24 @@ public class GuavaFluentIterableConversionRule extends BaseGuavaTypeConversionRu
|
||||
};
|
||||
needSpecifyType = false;
|
||||
}
|
||||
else if (methodName.equals("last")) {
|
||||
descriptorBase = new TypeConversionDescriptor("$it$.last()", null) {
|
||||
@Override
|
||||
public PsiExpression replace(PsiExpression expression, TypeEvaluator evaluator) {
|
||||
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(expression.getProject());
|
||||
String varA = suggestName("a", codeStyleManager, expression);
|
||||
String varB = suggestName("b", codeStyleManager, expression);
|
||||
setReplaceByString("$it$.reduce((" + varA + ", " + varB + ") -> " + varB + ")");
|
||||
return super.replace(expression, evaluator);
|
||||
}
|
||||
|
||||
private String suggestName(String baseName, JavaCodeStyleManager codeStyleManager, PsiElement place) {
|
||||
final SuggestedNameInfo suggestedNameInfo = codeStyleManager
|
||||
.suggestVariableName(VariableKind.LOCAL_VARIABLE, baseName, null, null, false);
|
||||
return codeStyleManager.suggestUniqueVariableName(suggestedNameInfo, place, false).names[0];
|
||||
}
|
||||
};
|
||||
}
|
||||
else {
|
||||
final TypeConversionDescriptorFactory base = DESCRIPTORS_MAP.get(methodName);
|
||||
if (base != null) {
|
||||
|
||||
@@ -289,6 +289,10 @@ public class GuavaInspectionTest extends JavaCodeInsightFixtureTestCase {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testFluentIterableLast() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
private void doTestNoQuickFixes(Class<? extends PsiElement>... highlightedElements) {
|
||||
myFixture.configureByFile(getTestName(true) + ".java");
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.collect.FluentIterable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class LastMigration {
|
||||
|
||||
void m(ArrayList<String> ss, String a, String b) {
|
||||
Optional<String> last = FluentIterable.fr<caret>om(ss).last();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Optional;
|
||||
|
||||
public class LastMigration {
|
||||
|
||||
void m(ArrayList<String> ss, String a, String b) {
|
||||
Optional<String> last = ss.stream().reduce((a1, b1) -> b1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user