remove local redundant unchecked suppress inspection - replace with existing global one (IDEA-66039)

This commit is contained in:
anna
2011-03-09 20:50:09 +01:00
parent c61a0360a7
commit e1fc66adaa
20 changed files with 139 additions and 265 deletions

View File

@@ -93,7 +93,10 @@ public class RedundantSuppressInspection extends GlobalInspectionTool{
@Nullable @Nullable
private CommonProblemDescriptor[] checkElement(RefClass refEntity, InspectionManager manager, final Project project) { private CommonProblemDescriptor[] checkElement(RefClass refEntity, InspectionManager manager, final Project project) {
final PsiElement psiElement = refEntity.getElement(); return checkElement(refEntity.getElement(), manager, project);
}
public CommonProblemDescriptor[] checkElement(final PsiElement psiElement, InspectionManager manager, Project project) {
final Map<PsiElement, Collection<String>> suppressedScopes = new THashMap<PsiElement, Collection<String>>(); final Map<PsiElement, Collection<String>> suppressedScopes = new THashMap<PsiElement, Collection<String>>();
psiElement.accept(new JavaRecursiveElementWalkingVisitor() { psiElement.accept(new JavaRecursiveElementWalkingVisitor() {
@Override public void visitModifierList(PsiModifierList list) { @Override public void visitModifierList(PsiModifierList list) {
@@ -138,12 +141,8 @@ public class RedundantSuppressInspection extends GlobalInspectionTool{
if (suppressedScopes.values().isEmpty()) return null; if (suppressedScopes.values().isEmpty()) return null;
// have to visit all file from scratch since inspections can be written in any perversive way including checkFile() overriding // have to visit all file from scratch since inspections can be written in any perversive way including checkFile() overriding
final ModifiableModel model = InspectionProjectProfileManager.getInstance(manager.getProject()).getInspectionProfile().getModifiableModel();
InspectionProfileWrapper profile = new InspectionProfileWrapper((InspectionProfile)model);
profile.init(manager.getProject());
Collection<InspectionTool> suppressedTools = new THashSet<InspectionTool>(); Collection<InspectionTool> suppressedTools = new THashSet<InspectionTool>();
InspectionTool[] tools = getInspectionTools(psiElement, manager);
InspectionTool[] tools = profile.getInspectionTools(psiElement);
for (Collection<String> ids : suppressedScopes.values()) { for (Collection<String> ids : suppressedScopes.values()) {
for (String id : ids) { for (String id : ids) {
String shortName = id.trim(); String shortName = id.trim();
@@ -194,7 +193,7 @@ public class RedundantSuppressInspection extends GlobalInspectionTool{
PsiElement element = ((ProblemDescriptor)descriptor).getPsiElement(); PsiElement element = ((ProblemDescriptor)descriptor).getPsiElement();
if (element == null) continue; if (element == null) continue;
PsiElement annotation = SuppressManager.getInstance().getElementToolSuppressedIn(element, toolId); PsiElement annotation = SuppressManager.getInstance().getElementToolSuppressedIn(element, toolId);
if (annotation != null && PsiTreeUtil.isAncestor(suppressedScope, annotation, false)) { if (annotation != null && PsiTreeUtil.isAncestor(suppressedScope, annotation, false) || annotation == null && !PsiTreeUtil.isAncestor(suppressedScope, element, false)) {
hasErrorInsideSuppressedScope = true; hasErrorInsideSuppressedScope = true;
break; break;
} }
@@ -238,11 +237,20 @@ public class RedundantSuppressInspection extends GlobalInspectionTool{
} }
finally { finally {
refManager.inspectionReadActionFinished(); refManager.inspectionReadActionFinished();
globalContext.close(true); globalContext.close(true);
} }
return result.toArray(new ProblemDescriptor[result.size()]); return result.toArray(new ProblemDescriptor[result.size()]);
} }
protected InspectionTool[] getInspectionTools(PsiElement psiElement, InspectionManager manager) {
final ModifiableModel
model = InspectionProjectProfileManager.getInstance(manager.getProject()).getInspectionProfile().getModifiableModel();
InspectionProfileWrapper profile = new InspectionProfileWrapper((InspectionProfile)model);
profile.init(manager.getProject());
return profile.getInspectionTools(psiElement);
}
@Nullable @Nullable
public QuickFix getQuickFix(final String hint) { public QuickFix getQuickFix(final String hint) {

View File

@@ -1,211 +0,0 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInspection;
import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.codeInsight.daemon.GroupNames;
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.codeInsight.daemon.impl.analysis.GenericsHighlightUtil;
import com.intellij.codeInspection.uncheckedWarnings.UncheckedWarningLocalInspection;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.Function;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import java.util.regex.Matcher;
/**
* User: anna
* Date: 2/14/11
*/
public class RedundantUncheckedSuppressWarningsInspection extends BaseJavaLocalInspectionTool {
@Nls
@NotNull
@Override
public String getGroupDisplayName() {
return GroupNames.LANGUAGE_LEVEL_SPECIFIC_GROUP_NAME;
}
@Nls
@NotNull
@Override
public String getDisplayName() {
return "Redundant unchecked warning suppressed";
}
@NotNull
@Override
public String getShortName() {
return "RedundantUncheckedSuppress";
}
@Override
public boolean isEnabledByDefault() {
return true;
}
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new JavaElementVisitor() {
@Override
public void visitReferenceExpression(PsiReferenceExpression expression) {
}
@Override
public void visitComment(PsiComment comment) {
super.visitComment(comment);
if (!PsiUtil.getLanguageLevel(comment).isAtLeast(LanguageLevel.JDK_1_7)) return;
final HashSet<String> tools2Suppress = new HashSet<String>();
collectCommentSuppresses(comment, tools2Suppress);
if (tools2Suppress.contains(RemoveUncheckedWarningFix.UNCHECKED)) {
final PsiElement statement = PsiTreeUtil.skipSiblingsForward(comment, PsiWhiteSpace.class);
if (statement instanceof PsiStatement) {
checkIfSafeToRemoveWarning(comment, statement, holder);
}
}
}
@Override
public void visitAnnotation(PsiAnnotation annotation) {
super.visitAnnotation(annotation);
if (!PsiUtil.getLanguageLevel(annotation).isAtLeast(LanguageLevel.JDK_1_7)) return;
if (Comparing.strEqual(annotation.getQualifiedName(), SuppressWarnings.class.getName())) {
final PsiAnnotationOwner owner = annotation.getOwner();
if (owner instanceof PsiModifierList) {
final PsiModifierListOwner modifierListOwner = PsiTreeUtil.getParentOfType(((PsiModifierList)owner), PsiModifierListOwner.class);
if (modifierListOwner != null) {
final Collection<String> suppressedIds =
SuppressManager.getInstance().getInspectionIdsSuppressedInAnnotation(modifierListOwner);
if (suppressedIds.contains(RemoveUncheckedWarningFix.UNCHECKED)) {
checkIfSafeToRemoveWarning(annotation, modifierListOwner, holder);
}
}
}
}
}
};
}
private static void checkIfSafeToRemoveWarning(PsiElement suppressElement, PsiElement placeToCheckWarningsIn, ProblemsHolder holder) {
final HashSet<PsiElement> warningsElements = new HashSet<PsiElement>();
collectUncheckedWarnings(placeToCheckWarningsIn, warningsElements);
if (warningsElements.isEmpty()) {
final int uncheckedIdx = suppressElement.getText().indexOf(RemoveUncheckedWarningFix.UNCHECKED);
holder.registerProblem(suppressElement,
uncheckedIdx > -1 ? new TextRange(uncheckedIdx, uncheckedIdx + RemoveUncheckedWarningFix.UNCHECKED.length()) : null,
"Redundant suppression", new RemoveUncheckedWarningFix());
}
}
public static void collectUncheckedWarnings(final PsiElement place, final Collection<PsiElement> warningsElements) {
final UncheckedWarningLocalInspection.UncheckedWarningsVisitor visitor =
new UncheckedWarningLocalInspection.UncheckedWarningsVisitor(false) {
@Override
protected void registerProblem(String message, PsiElement psiElement, LocalQuickFix... quickFix) {
warningsElements.add(psiElement);
}
};
final PossibleHeapPollutionVarargsInspection.HeapPollutionVisitor hVisitor = new PossibleHeapPollutionVarargsInspection.HeapPollutionVisitor() {
@Override
protected void registerProblem(PsiMethod method, PsiIdentifier nameIdentifier) {
warningsElements.add(method);
}
};
place.accept(new JavaRecursiveElementVisitor(){
@Override
public void visitElement(PsiElement element) {
super.visitElement(element);
element.accept(visitor);
element.accept(hVisitor);
}
});
}
private static boolean collectCommentSuppresses(PsiElement psiElement, Set<String> tools2Suppress) {
final Matcher matcher = SuppressionUtil.SUPPRESS_IN_LINE_COMMENT_PATTERN.matcher(psiElement.getText());
if (matcher.matches()) {
final String inspections = matcher.group(1);
final String[] toolsIds = inspections.split("[, ]");
for (String toolsId : toolsIds) {
final String id = toolsId.trim();
if (!id.isEmpty()) {
tools2Suppress.add(id);
}
}
return true;
}
return false;
}
private static class RemoveUncheckedWarningFix implements LocalQuickFix {
@NonNls private static final String UNCHECKED = "unchecked";
@NotNull
@Override
public String getName() {
return "Remove redundant \"unchecked\" suppression";
}
@NotNull
@Override
public String getFamilyName() {
return getName();
}
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement psiElement = descriptor.getPsiElement();
if (psiElement instanceof PsiComment) {
final Set<String> tools2Suppress = new LinkedHashSet<String>();
if (collectCommentSuppresses(psiElement, tools2Suppress)) {
tools2Suppress.remove(UNCHECKED);
if (tools2Suppress.isEmpty()) {
psiElement.delete();
} else {
psiElement.replace(JavaPsiFacade.getElementFactory(project).createCommentFromText("//" + SuppressionUtil.SUPPRESS_INSPECTIONS_TAG_NAME + " " + StringUtil
.join(tools2Suppress, ", "), psiElement));
}
}
} else if (psiElement instanceof PsiAnnotation) {
final PsiAnnotation annotation = (PsiAnnotation)psiElement;
final PsiModifierList owner = (PsiModifierList)annotation.getOwner();
final PsiModifierListOwner listOwner = PsiTreeUtil.getParentOfType(owner, PsiModifierListOwner.class);
if (listOwner != null) {
final Collection<String> tool2Suppress = SuppressManager.getInstance().getInspectionIdsSuppressedInAnnotation(listOwner);
tool2Suppress.remove(UNCHECKED);
if (tool2Suppress.isEmpty()) {
annotation.delete();
} else {
annotation.replace(JavaPsiFacade.getElementFactory(project).createAnnotationFromText("@" + annotation.getQualifiedName() + "({" +StringUtil.join(tool2Suppress, new Function<String, String>() {
@Override
public String fun(String s) {
return "\"" + s + "\"";
}
}, ", ")+ "})", annotation));
}
}
}
}
}
}

View File

@@ -118,8 +118,7 @@ public class StandardInspectionToolsProvider implements InspectionToolProvider {
UnknownGuardInspection.class, UnknownGuardInspection.class,
ExplicitTypeCanBeDiamondInspection.class, ExplicitTypeCanBeDiamondInspection.class,
PossibleHeapPollutionVarargsInspection.class, PossibleHeapPollutionVarargsInspection.class
RedundantUncheckedSuppressWarningsInspection.class
}; };
} }
} }

View File

@@ -1,4 +1,4 @@
// "Remove redundant "unchecked" suppression" "true" // "Remove 'unchecked' suppression" "true"
import java.util.ArrayList; import java.util.ArrayList;
public class Test { public class Test {

View File

@@ -1,4 +1,4 @@
// "Remove redundant "unchecked" suppression" "true" // "Remove 'unchecked' suppression" "true"
import java.util.ArrayList; import java.util.ArrayList;
public class Test { public class Test {
@@ -7,7 +7,7 @@ public class Test {
} }
void foo() { void foo() {
//noinspection blah-blah-toolid // noinspection blah-blah-toolid
foo(new ArrayList<String>()); foo(new ArrayList<String>());
} }
} }

View File

@@ -1,4 +1,4 @@
// "Remove redundant "unchecked" suppression" "true" // "Remove 'unchecked' suppression" "true"
import java.util.*; import java.util.*;
public class Test { public class Test {

View File

@@ -1,4 +1,4 @@
// "Remove redundant "unchecked" suppression" "true" // "Remove 'unchecked' suppression" "true"
import java.util.*; import java.util.*;
public class Test { public class Test {

View File

@@ -1,4 +1,4 @@
// "Remove redundant "unchecked" suppression" "true" // "Remove 'unchecked' suppression" "true"
import java.util.*; import java.util.*;
public class Test { public class Test {

View File

@@ -1,4 +1,4 @@
// "Remove redundant "unchecked" suppression" "true" // "Remove 'unchecked' suppression" "true"
import java.util.*; import java.util.*;
@SuppressWarnings({"bla-blah-toolid"}) @SuppressWarnings({"bla-blah-toolid"})

View File

@@ -1,4 +1,4 @@
// "Remove redundant "unchecked" suppression" "true" // "Remove 'unchecked' suppression" "true"
import java.util.ArrayList; import java.util.ArrayList;
public class Test { public class Test {
@@ -6,8 +6,8 @@ public class Test {
static <T> void foo(T... t){ static <T> void foo(T... t){
} }
void foo() { void fo<caret>o() {
//noinspection unc<caret>hecked //noinspection unchecked
foo(new ArrayList<String>()); foo(new ArrayList<String>());
} }
} }

View File

@@ -1,11 +1,11 @@
// "Remove redundant "unchecked" suppression" "false" // "Remove 'unchecked' suppression" "false"
@SuppressWarnings({"unc<caret>hecked", "bla-blah-toolid"}) @SuppressWarnings({"unchecked", "bla-blah-toolid"})
public enum Planet { public enum Planet {
MERCURY(), MERCURY(),
VENUS(); VENUS();
<T> Planet(T... ts) { <T> Plan<caret>et(T... ts) {
} }
} }

View File

@@ -1,4 +1,4 @@
// "Remove redundant "unchecked" suppression" "true" // "Remove 'unchecked' suppression" "true"
import java.util.ArrayList; import java.util.ArrayList;
public class Test { public class Test {
@@ -6,8 +6,8 @@ public class Test {
static <T> void foo(T... t){ static <T> void foo(T... t){
} }
void foo() { void f<caret>oo() {
//noinspection unc<caret>hecked,blah-blah-toolid //noinspection unchecked,blah-blah-toolid
foo(new ArrayList<String>()); foo(new ArrayList<String>());
} }
} }

View File

@@ -1,4 +1,4 @@
// "Remove redundant "unchecked" suppression" "false" // "Remove 'unchecked' suppression" "false"
import java.util.*; import java.util.*;
public class Test { public class Test {
@@ -7,9 +7,13 @@ public class Test {
return null; return null;
} }
void foo() { static <T> List<T> foo1(T... t){
//noinspection unc<caret>hecked return null;
foo(new ArrayList<String>()).addAll(Arrays.asList(new ArrayList<String>())); }
void fo<caret>o() {
//noinspection unchecked
foo(new ArrayList<String>()).addAll(foo1(new ArrayList<String>()));
} }
} }

View File

@@ -1,4 +1,4 @@
// "Remove redundant "unchecked" suppression" "true" // "Remove 'unchecked' suppression" "true"
import java.util.*; import java.util.*;
public class Test { public class Test {
@@ -7,8 +7,8 @@ public class Test {
return null; return null;
} }
void foo() { void fo<caret>o() {
//noinspection unc<caret>hecked //noinspection unchecked
List<ArrayList<String>> list = foo(new ArrayList<String>()); List<ArrayList<String>> list = foo(new ArrayList<String>());
} }
} }

View File

@@ -1,4 +1,4 @@
// "Remove redundant "unchecked" suppression" "true" // "Remove 'unchecked' suppression" "true"
import java.util.*; import java.util.*;
public class Test { public class Test {
@@ -7,8 +7,8 @@ public class Test {
return null; return null;
} }
@SuppressWarnings("unc<caret>hecked") @SuppressWarnings("unchecked")
void foo() { void fo<caret>o() {
List<ArrayList<String>> list = foo(new ArrayList<String>()); List<ArrayList<String>> list = foo(new ArrayList<String>());
} }
} }

View File

@@ -1,8 +1,8 @@
// "Remove redundant "unchecked" suppression" "true" // "Remove 'unchecked' suppression" "true"
import java.util.*; import java.util.*;
@SuppressWarnings("unc<caret>hecked") @SuppressWarnings("unchecked")
public class Test { public class Te<caret>st {
@SafeVarargs @SafeVarargs
static <T> List<T> foo(T... t){ static <T> List<T> foo(T... t){
return null; return null;

View File

@@ -1,15 +1,19 @@
// "Remove redundant "unchecked" suppression" "false" // "Remove 'unchecked' suppression" "false"
import java.util.*; import java.util.*;
@SuppressWarnings("unc<caret>hecked")
public class Test { public class Test {
@SafeVarargs @SafeVarargs
static <T> List<T> foo(T... t){ static <T> List<T> foo(T... t){
return null; return null;
} }
static <T> List<T> foo1(T... t){
return null;
}
@SuppressWarnings("unchecked")
void foo() { void foo() {
foo(new ArrayList<String>()).addAll(Arrays.asList(new ArrayList<String>); foo(new ArrayList<String>()).addAll(foo1(new ArrayList<String>);
} }
} }

View File

@@ -1,8 +1,8 @@
// "Remove redundant "unchecked" suppression" "false" // "Remove 'unchecked' suppression" "false"
import java.util.*; import java.util.*;
@SuppressWarnings("AL<caret>L") @SuppressWarnings("ALL")
public class Test { public class Te<caret>st {
@SafeVarargs @SafeVarargs
static <T> List<T> foo(T... t){ static <T> List<T> foo(T... t){
return null; return null;

View File

@@ -1,8 +1,8 @@
// "Remove redundant "unchecked" suppression" "true" // "Remove 'unchecked' suppression" "true"
import java.util.*; import java.util.*;
@SuppressWarnings({"unc<caret>hecked", "bla-blah-toolid"}) @SuppressWarnings({"unchecked", "bla-blah-toolid"})
public class Test { public class Tes<caret>t {
@SafeVarargs @SafeVarargs
static <T> List<T> foo(T... t){ static <T> List<T> foo(T... t){
return null; return null;

View File

@@ -15,17 +15,87 @@
*/ */
package com.intellij.codeInsight.daemon.quickFix; package com.intellij.codeInsight.daemon.quickFix;
import com.intellij.codeInspection.LocalInspectionTool; import com.intellij.codeInspection.*;
import com.intellij.codeInspection.PossibleHeapPollutionVarargsInspection; import com.intellij.codeInspection.ex.InspectionTool;
import com.intellij.codeInspection.RedundantUncheckedSuppressWarningsInspection; import com.intellij.codeInspection.ex.LocalInspectionToolWrapper;
import com.intellij.codeInspection.reference.RefFileImpl;
import com.intellij.codeInspection.uncheckedWarnings.UncheckedWarningLocalInspection;
import com.intellij.psi.*;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
public class RemoveRedundantUncheckedSuppressionTest extends LightQuickFixTestCase { public class RemoveRedundantUncheckedSuppressionTest extends LightQuickFixTestCase {
@Override @Override
protected LocalInspectionTool[] configureLocalInspectionTools() { protected LocalInspectionTool[] configureLocalInspectionTools() {
return new LocalInspectionTool[]{ final PossibleHeapPollutionVarargsInspection varargsInspection = new PossibleHeapPollutionVarargsInspection();
new RedundantUncheckedSuppressWarningsInspection(), final UncheckedWarningLocalInspection warningLocalInspection = new UncheckedWarningLocalInspection();
new PossibleHeapPollutionVarargsInspection(), final RedundantSuppressInspection inspection = new RedundantSuppressInspection(){
@Override
protected InspectionTool[] getInspectionTools(PsiElement psiElement, InspectionManager manager) {
return new InspectionTool[]{
new LocalInspectionToolWrapper(varargsInspection),
new LocalInspectionToolWrapper(warningLocalInspection)
};
}
};
return new LocalInspectionTool[] {
new LocalInspectionTool() {
@Nls
@NotNull
@Override
public String getGroupDisplayName() {
return inspection.getGroupDisplayName();
}
@Nls
@NotNull
@Override
public String getDisplayName() {
return inspection.getDisplayName();
}
@NotNull
@Override
public String getShortName() {
return inspection.getShortName();
}
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder,
boolean isOnTheFly,
LocalInspectionToolSession session) {
return new JavaElementVisitor() {
@Override
public void visitReferenceExpression(PsiReferenceExpression expression) {
}
@Override
public void visitClass(PsiClass aClass) {
checkMember(aClass, inspection, holder);
}
@Override
public void visitMethod(PsiMethod method) {
checkMember(method, inspection, holder);
}
};
}
private void checkMember(PsiMember member, RedundantSuppressInspection inspection, ProblemsHolder holder) {
final ProblemDescriptor[] problemDescriptors =
(ProblemDescriptor[])inspection.checkElement(member, InspectionManager.getInstance(getProject()), getProject());
if (problemDescriptors != null) {
for (ProblemDescriptor problemDescriptor : problemDescriptors) {
holder.registerProblem(problemDescriptor);
}
}
}
},
varargsInspection,
warningLocalInspection
}; };
} }