diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java index e205d50a6..298271ca8 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java @@ -296,17 +296,6 @@ class Verifier } } } - else { - // These are the non-static variables - if (Scope.PRIVATE.equals(variableScope) - || (mConfig.isAllowPackage() && isPckg) - || (mConfig.isAllowProtected() && isProt)) - { -// checkVariable(aVar, -// mConfig.getMemberRegexp(), -// mConfig.getMemberPat()); - } - } } } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/PublicMemberNameCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/PublicMemberNameCheck.java new file mode 100644 index 000000000..2fa014dfd --- /dev/null +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/PublicMemberNameCheck.java @@ -0,0 +1,57 @@ +//////////////////////////////////////////////////////////////////////////////// +// 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.ScopeUtils; +import com.puppycrawl.tools.checkstyle.api.TokenTypes; + +/** + * Checks that public, non-static variable names conform to a specified format. + * + * @author Rick Giles + * @version 1.0 + */ +public class PublicMemberNameCheck + extends AbstractNameCheck +{ + /** Creates a new PublicMemberNameCheck instance. */ + public PublicMemberNameCheck() + { + super("^f[A-Z][a-zA-Z0-9]*$"); + } + + /** @see com.puppycrawl.tools.checkstyle.api.Check */ + public int[] getDefaultTokens() + { + return new int[] {TokenTypes.VARIABLE_DEF}; + } + + /** @see com.puppycrawl.tools.checkstyle.check.AbstractNameCheck */ + protected final boolean mustCheckName(DetailAST aAST) + { + DetailAST modifiersAST = aAST.findFirstToken(TokenTypes.MODIFIERS); + final boolean isStatic = (modifiersAST != null) + && modifiersAST.branchContains(TokenTypes.LITERAL_STATIC); + final boolean isPublic = (modifiersAST != null) + && modifiersAST.branchContains(TokenTypes.LITERAL_PUBLIC); + + return (!isStatic && isPublic && !ScopeUtils.inInterfaceBlock(aAST)); + } +} diff --git a/src/tests/com/puppycrawl/tools/checkstyle/PublicMemberNameCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/PublicMemberNameCheckTest.java new file mode 100644 index 000000000..e33462f3f --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/PublicMemberNameCheckTest.java @@ -0,0 +1,26 @@ +package com.puppycrawl.tools.checkstyle; + +import com.puppycrawl.tools.checkstyle.checks.PublicMemberNameCheck; + +public class PublicMemberNameCheckTest + extends BaseCheckTestCase +{ + public PublicMemberNameCheckTest(String aName) + { + super(aName); + } + + public void testDefault() + throws Exception + { + final CheckConfiguration checkConfig = new CheckConfiguration(); + checkConfig.setClassname(PublicMemberNameCheck.class.getName()); + final Checker c = createChecker(checkConfig); + final String fname = getPath("InputSimple.java"); + final String[] expected = { + "58:16: Name 'mTest2' must match pattern '^f[A-Z][a-zA-Z0-9]*$'.", + }; + verify(c, fname, expected); + } +} +