Added PackageDeclaration check. (request 750753).

This commit is contained in:
Oleg Sukhodolsky 2003-08-22 15:24:27 +00:00
parent 3c53211c86
commit bdaf63e08c
7 changed files with 137 additions and 1 deletions

View File

@ -70,6 +70,9 @@
<li>
<a href="#NestedTryDepth">NestedTryDepth</a>
</li>
<li>
<a href="#PackageDeclaration">PackageDeclaration</a>
</li>
<li>
<a href="#RedundantThrows">RedundantThrows</a>
</li>
@ -1015,7 +1018,6 @@ return !valid();
<a href="config.html#treewalker">TreeWalker</a>
</p>
<!-- --> <a name="IllegalCatch"></a> <h2>IllegalCatch</h2> <h4>Description</h4>
<p class="body">
Catching java.lang.Exception, java.lang.Error or java.lang.RuntimeException
@ -1060,6 +1062,32 @@ return !valid();
<a href="config.html#treewalker">TreeWalker</a>
</p>
<!-- --> <a name="PackageDeclaration"></a> <h2>PackageDeclaration</h2>
<h4>Description</h4>
<p class="body">
Ensure a class is has a package declaration.
</p>
<p class="body">
Rationale: Classes that live in the null package cannot be
imported. Many novice developers are not aware of this.
</p>
<h4>Examples</h4>
<p class="body">
To configure the check:
</p>
<pre class="body">
&lt;module name=&quot;PackageDeclaration&quot;/&gt;
</pre>
<h4>Package</h4>
<p class="body">
com.puppycrawl.tools.checkstyle.checks.coding
</p>
<h4>Parent Module</h4>
<p class="body">
<a href="config.html#treewalker">TreeWalker</a>
</p>
</td>
</tr>
</table>

View File

@ -143,6 +143,8 @@
<li class="body">Added IllegalCatch check. (request 750746).</li>
<li class="body">Added PackageDeclaration check. (request 750753).</li>
</ul>
<p class="body">

View File

@ -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 <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
*/
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;
}
}

View File

@ -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.

View File

@ -0,0 +1,5 @@
/**
* No package here.
*/
public class InputNoPackage {
}

View File

@ -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));

View File

@ -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);
}
}