java: replace explicit type with var: disable if TYPE_USE annotated (IDEA-259100)

GitOrigin-RevId: 4096a327d8c8f9f569fd82c2d6684f7f3d2090fe
This commit is contained in:
Anna Kozlova
2021-01-11 10:31:04 +00:00
committed by intellij-monorepo-bot
parent c53d717005
commit 7793b16164
5 changed files with 56 additions and 17 deletions
@@ -13,6 +13,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
/**
@@ -248,4 +249,20 @@ public final class AnnotationTargetUtil {
}
return list;
}
public static void collectStrictlyTypeUseAnnotations(PsiModifierList modifierList, List<? super PsiAnnotation> annotations) {
if (modifierList == null) return;
for (PsiAnnotation annotation : modifierList.getAnnotations()) {
PsiClass annotationClass = annotation.resolveAnnotationType();
if (annotationClass != null) {
Set<PsiAnnotation.TargetType> targets = getAnnotationTargets(annotationClass);
if (targets != null && targets.contains(PsiAnnotation.TargetType.TYPE_USE) &&
(targets.size() == 1 ||
!ContainerUtil.exists(getTargetsForLocation(modifierList),
target -> target != PsiAnnotation.TargetType.TYPE_USE && targets.contains(target)))) {
annotations.add(annotation);
}
}
}
}
}
@@ -1,6 +1,9 @@
// "Replace explicit type with 'var'" "true"
class Main {
{
var i = 0;
@I var i = 0;
}
}
}
@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE_USE, java.lang.annotation.ElementType.LOCAL_VARIABLE})
@interface I {}
@@ -1,6 +1,9 @@
// "Replace explicit type with 'var'" "true"
class Main {
{
<caret>int i = 0;
@I <caret>int i = 0;
}
}
}
@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE_USE, java.lang.annotation.ElementType.LOCAL_VARIABLE})
@interface I {}
@@ -0,0 +1,9 @@
// "Replace explicit type with 'var'" "false"
class Main {
{
@I <caret>int i = 0;
}
}
@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE_USE})
@interface I {}
@@ -1,6 +1,7 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInspection;
import com.intellij.codeInsight.AnnotationTargetUtil;
import com.intellij.openapi.project.Project;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
@@ -11,6 +12,8 @@ import com.siyeh.InspectionGadgetsBundle;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
public class RedundantExplicitVariableTypeInspection extends AbstractBaseJavaLocalInspectionTool {
@NotNull
@Override
@@ -42,19 +45,23 @@ public class RedundantExplicitVariableTypeInspection extends AbstractBaseJavaLoc
}
}
private void doCheck(PsiVariable variable,
PsiVariable copyVariable,
PsiTypeElement element2Highlight) {
PsiTypeElement typeElementCopy = copyVariable.getTypeElement();
if (typeElementCopy != null) {
IntroduceVariableBase.expandDiamondsAndReplaceExplicitTypeWithVar(typeElementCopy, variable);
if (variable.getType().equals(getNormalizedType(copyVariable))) {
holder.registerProblem(element2Highlight,
InspectionGadgetsBundle.message("inspection.redundant.explicit.variable.type.description"),
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
new ReplaceWithVarFix());
}
}
private void doCheck(PsiVariable variable,
PsiVariable copyVariable,
PsiTypeElement element2Highlight) {
ArrayList<PsiAnnotation> typeUseAnnotations = new ArrayList<>();
AnnotationTargetUtil.collectStrictlyTypeUseAnnotations(copyVariable.getModifierList(), typeUseAnnotations);
if (!typeUseAnnotations.isEmpty()) return;
PsiTypeElement typeElementCopy = copyVariable.getTypeElement();
if (typeElementCopy != null) {
IntroduceVariableBase.expandDiamondsAndReplaceExplicitTypeWithVar(typeElementCopy, variable);
if (variable.getType().equals(getNormalizedType(copyVariable))) {
holder.registerProblem(element2Highlight,
InspectionGadgetsBundle.message("inspection.redundant.explicit.variable.type.description"),
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
new ReplaceWithVarFix());
}
}
}
private PsiType getNormalizedType(PsiVariable copyVariable) {