Source code: com/puppycrawl/tools/checkstyle/checks/AbstractNameCheck.java
1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2002 Oliver Burn
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.checks;
21
22 import com.puppycrawl.tools.checkstyle.api.DetailAST;
23 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
24
25 /**
26 * Abstract class for checking that names conform to a specified format.
27 *
28 * @author Rick Giles
29 * @version 1.0
30 */
31 public abstract class AbstractNameCheck
32 extends AbstractFormatCheck
33 {
34 /**
35 * Creates a new <code>AbstractNameCheck</code> instance.
36 * @param aFormat format to check with
37 */
38 public AbstractNameCheck(String aFormat)
39 {
40 super(aFormat);
41 }
42
43 /**
44 * Decides whether the name of an AST should be checked against
45 * the format regexp.
46 * @param aAST the AST to check.
47 * @return true if the IDENT subnode of aAST should be checked against
48 * the format regexp.
49 */
50 protected boolean mustCheckName(DetailAST aAST)
51 {
52 return true;
53 }
54
55 /** @see com.puppycrawl.tools.checkstyle.api.Check */
56 public void visitToken(DetailAST aAST)
57 {
58 if (mustCheckName(aAST)) {
59 final DetailAST nameAST = aAST.findFirstToken(TokenTypes.IDENT);
60 if (!getRegexp().match(nameAST.getText())) {
61 log(nameAST.getLineNo(),
62 nameAST.getColumnNo(),
63 "name.invalidPattern",
64 nameAST.getText(),
65 getFormat());
66 }
67 }
68 }
69 }