new "'package-info.java' without 'package' statement" inspection

This commit is contained in:
Bas Leijdekkers
2013-10-01 20:01:57 +02:00
parent ffe104b6ec
commit f3cc029db1
8 changed files with 175 additions and 1 deletions
@@ -1203,6 +1203,10 @@
key="missing.package.info.display.name" groupBundle="messages.InspectionsBundle"
groupKey="group.names.javadoc.issues" enabledByDefault="false" level="WARNING"
implementationClass="com.siyeh.ig.javadoc.MissingPackageInfoInspection"/>
<localInspection shortName="PackageInfoWithoutPackage" bundle="com.siyeh.InspectionGadgetsBundle"
key="package.info.java.without.package.display.name" groupBundle="messages.InspectionsBundle"
groupKey="group.names.javadoc.issues" enabledByDefault="false" level="WARNING"
implementationClass="com.siyeh.ig.javadoc.PackageInfoWithoutPackageInspection"/>
<localInspection shortName="PackageDotHtmlMayBePackageInfo" bundle="com.siyeh.InspectionGadgetsBundle"
key="package.dot.html.may.be.package.info.display.name" groupBundle="messages.InspectionsBundle"
groupKey="group.names.javadoc.issues" enabledByDefault="false" level="WARNING"
@@ -2040,4 +2040,8 @@ unnecessary.unicode.escape.display.name=Unnecessary unicode escape sequence
unnecessary.unicode.escape.problem.descriptor=Unicode escape sequence <code>#ref</code> can be replaced with ''{0}''
missing.package.info.display.name=Missing 'package-info.java'
missing.package.info.problem.descriptor=Package ''{0}'' is missing a <code>package-info.java</code> file
missing.package.html.problem.descriptor=Package ''{0}'' is missing a <code>package.html</code> file
missing.package.html.problem.descriptor=Package ''{0}'' is missing a <code>package.html</code> file
package.info.java.without.package.display.name='package-info.java' without 'package' statement
package.info.without.package.problem.descriptor='package-info.java' does not have a package statement
package.info.without.package.quickfix=add ''package {0};''
package.info.without.package.family.quickfix=add package statement
@@ -0,0 +1,116 @@
/*
* Copyright 2000-2013 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.siyeh.ig.javadoc;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author Bas Leijdekkers
*/
public class PackageInfoWithoutPackageInspection extends BaseInspection {
@Nls
@NotNull
@Override
public String getDisplayName() {
return InspectionGadgetsBundle.message("package.info.java.without.package.display.name");
}
@NotNull
@Override
protected String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message("package.info.without.package.problem.descriptor");
}
@Nullable
@Override
protected InspectionGadgetsFix buildFix(Object... infos) {
return new PackageInfoWithoutPackageFix((String)infos[0]);
}
private static class PackageInfoWithoutPackageFix extends InspectionGadgetsFix {
private final String myPackageName;
public PackageInfoWithoutPackageFix(String packageName) {
myPackageName = packageName;
}
@NotNull
@Override
public String getName() {
return InspectionGadgetsBundle.message("package.info.without.package.quickfix", myPackageName);
}
@NotNull
@Override
public String getFamilyName() {
return InspectionGadgetsBundle.message("package.info.without.package.family.quickfix");
}
@Override
protected void doFix(Project project, ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
if (!(element instanceof PsiJavaFile)) {
return;
}
final PsiJavaFile file = (PsiJavaFile)element;
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
final PsiPackageStatement packageStatement = factory.createPackageStatement(myPackageName);
file.add(packageStatement);
}
}
@Override
public BaseInspectionVisitor buildVisitor() {
return new PackageInfoWithoutPackageVisitor();
}
private static class PackageInfoWithoutPackageVisitor extends BaseInspectionVisitor {
@Override
public void visitJavaFile(PsiJavaFile file) {
final PsiPackageStatement packageStatement = file.getPackageStatement();
if (packageStatement != null) {
return;
}
@NonNls final String name = file.getName();
if (!PsiPackage.PACKAGE_INFO_FILE.equals(name)) {
return;
}
final JavaDirectoryService directoryService = JavaDirectoryService.getInstance();
final PsiDirectory directory = file.getContainingDirectory();
final PsiPackage aPackage = directoryService.getPackage(directory);
if (aPackage == null) {
return;
}
final String packageName = aPackage.getQualifiedName();
if (packageName.isEmpty()) {
return;
}
registerError(file, packageName);
}
}
}
@@ -0,0 +1,9 @@
<html>
<body>
Reports <b>package-info.java</b> files without a <b>package</b> statement.
The Javadoc tool considers such files documentation for the default package even when the file is located somewhere else.
<!-- tooltip end -->
<p>
<small>New in 13</small>
</body>
</html>
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>package-info.java</file>
<line>1</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">'package-info.java' without 'package' statement</problem_class>
<description>'package-info.java' does not have a package statement</description>
</problem>
</problems>
@@ -0,0 +1,3 @@
/**
* Some explanation for the existence of this package
*/
@@ -0,0 +1,28 @@
/*
* Copyright 2000-2013 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.siyeh.ig.javadoc;
import com.siyeh.ig.IGInspectionTestCase;
/**
* @author Bas Leijdekkers
*/
public class PackageInfoWithoutPackageInspectionTest extends IGInspectionTestCase {
public void test() {
doTest("com/siyeh/igtest/javadoc/package_info_without_package", new PackageInfoWithoutPackageInspection());
}
}