forked from checkstyle/checkstyle
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c3e98e5
commit 7a3a5e7
Showing
8 changed files
with
377 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
src/main/java/com/puppycrawl/tools/checkstyle/checks/design/OneTopLevelClassCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2014 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.design; | ||
|
||
import java.util.Iterator; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
|
||
import com.puppycrawl.tools.checkstyle.api.Check; | ||
import com.puppycrawl.tools.checkstyle.api.DetailAST; | ||
import com.puppycrawl.tools.checkstyle.api.TokenTypes; | ||
|
||
/** | ||
* | ||
* Checks that each top-level class, interface | ||
* or enum resides in a source file of its own. | ||
* <p> | ||
* Official description of a 'top-level' term:<a | ||
* href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.6"> | ||
* 7.6. Top Level Type Declarations</a>. | ||
* </p> | ||
* <p> | ||
* An example of check's configuration: | ||
* </p> | ||
* <pre> | ||
* <module name="OneTopLevelClass"/> | ||
* </pre> | ||
* <p> | ||
* An example of check's configuration applied only to classes: | ||
* </p> | ||
* <pre> | ||
* <module name="OneTopLevelClass"> | ||
* <property name="tokens" value="CLASS_DEF"> | ||
* </module> | ||
* </pre> | ||
* <p> | ||
* An example of code with violations: | ||
* </p> | ||
* <pre><code> | ||
* public class Foo{ | ||
* //methods | ||
* } | ||
* | ||
* class Foo2{ | ||
* //methods | ||
* } | ||
* </code></pre> | ||
* | ||
* <p> | ||
* An example of code without violations: | ||
* </p> | ||
* <pre><code> | ||
* public class Foo{ | ||
* //methods | ||
* } | ||
* </code></pre> | ||
* | ||
* @author Max Vetrenko | ||
*/ | ||
public class OneTopLevelClassCheck extends Check | ||
{ | ||
/** True, if java file contains top-level public type*/ | ||
private boolean mTopLevelPublicClass; | ||
/** Contains top level types and line numbers of there declaration*/ | ||
private TreeMap<Integer, String> mClassCollector = | ||
new TreeMap<Integer, String>(); | ||
|
||
@Override | ||
public int[] getDefaultTokens() | ||
{ | ||
return new int[] {}; | ||
} | ||
@Override | ||
public void beginTree(DetailAST aRootAST) | ||
{ | ||
DetailAST curNode = aRootAST; | ||
while (curNode != null) { | ||
if (curNode.getType() == TokenTypes.CLASS_DEF | ||
| curNode.getType() == TokenTypes.ENUM_DEF | ||
| curNode.getType() == TokenTypes.INTERFACE_DEF) | ||
{ | ||
if (isPublic(curNode.findFirstToken(TokenTypes.MODIFIERS))) { | ||
mTopLevelPublicClass = true; | ||
} | ||
|
||
else { | ||
final String typeName = | ||
curNode.findFirstToken(TokenTypes.IDENT).getText(); | ||
mClassCollector.put(curNode.getLineNo(), typeName); | ||
} | ||
} | ||
curNode = curNode.getNextSibling(); | ||
} | ||
} | ||
|
||
@Override | ||
public void finishTree(DetailAST aRootAST) | ||
{ | ||
if (mTopLevelPublicClass) { | ||
for (Map.Entry<Integer, String> entry | ||
: mClassCollector.entrySet()) | ||
{ | ||
log(entry.getKey(), "one.top.level.class", entry.getValue()); | ||
} | ||
} | ||
|
||
else { | ||
boolean skipFirstType = false; //to skip first top-level type | ||
Iterator<Map.Entry<Integer, String>> iterator = mClassCollector.entrySet().iterator(); | ||
while(iterator.hasNext()) | ||
{ | ||
Map.Entry<Integer, String> entry = iterator.next(); | ||
if (skipFirstType) { | ||
log(entry.getKey(), | ||
"one.top.level.class", entry.getValue()); | ||
} else { | ||
iterator.remove(); | ||
skipFirstType = true; | ||
} | ||
} | ||
} | ||
mClassCollector.clear(); | ||
mTopLevelPublicClass = false; | ||
} | ||
|
||
/** | ||
* Check if type has public level access. | ||
* @param aModifireNode modifiers of type node. | ||
* @return ture, if type has public level access. | ||
*/ | ||
private boolean isPublic(DetailAST aModifireNode) | ||
{ | ||
return aModifireNode.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/test/java/com/puppycrawl/tools/checkstyle/checks/design/OneTopLevelClassCheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2014 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.design; | ||
|
||
import java.io.File; | ||
|
||
import org.junit.Test; | ||
|
||
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport; | ||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration; | ||
|
||
public class OneTopLevelClassCheckTest extends BaseCheckTestSupport | ||
{ | ||
|
||
@Test | ||
public void testGood() throws Exception | ||
{ | ||
final DefaultConfiguration checkConfig = | ||
createCheckConfig(OneTopLevelClassCheck.class); | ||
final String[] expected = {}; | ||
verify(checkConfig, getPath("design" + File.separator + "InputOneTopLevelClass.java"), expected); | ||
} | ||
@Test | ||
public void testBad() throws Exception | ||
{ | ||
final DefaultConfiguration checkConfig = | ||
createCheckConfig(OneTopLevelClassCheck.class); | ||
final String[] expected = { | ||
"25: Top-level class NoSuperClone has to reside in its own source file.", | ||
"33: Top-level class InnerClone has to reside in its own source file.", | ||
"50: Top-level class CloneWithTypeArguments has to reside in its own source file.", | ||
"58: Top-level class CloneWithTypeArgumentsAndNoSuper has to reside in its own source file.", | ||
"67: Top-level class MyClassWithGenericSuperMethod has to reside in its own source file.", | ||
"84: Top-level class AnotherClass has to reside in its own source file.", | ||
}; | ||
verify(checkConfig, getPath("coding" + File.separator + "InputClone.java"), expected); | ||
} | ||
@Test | ||
public void testBad_enumClass() throws Exception | ||
{ | ||
final DefaultConfiguration checkConfig = | ||
createCheckConfig(OneTopLevelClassCheck.class); | ||
final String[] expected = { | ||
"83: Top-level class InputDeclarationOrderEnum has to reside in its own source file.", | ||
}; | ||
verify(checkConfig, getPath("coding" + File.separator + "InputDeclarationOrder.java"), expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,4 @@ public void methodWithLiterals() | |
final String ref = "<a href=\""; | ||
final String refCase = "<A hReF=\""; | ||
} | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/test/resources/com/puppycrawl/tools/checkstyle/design/InputOneTopLevelClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package net.ubiquity.tools.checkstyle.tests; | ||
|
||
public class InputOneTopLevelClass | ||
{ | ||
static final int FOO2 = 3; | ||
|
||
// error public before package | ||
public static final int FOO = 3; | ||
|
||
private static final int FOO3 = 3; | ||
|
||
// eror public before package and private | ||
public static final int FOO4 = 3; | ||
|
||
private static final String ERROR = "error"; | ||
|
||
// error protected before private | ||
protected static final String ERROR1 = "error"; | ||
|
||
// error public before private | ||
public static final String WARNING = "warning"; | ||
|
||
private int mMaxInitVars = 3; | ||
|
||
// error statics should be before instance members | ||
// error publics before private | ||
public static final int MAX_ITER_VARS = 3; | ||
|
||
private class InnerClass | ||
{ | ||
private static final int INNER_FOO = 2; | ||
|
||
// error public before private | ||
public static final int INNER_FOO2 = 2; | ||
|
||
public InnerClass() | ||
{ | ||
int foo = INNER_FOO; | ||
foo += INNER_FOO2; | ||
foo += INNER_FOO3; | ||
} | ||
|
||
// error member variables should be before methods or ctors | ||
// error public before private | ||
public static final int INNER_FOO3 = 2; | ||
} | ||
|
||
public int getFoo1() | ||
{ | ||
return mFoo; | ||
} | ||
|
||
// error ctors before methods | ||
public InputDeclarationOrder() | ||
{ | ||
String foo = ERROR; | ||
foo += ERROR1; | ||
foo += WARNING; | ||
int fooInt = mMaxInitVars; | ||
fooInt += MAX_ITER_VARS; | ||
fooInt += mFoo; | ||
} | ||
|
||
public static int getFoo2() | ||
{ | ||
return 13; | ||
} | ||
|
||
public int getFoo() | ||
{ | ||
return mFoo; | ||
} | ||
|
||
private static int getFoo21() | ||
{ | ||
return 14; | ||
} | ||
|
||
// error member variables should be before methods or ctors | ||
private int mFoo = 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.