001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2014 Oliver Burn 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.naming; 021 022import com.puppycrawl.tools.checkstyle.api.DetailAST; 023import com.puppycrawl.tools.checkstyle.api.TokenTypes; 024import com.puppycrawl.tools.checkstyle.checks.AbstractFormatCheck; 025 026/** 027 * Abstract class for checking that names conform to a specified format. 028 * 029 * @author Rick Giles 030 * @version 1.0 031 */ 032public abstract class AbstractNameCheck 033 extends AbstractFormatCheck 034{ 035 /** 036 * Message key for invalid pattern error. 037 */ 038 public static final String MSG_INVALID_PATTERN = "name.invalidPattern"; 039 040 /** 041 * Creates a new <code>AbstractNameCheck</code> instance. 042 * @param format format to check with 043 */ 044 public AbstractNameCheck(String format) 045 { 046 super(format); 047 } 048 049 /** 050 * Decides whether the name of an AST should be checked against 051 * the format regexp. 052 * @param ast the AST to check. 053 * @return true if the IDENT subnode of ast should be checked against 054 * the format regexp. 055 */ 056 protected boolean mustCheckName(DetailAST ast) 057 { 058 return true; 059 } 060 061 @Override 062 public void visitToken(DetailAST ast) 063 { 064 if (mustCheckName(ast)) { 065 final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT); 066 if (!getRegexp().matcher(nameAST.getText()).find()) { 067 log(nameAST.getLineNo(), 068 nameAST.getColumnNo(), 069 MSG_INVALID_PATTERN, 070 nameAST.getText(), 071 getFormat()); 072 } 073 } 074 } 075}