From b0b6bb27bf0903b348416bfe3eb87dc444563b03 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Thu, 25 May 2017 20:45:31 +0300 Subject: [PATCH] [java] "dependency on an automatic module" inspection --- .../JavaRequiresAutoModuleInspection.java | 64 +++++++++++++++++++ .../JavaRequiresAutoModuleInspectionTest.kt | 43 +++++++++++++ .../src/messages/InspectionsBundle.properties | 5 ++ .../JavaRequiresAutoModule.html | 6 ++ resources/src/META-INF/IdeaPlugin.xml | 4 ++ 5 files changed, 122 insertions(+) create mode 100644 java/java-analysis-impl/src/com/intellij/codeInspection/java19modules/JavaRequiresAutoModuleInspection.java create mode 100644 java/java-tests/testSrc/com/intellij/java/codeInspection/JavaRequiresAutoModuleInspectionTest.kt create mode 100644 resources-en/src/inspectionDescriptions/JavaRequiresAutoModule.html diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/java19modules/JavaRequiresAutoModuleInspection.java b/java/java-analysis-impl/src/com/intellij/codeInspection/java19modules/JavaRequiresAutoModuleInspection.java new file mode 100644 index 000000000000..672320c8c1ac --- /dev/null +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/java19modules/JavaRequiresAutoModuleInspection.java @@ -0,0 +1,64 @@ +/* + * 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.codeInspection.java19modules; + +import com.intellij.codeInspection.BaseJavaLocalInspectionTool; +import com.intellij.codeInspection.InspectionsBundle; +import com.intellij.codeInspection.ProblemsHolder; +import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel; +import com.intellij.psi.*; +import com.intellij.psi.impl.light.LightJavaModule; +import com.intellij.psi.util.PsiUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; + +public class JavaRequiresAutoModuleInspection extends BaseJavaLocalInspectionTool { + public boolean TRANSITIVE_ONLY = true; + + @Nullable + @Override + public JComponent createOptionsPanel() { + return new SingleCheckboxOptionsPanel(InspectionsBundle.message("inspection.requires.auto.module.option"), this, "TRANSITIVE_ONLY"); + } + + @NotNull + @Override + public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) { + return !PsiUtil.isModuleFile(holder.getFile()) ? PsiElementVisitor.EMPTY_VISITOR : new JavaElementVisitor() { + @Override + public void visitRequiresStatement(PsiRequiresStatement statement) { + super.visitRequiresStatement(statement); + PsiJavaModuleReferenceElement refElement = statement.getReferenceElement(); + if (refElement != null) { + PsiPolyVariantReference reference = refElement.getReference(); + if (reference != null) { + PsiElement target = reference.resolve(); + if (target instanceof LightJavaModule) { + if (!TRANSITIVE_ONLY) { + holder.registerProblem(refElement, InspectionsBundle.message("inspection.requires.auto.module.message")); + } + else if (statement.hasModifierProperty(PsiModifier.TRANSITIVE)) { + holder.registerProblem(refElement, InspectionsBundle.message("inspection.requires.auto.module.transitive")); + } + } + } + } + } + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/JavaRequiresAutoModuleInspectionTest.kt b/java/java-tests/testSrc/com/intellij/java/codeInspection/JavaRequiresAutoModuleInspectionTest.kt new file mode 100644 index 000000000000..e99e1b3d3f64 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/JavaRequiresAutoModuleInspectionTest.kt @@ -0,0 +1,43 @@ +/* + * 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.java.codeInspection + +import com.intellij.codeInspection.java19modules.JavaRequiresAutoModuleInspection +import com.intellij.java.testFramework.fixtures.LightJava9ModulesCodeInsightFixtureTestCase + +class JavaRequiresAutoModuleInspectionTest : LightJava9ModulesCodeInsightFixtureTestCase() { + private lateinit var inspection: JavaRequiresAutoModuleInspection + + override fun setUp() { + super.setUp() + inspection = JavaRequiresAutoModuleInspection() + myFixture.enableInspections(inspection) + } + + fun testTransitive() { + highlighting("""module M { requires transitive lib.claimed; }""") + } + + fun testAny() { + inspection.TRANSITIVE_ONLY = false + highlighting("""module M { requires lib.claimed; }""") + } + + private fun highlighting(text: String) { + myFixture.configureByText("module-info.java", text) + myFixture.checkHighlighting() + } +} \ No newline at end of file diff --git a/platform/platform-resources-en/src/messages/InspectionsBundle.properties b/platform/platform-resources-en/src/messages/InspectionsBundle.properties index 08642f0642b9..aad7670cce23 100644 --- a/platform/platform-resources-en/src/messages/InspectionsBundle.properties +++ b/platform/platform-resources-en/src/messages/InspectionsBundle.properties @@ -767,6 +767,11 @@ inspection.module.exports.package.to.itself=Module exports/opens package to itse exports.to.itself.delete.statement.fix=Delete statement exports.to.itself.delete.module.ref.fix=Delete reference to module ''{0}'' +inspection.requires.auto.module=Java module naming conventions +inspection.requires.auto.module.message='requires' directive for an automatic module +inspection.requires.auto.module.transitive='requires transitive' directive for an automatic module +inspection.requires.auto.module.option=Highlight only transitive dependencies + inspection.replace.with.bulk.message=Iteration can be replaced with bulk ''{0}'' call inspection.replace.with.bulk.fix.name=Replace iteration with bulk ''{0}'' call inspection.replace.with.bulk.fix.family.name=Replace with bulk method call diff --git a/resources-en/src/inspectionDescriptions/JavaRequiresAutoModule.html b/resources-en/src/inspectionDescriptions/JavaRequiresAutoModule.html new file mode 100644 index 000000000000..40f809eb400c --- /dev/null +++ b/resources-en/src/inspectionDescriptions/JavaRequiresAutoModule.html @@ -0,0 +1,6 @@ + + +The inspection warns about use of automatic modules in the "requires" clauses. +Corresponds to -Xlint:requires-automatic and -Xlint:requires-transitive-automatic Javac options. + + \ No newline at end of file diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index e71741a9ed0a..e4d9b21a6d54 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -948,6 +948,10 @@ enabledByDefault="true" level="WARNING" key="inspection.module.exports.package.to.itself" bundle="messages.InspectionsBundle" implementationClass="com.intellij.codeInspection.java19modules.Java9ModuleExportsPackageToItselfInspection"/> +