001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 the original author or authors. 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 java.util.regex.Pattern; 023 024import com.puppycrawl.tools.checkstyle.StatelessCheck; 025import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 026import com.puppycrawl.tools.checkstyle.api.DetailAST; 027import com.puppycrawl.tools.checkstyle.api.TokenTypes; 028 029/** 030 * <div> 031 * Ensures that the names of abstract classes conforming to some pattern 032 * and check that {@code abstract} modifier exists. 033 * </div> 034 * 035 * <p> 036 * Rationale: Abstract classes are convenience base class implementations of 037 * interfaces. For this reason, it should be made obvious that a given class 038 * is abstract by prefacing the class name with 'Abstract'. 039 * </p> 040 * 041 * @since 3.2 042 */ 043@StatelessCheck 044public final class AbstractClassNameCheck extends AbstractCheck { 045 046 /** 047 * A key is pointing to the warning message text in "messages.properties" file. 048 */ 049 public static final String MSG_ILLEGAL_ABSTRACT_CLASS_NAME = "illegal.abstract.class.name"; 050 051 /** 052 * A key is pointing to the warning message text in "messages.properties" file. 053 */ 054 public static final String MSG_NO_ABSTRACT_CLASS_MODIFIER = "no.abstract.class.modifier"; 055 056 /** 057 * Control whether to ignore checking for the {@code abstract} modifier on 058 * classes that match the name. 059 */ 060 private boolean ignoreModifier; 061 062 /** 063 * Control whether to ignore checking the name. Realistically only useful 064 * if using the check to identify that match name and do not have the 065 * {@code abstract} modifier. 066 */ 067 private boolean ignoreName; 068 069 /** Specify valid identifiers. */ 070 private Pattern format = Pattern.compile("^Abstract.+$"); 071 072 /** 073 * Creates a new {@code AbstractClassNameCheck} instance. 074 */ 075 public AbstractClassNameCheck() { 076 // no code by default 077 } 078 079 /** 080 * Setter to control whether to ignore checking for the {@code abstract} modifier on 081 * classes that match the name. 082 * 083 * @param value new value 084 * @since 5.3 085 */ 086 public void setIgnoreModifier(boolean value) { 087 ignoreModifier = value; 088 } 089 090 /** 091 * Setter to control whether to ignore checking the name. Realistically only useful if 092 * using the check to identify that match name and do not have the {@code abstract} modifier. 093 * 094 * @param value new value. 095 * @since 5.3 096 */ 097 public void setIgnoreName(boolean value) { 098 ignoreName = value; 099 } 100 101 /** 102 * Setter to specify valid identifiers. 103 * 104 * @param pattern the new pattern 105 * @since 3.2 106 */ 107 public void setFormat(Pattern pattern) { 108 format = pattern; 109 } 110 111 @Override 112 public int[] getDefaultTokens() { 113 return getRequiredTokens(); 114 } 115 116 @Override 117 public int[] getRequiredTokens() { 118 return new int[] {TokenTypes.CLASS_DEF}; 119 } 120 121 @Override 122 public int[] getAcceptableTokens() { 123 return getRequiredTokens(); 124 } 125 126 @Override 127 public void visitToken(DetailAST ast) { 128 visitClassDef(ast); 129 } 130 131 /** 132 * Checks class definition. 133 * 134 * @param ast class definition for check. 135 */ 136 private void visitClassDef(DetailAST ast) { 137 final String className = 138 ast.findFirstToken(TokenTypes.IDENT).getText(); 139 if (isAbstract(ast)) { 140 // if class has abstract modifier 141 if (!ignoreName && !isMatchingClassName(className)) { 142 log(ast, MSG_ILLEGAL_ABSTRACT_CLASS_NAME, className, format.pattern()); 143 } 144 } 145 else if (!ignoreModifier && isMatchingClassName(className)) { 146 log(ast, MSG_NO_ABSTRACT_CLASS_MODIFIER, className); 147 } 148 } 149 150 /** 151 * Checks if declared class is abstract or not. 152 * 153 * @param ast class definition for check. 154 * @return true if a given class declared as abstract. 155 */ 156 private static boolean isAbstract(DetailAST ast) { 157 final DetailAST abstractAST = ast.findFirstToken(TokenTypes.MODIFIERS) 158 .findFirstToken(TokenTypes.ABSTRACT); 159 160 return abstractAST != null; 161 } 162 163 /** 164 * Returns true if class name matches format of abstract class names. 165 * 166 * @param className class name for check. 167 * @return true if class name matches format of abstract class names. 168 */ 169 private boolean isMatchingClassName(String className) { 170 return format.matcher(className).find(); 171 } 172 173}