View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
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.naming;
21  
22  import java.util.regex.Pattern;
23  
24  import com.puppycrawl.tools.checkstyle.StatelessCheck;
25  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
26  import com.puppycrawl.tools.checkstyle.api.DetailAST;
27  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
28  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
29  
30  /**
31   * Abstract class for checking that names conform to a specified format.
32   *
33   */
34  @StatelessCheck
35  public abstract class AbstractNameCheck
36      extends AbstractCheck {
37  
38      /**
39       * Message key for invalid pattern violation.
40       */
41      public static final String MSG_INVALID_PATTERN = "name.invalidPattern";
42  
43      /** The regexp to match against. */
44      private Pattern format;
45  
46      /**
47       * Creates a new {@code AbstractNameCheck} instance.
48       *
49       * @param format format to check with
50       */
51      protected AbstractNameCheck(String format) {
52          this.format = CommonUtil.createPattern(format);
53      }
54  
55      /**
56       * Decides whether the name of an AST should be checked against
57       * the format regexp.
58       *
59       * @param ast the AST to check.
60       * @return true if the IDENT subnode of ast should be checked against
61       *     the format regexp.
62       */
63      protected abstract boolean mustCheckName(DetailAST ast);
64  
65      /**
66       * Sets the pattern to match valid identifiers.
67       *
68       * @param pattern the new pattern
69       */
70      public final void setFormat(Pattern pattern) {
71          format = pattern;
72      }
73  
74      @Override
75      public void visitToken(DetailAST ast) {
76          if (mustCheckName(ast)) {
77              final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT);
78              if (!format.matcher(nameAST.getText()).find()) {
79                  log(nameAST,
80                      MSG_INVALID_PATTERN,
81                      nameAST.getText(),
82                      format.pattern());
83              }
84          }
85      }
86  
87  }