make suppress intentions work in module-info.java files

This commit is contained in:
Bas Leijdekkers
2017-03-12 16:46:52 +01:00
parent d803164a12
commit ca51903f4e
8 changed files with 98 additions and 27 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
* Copyright 2000-2017 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.
@@ -154,7 +154,7 @@ public class CompilerErrorTreeView extends NewErrorTreeViewPanel {
}
@Override
protected boolean use15Suppressions(@NotNull final PsiDocCommentOwner container) {
protected boolean use15Suppressions(@NotNull final PsiJavaDocumentedElement container) {
return true;
}
};
@@ -166,7 +166,7 @@ public class CompilerErrorTreeView extends NewErrorTreeViewPanel {
protected SuppressFix getSuppressAction(@NotNull final String id) {
return new SuppressForClassFix(id){
@Override
protected boolean use15Suppressions(@NotNull final PsiDocCommentOwner container) {
protected boolean use15Suppressions(@NotNull final PsiJavaDocumentedElement container) {
return true;
}
};

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2017 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.
@@ -43,8 +43,8 @@ public class SuppressAllForClassFix extends SuppressFix {
@Override
@Nullable
public PsiDocCommentOwner getContainer(final PsiElement element) {
PsiDocCommentOwner container = super.getContainer(element);
public PsiJavaDocumentedElement getContainer(final PsiElement element) {
PsiJavaDocumentedElement container = super.getContainer(element);
if (container == null) {
return null;
}
@@ -55,7 +55,7 @@ public class SuppressAllForClassFix extends SuppressFix {
}
container = parentClass;
}
return container;
return null;
}
@Override
@@ -72,7 +72,7 @@ public class SuppressAllForClassFix extends SuppressFix {
@Override
public void invoke(@NotNull final Project project, @NotNull final PsiElement element) throws IncorrectOperationException {
final PsiDocCommentOwner container = getContainer(element);
final PsiJavaDocumentedElement container = getContainer(element);
LOG.assertTrue(container != null);
if (use15Suppressions(container)) {
final PsiModifierList modifierList = container.getModifierList();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2017 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.
@@ -59,7 +59,7 @@ public class SuppressFix extends AbstractBatchSuppressByNoInspectionCommentFix {
@Override
@Nullable
public PsiDocCommentOwner getContainer(final PsiElement context) {
public PsiJavaDocumentedElement getContainer(final PsiElement context) {
if (context == null || !context.getManager().isInProject(context)) {
return null;
}
@@ -72,23 +72,32 @@ public class SuppressFix extends AbstractBatchSuppressByNoInspectionCommentFix {
return null;
}
PsiElement container = context;
while (container instanceof PsiAnonymousClass || !(container instanceof PsiDocCommentOwner) || container instanceof PsiTypeParameter) {
container = PsiTreeUtil.getParentOfType(container, PsiDocCommentOwner.class);
while (container instanceof PsiAnonymousClass || !(container instanceof PsiJavaDocumentedElement) || container instanceof PsiTypeParameter) {
container = PsiTreeUtil.getParentOfType(container, PsiJavaDocumentedElement.class);
if (container == null) return null;
}
return (PsiDocCommentOwner)container;
return (PsiJavaDocumentedElement)container;
}
@Override
public boolean isAvailable(@NotNull final Project project, @NotNull final PsiElement context) {
PsiDocCommentOwner container = getContainer(context);
PsiJavaDocumentedElement container = getContainer(context);
boolean isValid = container != null && !(container instanceof PsiMethod && container instanceof SyntheticElement);
if (!isValid) {
return false;
}
setText(container instanceof PsiClass
? InspectionsBundle.message("suppress.inspection.class")
: container instanceof PsiMethod ? InspectionsBundle.message("suppress.inspection.method") : InspectionsBundle.message("suppress.inspection.field"));
if (container instanceof PsiJavaModule) {
setText(InspectionsBundle.message("suppress.inspection.module"));
}
else if (container instanceof PsiClass) {
setText(InspectionsBundle.message("suppress.inspection.class"));
}
else if (container instanceof PsiMethod) {
setText(InspectionsBundle.message("suppress.inspection.method"));
}
else {
setText(InspectionsBundle.message("suppress.inspection.field"));
}
return true;
}
@@ -111,7 +120,7 @@ public class SuppressFix extends AbstractBatchSuppressByNoInspectionCommentFix {
return InspectionsBundle.message("suppress.inspection.member");
}
private boolean doSuppress(@NotNull Project project, PsiDocCommentOwner container) {
private boolean doSuppress(@NotNull Project project, PsiJavaDocumentedElement container) {
assert container != null;
if (use15Suppressions(container)) {
final PsiModifierList modifierList = container.getModifierList();
@@ -125,7 +134,7 @@ public class SuppressFix extends AbstractBatchSuppressByNoInspectionCommentFix {
return false;
}
private void suppressByDocComment(@NotNull Project project, PsiDocCommentOwner container) {
private void suppressByDocComment(@NotNull Project project, PsiJavaDocumentedElement container) {
PsiDocComment docComment = container.getDocComment();
PsiManager manager = PsiManager.getInstance(project);
if (docComment == null) {
@@ -147,7 +156,7 @@ public class SuppressFix extends AbstractBatchSuppressByNoInspectionCommentFix {
}
}
protected boolean use15Suppressions(@NotNull PsiDocCommentOwner container) {
protected boolean use15Suppressions(@NotNull PsiJavaDocumentedElement container) {
return JavaSuppressionUtil.canHave15Suppressions(container) &&
!JavaSuppressionUtil.alreadyHas14Suppressions(container);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2017 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.
@@ -17,10 +17,7 @@ package com.intellij.codeInsight.daemon.impl.actions;
import com.intellij.codeInsight.daemon.HighlightDisplayKey;
import com.intellij.codeInspection.InspectionsBundle;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiDeclarationStatement;
import com.intellij.psi.PsiDocCommentOwner;
import com.intellij.psi.PsiElement;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -40,8 +37,8 @@ public class SuppressForClassFix extends SuppressFix {
@Override
@Nullable
public PsiDocCommentOwner getContainer(final PsiElement element) {
PsiDocCommentOwner container = super.getContainer(element);
public PsiJavaDocumentedElement getContainer(final PsiElement element) {
PsiJavaDocumentedElement container = super.getContainer(element);
if (container == null || container instanceof PsiClass){
return null;
}

View File

@@ -0,0 +1,6 @@
/**
* {@linkplain nothing}
*/
@SuppressWarnings("JavadocReference") module com.suppress.test.test.test {
}

View File

@@ -0,0 +1,6 @@
/**
* {@linkplain <error descr="Cannot resolve symbol 'nothing'"><caret>nothing</error>}
*/
module com.suppress.test.test.test {
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2000-2017 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.codeInsight.daemon.quickFix;
import com.intellij.JavaTestUtil;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.javaDoc.JavaDocReferenceInspection;
import com.intellij.openapi.projectRoots.JavaSdkVersion;
import com.intellij.testFramework.IdeaTestUtil;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
import org.jetbrains.annotations.NotNull;
public class SuppressModuleInfoInspectionsTest extends LightCodeInsightFixtureTestCase {
public void testModuleInfo() {
// need to set jdk version here because it is not set correctly in the mock jdk which causes problems in
// com.intellij.codeInspection.JavaSuppressionUtil#canHave15Suppressions()
IdeaTestUtil.setTestVersion(JavaSdkVersion.JDK_1_9, myFixture.getModule(), getTestRootDisposable());
final LocalInspectionTool inspection = new JavaDocReferenceInspection();
myFixture.enableInspections(inspection);
myFixture.configureByFile("module-info.java");
myFixture.testHighlighting();
myFixture.launchAction(myFixture.findSingleIntention("Suppress for module declaration"));
myFixture.checkResultByFile("module-info.after.java");
}
@Override
protected String getTestDataPath() {
return JavaTestUtil.getJavaTestDataPath() + "/codeInsight/daemonCodeAnalyzer/quickFix/suppressModuleInfoInspections";
}
@NotNull
@Override
protected LightProjectDescriptor getProjectDescriptor() {
return JAVA_9;
}
}

View File

@@ -494,6 +494,7 @@ unchecked.warning=Unchecked warning
suppress.all.for.class=Suppress all inspections for class
suppress.inspection.family=Suppress inspection
suppress.inspection.statement=Suppress for statement
suppress.inspection.module=Suppress for module declaration
suppress.inspection.class=Suppress for class
suppress.inspection.field=Suppress for field
suppress.inspection.method=Suppress for method