From 96abec9ddf3ce448d75c32e7826eddb3c857408b Mon Sep 17 00:00:00 2001 From: Oliver Burn Date: Tue, 29 Oct 2002 13:30:30 +0000 Subject: [PATCH] Patch from Rick Giles - also removed the dead code --- .../tools/checkstyle/Configuration.java | 13 ----- .../com/puppycrawl/tools/checkstyle/Defn.java | 3 - .../puppycrawl/tools/checkstyle/Verifier.java | 6 +- .../checkstyle/checks/MemberNameCheck.java | 58 +++++++++++++++++++ .../tools/checkstyle/CheckerTest.java | 3 - .../tools/checkstyle/ConfigurationTest.java | 10 ---- .../tools/checkstyle/MemberNameCheckTest.java | 27 +++++++++ 7 files changed, 88 insertions(+), 32 deletions(-) create mode 100644 src/checkstyle/com/puppycrawl/tools/checkstyle/checks/MemberNameCheck.java create mode 100644 src/tests/com/puppycrawl/tools/checkstyle/MemberNameCheckTest.java diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java index d049cd3f9..249ed940d 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java @@ -55,7 +55,6 @@ public class Configuration PATTERN_DEFAULTS.put(Defn.TODO_PATTERN_PROP, "TODO:"); PATTERN_DEFAULTS.put(Defn.STATIC_PATTERN_PROP, "^[a-z][a-zA-Z0-9]*$"); PATTERN_DEFAULTS.put(Defn.CONST_PATTERN_PROP, "^[A-Z](_?[A-Z0-9]+)*$"); - PATTERN_DEFAULTS.put(Defn.MEMBER_PATTERN_PROP, "^[a-z][a-zA-Z0-9]*$"); PATTERN_DEFAULTS.put(Defn.PUBLIC_MEMBER_PATTERN_PROP, "^f[A-Z][a-zA-Z0-9]*$"); } @@ -323,18 +322,6 @@ public class Configuration return getRegexpProperty(Defn.CONST_PATTERN_PROP); } - /** @return pattern to match member variables **/ - String getMemberPat() - { - return getPatternProperty(Defn.MEMBER_PATTERN_PROP); - } - - /** @return regexp to match member variables **/ - RE getMemberRegexp() - { - return getRegexpProperty(Defn.MEMBER_PATTERN_PROP); - } - /** @return pattern to match public member variables **/ String getPublicMemberPat() { diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Defn.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Defn.java index c83ad9a10..3f2bad3f0 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Defn.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Defn.java @@ -35,8 +35,6 @@ public interface Defn String STATIC_PATTERN_PROP = "checkstyle.pattern.static"; /** property name for the static final variable pattern **/ String CONST_PATTERN_PROP = "checkstyle.pattern.const"; - /** property name for the member variable pattern **/ - String MEMBER_PATTERN_PROP = "checkstyle.pattern.member"; /** property name for the public member variable pattern **/ String PUBLIC_MEMBER_PATTERN_PROP = "checkstyle.pattern.publicmember"; /** property name for length of constructors **/ @@ -97,7 +95,6 @@ public interface Defn TODO_PATTERN_PROP, STATIC_PATTERN_PROP, CONST_PATTERN_PROP, - MEMBER_PATTERN_PROP, PUBLIC_MEMBER_PATTERN_PROP, }; diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java index f00768149..e205d50a6 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java @@ -302,9 +302,9 @@ class Verifier || (mConfig.isAllowPackage() && isPckg) || (mConfig.isAllowProtected() && isProt)) { - checkVariable(aVar, - mConfig.getMemberRegexp(), - mConfig.getMemberPat()); +// checkVariable(aVar, +// mConfig.getMemberRegexp(), +// mConfig.getMemberPat()); } } } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/MemberNameCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/MemberNameCheck.java new file mode 100644 index 000000000..14b6ae05a --- /dev/null +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/MemberNameCheck.java @@ -0,0 +1,58 @@ +//////////////////////////////////////////////////////////////////////////////// +// 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 instance variable names conform to a specified format. + * + * @author Rick Giles + * @version 1.0 + */ +public class MemberNameCheck + extends AbstractNameCheck +{ + /** Creates a new MemberNameCheck instance. */ + public MemberNameCheck() + { + super("^[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) + && !ScopeUtils.inCodeBlock(aAST)); + } +} diff --git a/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java b/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java index d61b3a2ac..5b0134f3e 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java @@ -295,15 +295,12 @@ public class CheckerTest { mProps.setProperty(Defn.MAX_CONSTRUCTOR_LENGTH_PROP, "9"); mProps.setProperty(Defn.STATIC_PATTERN_PROP, "^s[A-Z][a-zA-Z0-9]*$"); - mProps.setProperty(Defn.MEMBER_PATTERN_PROP, "^m[A-Z][a-zA-Z0-9]*$"); mProps.setProperty(Defn.TODO_PATTERN_PROP, "FIXME:"); - mProps.setProperty(Defn.MEMBER_PATTERN_PROP, "^m[A-Z][a-zA-Z0-9]*$"); final Checker c = createChecker(); final String filepath = getPath("InputSimple.java"); assertNotNull(c); final String[] expected = { filepath + ":30:24: Name 'badStatic' must match pattern '^s[A-Z][a-zA-Z0-9]*$'.", - filepath + ":35:17: Name 'badMember' must match pattern '^m[A-Z][a-zA-Z0-9]*$'.", filepath + ":42:40: ',' is not followed by whitespace.", filepath + ":71:30: ',' is not followed by whitespace.", filepath + ":103: Constructor length is 10 lines (max allowed is 9).", diff --git a/src/tests/com/puppycrawl/tools/checkstyle/ConfigurationTest.java b/src/tests/com/puppycrawl/tools/checkstyle/ConfigurationTest.java index 2271ec4ed..34fb00a41 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/ConfigurationTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/ConfigurationTest.java @@ -28,14 +28,4 @@ public class ConfigurationTest final Configuration c = new Configuration(p, System.out); assertNotNull(c); } - - public void testGetProperties() throws Exception - { - final Properties props = new Properties(); - props.setProperty(Defn.MEMBER_PATTERN_PROP, "bulldogs"); - - final Configuration c = new Configuration(props, System.out); - assertEquals("bulldogs", - c.getProperties().getProperty(Defn.MEMBER_PATTERN_PROP)); - } } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/MemberNameCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/MemberNameCheckTest.java new file mode 100644 index 000000000..5f8ec7e15 --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/MemberNameCheckTest.java @@ -0,0 +1,27 @@ +package com.puppycrawl.tools.checkstyle; + +import com.puppycrawl.tools.checkstyle.checks.MemberNameCheck; + +public class MemberNameCheckTest + extends BaseCheckTestCase +{ + public MemberNameCheckTest(String aName) + { + super(aName); + } + + public void testSpecified() + throws Exception + { + final CheckConfiguration checkConfig = new CheckConfiguration(); + checkConfig.setClassname(MemberNameCheck.class.getName()); + checkConfig.addProperty("format", "^m[A-Z][a-zA-Z0-9]*$"); + final Checker c = createChecker(checkConfig); + final String fname = getPath("InputSimple.java"); + final String[] expected = { + "35:17: Name 'badMember' must match pattern '^m[A-Z][a-zA-Z0-9]*$'.", + }; + verify(c, fname, expected); + } +} +