diff --git a/docs/config_coding.html b/docs/config_coding.html index 81c48a80a..c10617297 100644 --- a/docs/config_coding.html +++ b/docs/config_coding.html @@ -46,6 +46,9 @@
  • IllegalInstantiation
  • +
  • + IllegalToken +
  • InnerAssignment
  • @@ -399,6 +402,57 @@ public class MySingleton

    TreeWalker

    + +

    IllegalToken

    Description

    +

    + Checks for illegal tokens. +

    +

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

    + + + + + + + + + + + + +
    namedescriptiontypedefault value
    tokenstokens to checksubset of TokenTypes, + + LITERAL_SWITCH, + POST_INC, + POST_DEC
    +

    Example

    +

    + To configure the check to find token LITERAL_NATIVE: +

    +
    +<module name="IllegalToken">
    +    <property name="tokens" value="LITERAL_NATIVE"/>
    +</module>
    +      
    + +

    Package

    +

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

    +

    Parent Module

    +

    + TreeWalker +

    +

    InnerAssignment

    Description

    Checks for assignments in subexpressions, such as in String s diff --git a/docs/releasenotes.html b/docs/releasenotes.html index 31bb2cc37..bc3ec3820 100644 --- a/docs/releasenotes.html +++ b/docs/releasenotes.html @@ -101,6 +101,9 @@ NestedIfDepthCheck from Simon Harris (requests 750736 and 750744). +

  • Added IllegalTokenCheck from Simon Harris + (request 750755).
  • +

    diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/IllegalTokenCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/IllegalTokenCheck.java new file mode 100644 index 000000000..63031885f --- /dev/null +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/IllegalTokenCheck.java @@ -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; + +/** + *

    + * Checks for illegal tokens. + *

    + *

    + * 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. + *

    + *

    + * An example of how to configure the check is: + *

    + *
    + * <module name="IllegalToken"/>
    + * 
    + * *

    An example of how to configure the check to forbid + * a {@link TokenTypes#LITERAL_NATIVE LITERAL_NATIVE} token is: + *

    + * <module name="IllegalToken">
    + *     <property name="tokens" value="LITERAL_NATIVE"/>
    + * </module>
    + * 
    + * @author Simon Harris + * @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()); + } + +} 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 ce3562956..675af19b5 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/messages.properties +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/messages.properties @@ -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. diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/InputIllegalTokens.java b/src/testinputs/com/puppycrawl/tools/checkstyle/InputIllegalTokens.java new file mode 100644 index 000000000..063f731fe --- /dev/null +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/InputIllegalTokens.java @@ -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(); +} diff --git a/src/tests/com/puppycrawl/tools/checkstyle/AllTests.java b/src/tests/com/puppycrawl/tools/checkstyle/AllTests.java index e6aeca72a..ed61a3bae 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/AllTests.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/AllTests.java @@ -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)); diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/coding/IllegalTokenCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/coding/IllegalTokenCheckTest.java new file mode 100644 index 000000000..3451b357f --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/coding/IllegalTokenCheckTest.java @@ -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); + } + +} +