added IllegalTokenCheck, request 750755
This commit is contained in:
parent
983111eb06
commit
b07f845ac1
|
|
@ -46,6 +46,9 @@
|
|||
<li>
|
||||
<a href="#IllegalInstantiation">IllegalInstantiation</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="#IllegalToken">IllegalToken</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="#InnerAssignment">InnerAssignment</a>
|
||||
</li>
|
||||
|
|
@ -399,6 +402,57 @@ public class MySingleton
|
|||
<p class="body">
|
||||
<a href="config.html#treewalker">TreeWalker</a>
|
||||
</p>
|
||||
|
||||
<!-- --> <a name="IllegalToken"></a> <h2>IllegalToken</h2> <h4>Description</h4>
|
||||
<p class="body">
|
||||
Checks for illegal tokens.
|
||||
</p>
|
||||
<p class="body">
|
||||
Rational: Certain language features are often lead to hard to
|
||||
maintain code or are non-obvious to novice developers. Others
|
||||
may be discouraged in certain frameworks, such as not having
|
||||
native methods in EJB components.
|
||||
</p>
|
||||
<table width="100%" border="1" cellpadding="5" class="body">
|
||||
<tr class="header">
|
||||
<th>name</th>
|
||||
<th>description</th>
|
||||
<th>type</th>
|
||||
<th>default value</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>tokens</td>
|
||||
<td>tokens to check</td>
|
||||
<td>subset of <a
|
||||
href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html">TokenTypes</a>,
|
||||
<a</td>
|
||||
<td><a
|
||||
href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_SWITCH">LITERAL_SWITCH</a>,
|
||||
<a
|
||||
href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#POST_INC">POST_INC</a>,
|
||||
<a
|
||||
href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#POST_DEC">POST_DEC</a></td>
|
||||
</tr>
|
||||
</table>
|
||||
<h4>Example</h4>
|
||||
<p class="body">
|
||||
To configure the check to find token LITERAL_NATIVE:
|
||||
</p>
|
||||
<pre class="body">
|
||||
<module name="IllegalToken">
|
||||
<property name="tokens" value="LITERAL_NATIVE"/>
|
||||
</module>
|
||||
</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>
|
||||
|
||||
<!-- --> <a name="InnerAssignment"></a> <h2>InnerAssignment</h2> <h4>Description</h4>
|
||||
<p class="body">
|
||||
Checks for assignments in subexpressions, such as in <span class="code">String s
|
||||
|
|
|
|||
|
|
@ -101,6 +101,9 @@
|
|||
NestedIfDepthCheck from Simon Harris (requests 750736 and
|
||||
750744).</li>
|
||||
|
||||
<li class="body">Added IllegalTokenCheck from Simon Harris
|
||||
(request 750755).</li>
|
||||
|
||||
</ul>
|
||||
|
||||
<p class="body">
|
||||
|
|
|
|||
|
|
@ -0,0 +1,104 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
// 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 java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.api.Check;
|
||||
import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
||||
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Checks for illegal tokens.
|
||||
* </p>
|
||||
* <p>
|
||||
* Rational: Certain language features are often lead to hard to
|
||||
* maintain code or are non-obvious to novice developers. Others
|
||||
* may be discouraged in certain frameworks, such as not having
|
||||
* native methods in EJB components.
|
||||
* </p>
|
||||
* <p>
|
||||
* An example of how to configure the check is:
|
||||
* </p>
|
||||
* <pre>
|
||||
* <module name="IllegalToken"/>
|
||||
* </pre>
|
||||
* * <p> An example of how to configure the check to forbid
|
||||
* a {@link TokenTypes#LITERAL_NATIVE LITERAL_NATIVE} token is:
|
||||
* <pre>
|
||||
* <module name="IllegalToken">
|
||||
* <property name="tokens" value="LITERAL_NATIVE"/>
|
||||
* </module>
|
||||
* </pre>
|
||||
* @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
|
||||
* @author Rick Giles
|
||||
*/
|
||||
public class IllegalTokenCheck
|
||||
extends Check
|
||||
{
|
||||
/**
|
||||
* @see com.puppycrawl.tools.checkstyle.api.Check
|
||||
*/
|
||||
public int[] getDefaultTokens()
|
||||
{
|
||||
return new int[] {
|
||||
TokenTypes.LITERAL_SWITCH,
|
||||
TokenTypes.POST_INC,
|
||||
TokenTypes.POST_DEC,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @see com.puppycrawl.tools.checkstyle.api.Check
|
||||
*/
|
||||
public int[] getAcceptableTokens()
|
||||
{
|
||||
// Any tokens set by property 'tokens' are acceptable
|
||||
int[] tokensToCopy = getDefaultTokens();
|
||||
final Set tokenNames = getTokenNames();
|
||||
if (!tokenNames.isEmpty()) {
|
||||
tokensToCopy = new int[tokenNames.size()];
|
||||
int i = 0;
|
||||
final Iterator it = tokenNames.iterator();
|
||||
while (it.hasNext()) {
|
||||
final String name = (String) it.next();
|
||||
tokensToCopy[i] = TokenTypes.getTokenId(name);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
final int[] copy = new int[tokensToCopy.length];
|
||||
System.arraycopy(tokensToCopy, 0, copy, 0, tokensToCopy.length);
|
||||
return copy;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see com.puppycrawl.tools.checkstyle.api.Check
|
||||
*/
|
||||
public void visitToken(DetailAST aAST)
|
||||
{
|
||||
log(
|
||||
aAST.getLineNo(),
|
||||
aAST.getColumnNo(),
|
||||
"illegal.token",
|
||||
aAST.getText());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -12,6 +12,8 @@ equals.noHashCode=Definition of ''equals()'' without corresponding definition of
|
|||
|
||||
hidden.field=''{0}'' hides a field.
|
||||
|
||||
illegal.token=Using ''{0}'' is not allowed.
|
||||
|
||||
instantiation.avoid=Instantiation of {0} should be avoided.
|
||||
|
||||
magic.number=''{0}'' is a magic number.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,21 @@
|
|||
package com.puppycrawl.tools.checkstyle;
|
||||
|
||||
/**
|
||||
* Test for illegal tokens
|
||||
*/
|
||||
public class InputIllegalTokens
|
||||
{
|
||||
public void defaultMethod()
|
||||
{
|
||||
int i = 0;
|
||||
switch (i)
|
||||
{
|
||||
default:
|
||||
i--;
|
||||
i++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public native void nativeMethod();
|
||||
}
|
||||
|
|
@ -27,6 +27,7 @@ import com.puppycrawl.tools.checkstyle.checks.coding.EmptyStatementCheckTest;
|
|||
import com.puppycrawl.tools.checkstyle.checks.coding.EqualsHashCodeCheckTest;
|
||||
import com.puppycrawl.tools.checkstyle.checks.coding.HiddenFieldCheckTest;
|
||||
import com.puppycrawl.tools.checkstyle.checks.coding.IllegalInstantiationCheckTest;
|
||||
import com.puppycrawl.tools.checkstyle.checks.coding.IllegalTokenCheckTest;
|
||||
import com.puppycrawl.tools.checkstyle.checks.coding.InnerAssignmentCheckTest;
|
||||
import com.puppycrawl.tools.checkstyle.checks.coding.MagicNumberCheckTest;
|
||||
import com.puppycrawl.tools.checkstyle.checks.coding.MissingSwitchDefaultCheckTest;
|
||||
|
|
@ -119,6 +120,7 @@ public class AllTests {
|
|||
suite.addTest(new TestSuite(HideUtilityClassConstructorCheckTest.class));
|
||||
suite.addTest(new TestSuite(IllegalImportCheckTest.class));
|
||||
suite.addTest(new TestSuite(IllegalInstantiationCheckTest.class));
|
||||
suite.addTest(new TestSuite(IllegalTokenCheckTest.class));
|
||||
suite.addTest(new TestSuite(IndentationCheckTest.class));
|
||||
suite.addTest(new TestSuite(InnerAssignmentCheckTest.class));
|
||||
suite.addTest(new TestSuite(InterfaceIsTypeCheckTest.class));
|
||||
|
|
|
|||
|
|
@ -0,0 +1,35 @@
|
|||
package com.puppycrawl.tools.checkstyle.checks.coding;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.BaseCheckTestCase;
|
||||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
|
||||
|
||||
public class IllegalTokenCheckTest
|
||||
extends BaseCheckTestCase
|
||||
{
|
||||
public void testDefault()
|
||||
throws Exception
|
||||
{
|
||||
final DefaultConfiguration checkConfig =
|
||||
createCheckConfig(IllegalTokenCheck.class);
|
||||
final String[] expected = {
|
||||
"11:9: Using 'switch' is not allowed.",
|
||||
"14:18: Using '--' is not allowed.",
|
||||
"15:18: Using '++' is not allowed.",
|
||||
};
|
||||
verify(checkConfig, getPath("InputIllegalTokens.java"), expected);
|
||||
}
|
||||
|
||||
public void testNative()
|
||||
throws Exception
|
||||
{
|
||||
final DefaultConfiguration checkConfig =
|
||||
createCheckConfig(IllegalTokenCheck.class);
|
||||
checkConfig.addAttribute("tokens", "LITERAL_NATIVE");
|
||||
final String[] expected = {
|
||||
"20:12: Using 'native' is not allowed.",
|
||||
};
|
||||
verify(checkConfig, getPath("InputIllegalTokens.java"), expected);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue