[java] module naming inspection

This commit is contained in:
Roman Shevchenko
2017-02-17 19:58:32 +01:00
parent d34989b2bf
commit c4111f32cf
5 changed files with 112 additions and 0 deletions
@@ -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)));
}
}
};
}
}
@@ -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 <warning descr="Module name 'foo.bar42' should avoid terminal digits">foo.bar42</warning> { }""")
fix("module <caret>foo.bar42 { }", "module foo.bar { }")
}
fun testTerminalDigitsAndMiddleComments() {
highlighting("""module <warning descr="Module name 'foo.baz42' should avoid terminal digits">foo/*.bar*/.baz42</warning> { }""")
}
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)
}
}
@@ -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
@@ -0,0 +1,7 @@
<html>
<body>
The inspection detects situations when a module name contradicts Java Platform Module System recommendations.
<br>Example:
<code>module <b>foo.bar42</b> { }</code>
</body>
</html>
+4
View File
@@ -907,6 +907,10 @@
enabledByDefault="true" level="WARNING"
key="inspection.module.exports.package.to.itself" bundle="messages.InspectionsBundle"
implementationClass="com.intellij.codeInspection.java19modules.Java9ModuleExportsPackageToItselfInspection"/>
<localInspection language="JAVA" shortName="JavaModuleNaming" enabledByDefault="true" level="WARNING"
groupPath="Java" groupBundle="messages.InspectionsBundle" groupKey="group.names.naming.conventions"
bundle="messages.InspectionsBundle" key="inspection.java.module.naming"
implementationClass="com.intellij.codeInspection.java19modules.JavaModuleNamingInspection"/>
<intentionAction>
<className>com.intellij.codeInsight.intention.impl.SplitIfAction</className>