var -> explicit: fix lambdas with multiple parameters (IDEA-194414)

This commit is contained in:
Anna Kozlova
2018-06-22 18:15:50 +03:00
parent e7743c0b00
commit 17095a1640
5 changed files with 79 additions and 11 deletions
@@ -8,6 +8,9 @@ import com.intellij.psi.util.PsiUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
public class VariableTypeCanBeExplicitInspection extends AbstractBaseJavaLocalInspectionTool {
@NotNull
@Override
@@ -16,18 +19,44 @@ public class VariableTypeCanBeExplicitInspection extends AbstractBaseJavaLocalIn
return PsiElementVisitor.EMPTY_VISITOR;
}
return new JavaElementVisitor() {
@Override
public void visitLambdaExpression(PsiLambdaExpression expression) {
List<PsiTypeElement> typeElements = new ArrayList<>();
for (PsiParameter parameter: expression.getParameterList().getParameters()) {
PsiTypeElement typeElement = getTypeElementToExpand(parameter);
if (typeElement == null) return;
typeElements.add(typeElement);
}
for (PsiTypeElement typeElement: typeElements) {
registerTypeElementProblem(typeElement);
}
}
@Override
public void visitVariable(PsiVariable variable) {
PsiTypeElement typeElement = getTypeElementToExpand(variable);
if (typeElement != null) {
registerTypeElementProblem(typeElement);
}
}
private void registerTypeElementProblem(PsiTypeElement typeElement) {
holder.registerProblem(typeElement,
"'var' can be replaced with explicit type",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
new ReplaceVarWithExplicitTypeFix());
}
private PsiTypeElement getTypeElementToExpand(PsiVariable variable) {
PsiTypeElement typeElement = variable.getTypeElement();
if (typeElement != null && typeElement.isInferredType()) {
PsiType type = variable.getType();
if (PsiTypesUtil.isDenotableType(type, variable)) {
holder.registerProblem(typeElement,
"'var' can be replaced with explicit type",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
new ReplaceVarWithExplicitTypeFix());
return typeElement;
}
}
return null;
}
};
}
@@ -44,7 +73,21 @@ public class VariableTypeCanBeExplicitInspection extends AbstractBaseJavaLocalIn
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement element = descriptor.getPsiElement();
if (element instanceof PsiTypeElement) {
PsiTypesUtil.replaceWithExplicitType((PsiTypeElement)element);
PsiElement parent = element.getParent();
if (parent instanceof PsiParameter) {
PsiElement declarationScope = ((PsiParameter)parent).getDeclarationScope();
if (declarationScope instanceof PsiLambdaExpression) {
for (PsiParameter parameter: ((PsiLambdaExpression)declarationScope).getParameterList().getParameters()) {
PsiTypeElement typeElement = parameter.getTypeElement();
if (typeElement != null) {
PsiTypesUtil.replaceWithExplicitType(typeElement);
}
}
}
}
else {
PsiTypesUtil.replaceWithExplicitType((PsiTypeElement)element);
}
}
}
}
@@ -0,0 +1,9 @@
// "Replace 'var' with explicit type" "true"
import java.util.function.BiFunction;
final class Example {
void m() {
BiFunction<Integer, ? extends String, Integer> graph = (Integer x1, String x) -> x1*2;
}
}
@@ -0,0 +1,13 @@
// "Replace 'var' with explicit type" "false"
final class Example<J, T> {
interface I<A, B> {
void m(A a, B b);
}
void m(I<T, J> i) {}
void m(Example<? super String, Integer> e) {
e.m((var a, v<caret>ar b) -> {});
}
}
@@ -0,0 +1,9 @@
// "Replace 'var' with explicit type" "true"
import java.util.function.BiFunction;
final class Example {
void m() {
BiFunction<Integer, ? extends String, Integer> graph = (var x1, v<caret>ar x) -> x1*2;
}
}
@@ -4,7 +4,6 @@ package com.intellij.java.codeInsight.daemon.quickFix;
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.VariableTypeCanBeExplicitInspection;
import com.intellij.pom.java.LanguageLevel;
import org.jetbrains.annotations.NotNull;
@@ -19,11 +18,6 @@ public class VariableTypeCanBeExplicitInspectionTest extends LightQuickFixParame
public void test() { doAllTests(); }
@Override
protected LanguageLevel getLanguageLevel() {
return LanguageLevel.JDK_10;
}
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/var2explicit";