From f3cc029db1c35df1300ba4ea8753ef080ce243d1 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Tue, 1 Oct 2013 20:01:36 +0200 Subject: [PATCH] new "'package-info.java' without 'package' statement" inspection --- .../src/META-INF/InspectionGadgets.xml | 4 + .../siyeh/InspectionGadgetsBundle.properties | 6 +- .../PackageInfoWithoutPackageInspection.java | 116 ++++++++++++++++++ .../PackageInfoWithoutPackage.html | 9 ++ .../package_info_without_package/expected.xml | 9 ++ .../src/one/two/package-info.java | 1 + .../src/one/two/three/package-info.java | 3 + ...ckageInfoWithoutPackageInspectionTest.java | 28 +++++ 8 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/javadoc/PackageInfoWithoutPackageInspection.java create mode 100644 plugins/InspectionGadgets/src/inspectionDescriptions/PackageInfoWithoutPackage.html create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/package_info_without_package/expected.xml create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/package_info_without_package/src/one/two/package-info.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/package_info_without_package/src/one/two/three/package-info.java create mode 100644 plugins/InspectionGadgets/testsrc/com/siyeh/ig/javadoc/PackageInfoWithoutPackageInspectionTest.java diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml index eae3fc5949de..67e54faa2559 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml @@ -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"/> + #ref can be replaced with ''{0}'' missing.package.info.display.name=Missing 'package-info.java' missing.package.info.problem.descriptor=Package ''{0}'' is missing a package-info.java file -missing.package.html.problem.descriptor=Package ''{0}'' is missing a package.html file \ No newline at end of file +missing.package.html.problem.descriptor=Package ''{0}'' is missing a package.html 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 \ No newline at end of file diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/javadoc/PackageInfoWithoutPackageInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/javadoc/PackageInfoWithoutPackageInspection.java new file mode 100644 index 000000000000..e3949ef94af7 --- /dev/null +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/javadoc/PackageInfoWithoutPackageInspection.java @@ -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); + } + } +} diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/PackageInfoWithoutPackage.html b/plugins/InspectionGadgets/src/inspectionDescriptions/PackageInfoWithoutPackage.html new file mode 100644 index 000000000000..6290624778ee --- /dev/null +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/PackageInfoWithoutPackage.html @@ -0,0 +1,9 @@ + + +Reports package-info.java files without a package statement. +The Javadoc tool considers such files documentation for the default package even when the file is located somewhere else. + +

+New in 13 + + \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/package_info_without_package/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/package_info_without_package/expected.xml new file mode 100644 index 000000000000..030404f0fdac --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/package_info_without_package/expected.xml @@ -0,0 +1,9 @@ + + + + package-info.java + 1 + 'package-info.java' without 'package' statement + 'package-info.java' does not have a package statement + + \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/package_info_without_package/src/one/two/package-info.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/package_info_without_package/src/one/two/package-info.java new file mode 100644 index 000000000000..1a60ed728e5e --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/package_info_without_package/src/one/two/package-info.java @@ -0,0 +1 @@ +package one.two; \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/package_info_without_package/src/one/two/three/package-info.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/package_info_without_package/src/one/two/three/package-info.java new file mode 100644 index 000000000000..2af8f700b6a9 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/package_info_without_package/src/one/two/three/package-info.java @@ -0,0 +1,3 @@ +/** + * Some explanation for the existence of this package + */ \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/javadoc/PackageInfoWithoutPackageInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/javadoc/PackageInfoWithoutPackageInspectionTest.java new file mode 100644 index 000000000000..ed3862556c4a --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/javadoc/PackageInfoWithoutPackageInspectionTest.java @@ -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()); + } +}