diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/java19modules/JavaModuleNamingInspection.java b/java/java-analysis-impl/src/com/intellij/codeInspection/java19modules/JavaModuleNamingInspection.java new file mode 100644 index 000000000000..d4b905dca1f3 --- /dev/null +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/java19modules/JavaModuleNamingInspection.java @@ -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.codeInspection.java19modules; + +import com.intellij.codeInsight.intention.QuickFixFactory; +import com.intellij.codeInspection.BaseJavaLocalInspectionTool; +import com.intellij.codeInspection.InspectionsBundle; +import com.intellij.codeInspection.ProblemsHolder; +import com.intellij.psi.JavaElementVisitor; +import com.intellij.psi.PsiElementVisitor; +import com.intellij.psi.PsiJavaModule; +import com.intellij.psi.PsiJavaModuleReferenceElement; +import com.intellij.psi.util.PsiUtil; +import org.jetbrains.annotations.NotNull; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class JavaModuleNamingInspection extends BaseJavaLocalInspectionTool { + private final Pattern TRAILING_DIGITS = Pattern.compile("^([^0-9]+)\\d+$"); + + @NotNull + @Override + public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) { + return !PsiUtil.isModuleFile(holder.getFile()) ? PsiElementVisitor.EMPTY_VISITOR : new JavaElementVisitor() { + @Override + public void visitModule(PsiJavaModule module) { + super.visitModule(module); + PsiJavaModuleReferenceElement nameElement = module.getNameIdentifier(); + String name = nameElement.getReferenceText(); + Matcher matcher = TRAILING_DIGITS.matcher(name); + if (matcher.matches()) { + String message = InspectionsBundle.message("inspection.java.module.naming.terminal.digits", name); + holder.registerProblem(nameElement, message, QuickFixFactory.getInstance().createRenameElementFix(module, matcher.group(1))); + } + } + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/JavaModuleNamingTest.kt b/java/java-tests/testSrc/com/intellij/codeInspection/JavaModuleNamingTest.kt new file mode 100644 index 000000000000..75d4a4488f9e --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInspection/JavaModuleNamingTest.kt @@ -0,0 +1,46 @@ +/* + * 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 + +import com.intellij.codeInspection.java19modules.JavaModuleNamingInspection +import com.intellij.testFramework.fixtures.LightJava9ModulesCodeInsightFixtureTestCase + +class JavaModuleNamingTest : LightJava9ModulesCodeInsightFixtureTestCase() { + override fun setUp() { + super.setUp() + myFixture.enableInspections(JavaModuleNamingInspection()) + } + + fun testTerminalDigits() { + highlighting("""module foo.bar42 { }""") + fix("module foo.bar42 { }", "module foo.bar { }") + } + + fun testTerminalDigitsAndMiddleComments() { + highlighting("""module foo/*.bar*/.baz42 { }""") + } + + private fun highlighting(text: String) { + myFixture.configureByText("module-info.java", text) + myFixture.checkHighlighting() + } + + private fun fix(before: String, after: String) { + myFixture.configureByText("module-info.java", before) + myFixture.launchAction(myFixture.findSingleIntention("Rename")) + myFixture.checkResult("module-info.java", after, false) + } +} \ 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 b98b29213ee2..dfb627d3a738 100644 --- a/platform/platform-resources-en/src/messages/InspectionsBundle.properties +++ b/platform/platform-resources-en/src/messages/InspectionsBundle.properties @@ -755,6 +755,9 @@ 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.java.module.naming=Java module naming conventions +inspection.java.module.naming.terminal.digits=Module name ''{0}'' should avoid terminal digits + 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/JavaModuleNaming.html b/resources-en/src/inspectionDescriptions/JavaModuleNaming.html new file mode 100644 index 000000000000..b648e70dccd2 --- /dev/null +++ b/resources-en/src/inspectionDescriptions/JavaModuleNaming.html @@ -0,0 +1,7 @@ + + +The inspection detects situations when a module name contradicts Java Platform Module System recommendations. +
Example: +module foo.bar42 { } + + \ No newline at end of file diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index cf0071e5aad3..9721fe30e6c8 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -907,6 +907,10 @@ enabledByDefault="true" level="WARNING" key="inspection.module.exports.package.to.itself" bundle="messages.InspectionsBundle" implementationClass="com.intellij.codeInspection.java19modules.Java9ModuleExportsPackageToItselfInspection"/> + com.intellij.codeInsight.intention.impl.SplitIfAction