From e44cd4ec43a25cac72924e04d7b4ca7fa9fa10ee Mon Sep 17 00:00:00 2001 From: Oliver Burn Date: Sun, 27 Oct 2002 21:58:10 +0000 Subject: [PATCH] Added Constant Name Check --- .../checkstyle/checks/ConstantNameCheck.java | 84 +++++++++++++++++++ .../checkstyle/ConstantNameCheckTest.java | 26 ++++++ 2 files changed, 110 insertions(+) create mode 100644 src/checkstyle/com/puppycrawl/tools/checkstyle/checks/ConstantNameCheck.java create mode 100644 src/tests/com/puppycrawl/tools/checkstyle/ConstantNameCheckTest.java diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/ConstantNameCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/ConstantNameCheck.java new file mode 100644 index 000000000..a2e623139 --- /dev/null +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/ConstantNameCheck.java @@ -0,0 +1,84 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2002 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; + +import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.TokenTypes; +import com.puppycrawl.tools.checkstyle.api.Utils; + +/** + * Checks that constant names conform to a specified format. + * + * @author Rick Giles + * @version 1.0 + */ +public class ConstantNameCheck + extends AbstractFormatCheck +{ + /** Creates a new ConstantNameCheck instance. */ + public ConstantNameCheck() + { + super("^[A-Z](_?[A-Z0-9]+)*$"); + } + + /** @see com.puppycrawl.tools.checkstyle.api.Check */ + public int[] getDefaultTokens() + { + return new int[] {TokenTypes.VARIABLE_DEF}; + } + + /** @see com.puppycrawl.tools.checkstyle.api.Check */ + public void visitToken(DetailAST aAST) + { + //precondition + if (aAST.getType() != TokenTypes.VARIABLE_DEF) { + return; + } + + //constant? + final DetailAST modifiers = + Utils.findFirstToken(aAST.getFirstChild(), + TokenTypes.MODIFIERS); + if (modifiers == null + || !modifiers.branchContains(TokenTypes.LITERAL_STATIC) + || !modifiers.branchContains(TokenTypes.FINAL)) { + return; + } + + //name check + final DetailAST name = Utils.findFirstToken(aAST.getFirstChild(), + TokenTypes.IDENT); + if (name == null) { + return; + } + // Handle the serialVersionUID constant which is used for + // Serialization. Cannot enforce rules on it. :-) + if ("serialVersionUID".equals(name.getText())) { + return; + } + + if (!getRegexp().match(name.getText())) { + log(name.getLineNo(), + name.getColumnNo(), + "name.invalidPattern", + name.getText(), + getFormat()); + } + } +} diff --git a/src/tests/com/puppycrawl/tools/checkstyle/ConstantNameCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/ConstantNameCheckTest.java new file mode 100644 index 000000000..f4a6b0967 --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/ConstantNameCheckTest.java @@ -0,0 +1,26 @@ +package com.puppycrawl.tools.checkstyle; + +import com.puppycrawl.tools.checkstyle.checks.ConstantNameCheck; + +public class ConstantNameCheckTest + extends BaseCheckTestCase +{ + public ConstantNameCheckTest(String aName) + { + super(aName); + } + + public void testDefault() + throws Exception + { + final CheckConfiguration checkConfig = new CheckConfiguration(); + checkConfig.setClassname(ConstantNameCheck.class.getName()); + final Checker c = createChecker(checkConfig); + final String fname = getPath("InputSimple.java"); + final String[] expected = { + "25:29: Name 'badConstant' must match pattern '^[A-Z](_?[A-Z0-9]+)*$'.", + "142:30: Name 'BAD__NAME' must match pattern '^[A-Z](_?[A-Z0-9]+)*$'." + }; + verify(c, fname, expected); + } +}