diff --git a/docs/config_coding.html b/docs/config_coding.html index 1a600174f..ff5cce2fa 100644 --- a/docs/config_coding.html +++ b/docs/config_coding.html @@ -70,6 +70,9 @@
  • NestedTryDepth
  • +
  • + PackageDeclaration +
  • RedundantThrows
  • @@ -1015,7 +1018,6 @@ return !valid(); TreeWalker

    -

    IllegalCatch

    Description

    Catching java.lang.Exception, java.lang.Error or java.lang.RuntimeException @@ -1060,6 +1062,32 @@ return !valid(); TreeWalker

    +

    PackageDeclaration

    +

    Description

    +

    + Ensure a class is has a package declaration. +

    +

    + Rationale: Classes that live in the null package cannot be + imported. Many novice developers are not aware of this. +

    + +

    Examples

    +

    + To configure the check: +

    +
    +<module name="PackageDeclaration"/>
    +      
    +

    Package

    +

    + com.puppycrawl.tools.checkstyle.checks.coding +

    +

    Parent Module

    +

    + TreeWalker +

    + diff --git a/docs/releasenotes.html b/docs/releasenotes.html index 209d98c8d..445a1aa76 100644 --- a/docs/releasenotes.html +++ b/docs/releasenotes.html @@ -143,6 +143,8 @@
  • Added IllegalCatch check. (request 750746).
  • +
  • Added PackageDeclaration check. (request 750753).
  • +

    diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/PackageDeclarationCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/PackageDeclarationCheck.java new file mode 100644 index 000000000..b7032b697 --- /dev/null +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/PackageDeclarationCheck.java @@ -0,0 +1,68 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2003 Oliver Burn +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// +package com.puppycrawl.tools.checkstyle.checks.coding; + +import com.puppycrawl.tools.checkstyle.api.Check; +import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.TokenTypes; + +/** + * Ensures there is a package declaration. + * Rationale: Classes that live in the null package cannot be + * imported. Many novice developers are not aware of this. + * + * @author Simon Harris + */ +public final class PackageDeclarationCheck extends Check +{ + /** is package defined. */ + private boolean mDefined; + + /** @see Check */ + public int[] getDefaultTokens() + { + return new int[] {TokenTypes.PACKAGE_DEF}; + } + + /** @see Check */ + public int[] getRequiredTokens() + { + return getDefaultTokens(); + } + + /** @see Check */ + public void beginTree(DetailAST aAST) + { + mDefined = false; + } + + /** @see Check */ + public void finishTree(DetailAST aAST) + { + if (!mDefined) { + log(aAST.getLineNo(), "missing.package.declaration"); + } + } + + /** @see Check */ + public void visitToken(DetailAST aAST) + { + mDefined = true; + } +} diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/messages.properties b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/messages.properties index 47792a74c..6a4fa5f1a 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/messages.properties +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/messages.properties @@ -37,3 +37,4 @@ nested.try.depth=Nested try depth is {0,number,integer} (max allowed is {1,numbe string.literal.equality=Literal Strings should be compared using equals(), not ''{0}''. illegal.catch=Catching ''{0}'' is not allowed. +missing.package.declaration=Missing package declaration. diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/coding/InputNoPackage.java b/src/testinputs/com/puppycrawl/tools/checkstyle/coding/InputNoPackage.java new file mode 100644 index 000000000..b791af5c7 --- /dev/null +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/coding/InputNoPackage.java @@ -0,0 +1,5 @@ +/** + * No package here. + */ +public class InputNoPackage { +} diff --git a/src/tests/com/puppycrawl/tools/checkstyle/AllTests.java b/src/tests/com/puppycrawl/tools/checkstyle/AllTests.java index 3aa21ec0d..f7c71783b 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/AllTests.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/AllTests.java @@ -36,6 +36,7 @@ import com.puppycrawl.tools.checkstyle.checks.coding.MagicNumberCheckTest; import com.puppycrawl.tools.checkstyle.checks.coding.MissingSwitchDefaultCheckTest; import com.puppycrawl.tools.checkstyle.checks.coding.NestedIfDepthCheckTest; import com.puppycrawl.tools.checkstyle.checks.coding.NestedTryDepthCheckTest; +import com.puppycrawl.tools.checkstyle.checks.coding.PackageDeclarationCheckTest; import com.puppycrawl.tools.checkstyle.checks.coding.RedundantThrowsCheckTest; import com.puppycrawl.tools.checkstyle.checks.coding.SimplifyBooleanExpressionCheckTest; import com.puppycrawl.tools.checkstyle.checks.coding.SimplifyBooleanReturnCheckTest; @@ -169,6 +170,7 @@ public class AllTests { suite.addTest(new TestSuite(NoWhitespaceBeforeCheckTest.class)); suite.addTest(new TestSuite(OperatorWrapCheckTest.class)); suite.addTest(new TestSuite(OptionTest.class)); + suite.addTest(new TestSuite(PackageDeclarationCheckTest.class)); suite.addTest(new TestSuite(PackageHtmlCheckTest.class)); suite.addTest(new TestSuite(PackageNameCheckTest.class)); suite.addTest(new TestSuite(PackageNamesLoaderTest.class)); diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/coding/PackageDeclarationCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/coding/PackageDeclarationCheckTest.java new file mode 100644 index 000000000..eec190c73 --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/coding/PackageDeclarationCheckTest.java @@ -0,0 +1,30 @@ +package com.puppycrawl.tools.checkstyle.checks.coding; + +import com.puppycrawl.tools.checkstyle.BaseCheckTestCase; +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; + +import java.io.File; + +public class PackageDeclarationCheckTest extends BaseCheckTestCase +{ + public void testDefault() throws Exception + { + DefaultConfiguration checkConfig = createCheckConfig(PackageDeclarationCheck.class); + + String[] expected = { + "4: Missing package declaration.", + }; + + verify(checkConfig, getPath("coding" + File.separator + "InputNoPackage.java"), expected); + } + + public void testDefault1() throws Exception + { + DefaultConfiguration checkConfig = createCheckConfig(PackageDeclarationCheck.class); + + String[] expected = { + }; + + verify(checkConfig, getPath("coding" + File.separator + "InputIllegalCatchCheck.java"), expected); + } +}