001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2024 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 * <ul> 041 * <li> 042 * Property {@code format} - Specify valid identifiers. 043 * Type is {@code java.util.regex.Pattern}. 044 * Default value is {@code "^Abstract.+$"}.</li> 045 * <li> 046 * Property {@code ignoreModifier} - Control whether to ignore checking for the 047 * {@code abstract} modifier on classes that match the name. 048 * Type is {@code boolean}. 049 * Default value is {@code false}.</li> 050 * <li> 051 * Property {@code ignoreName} - Control whether to ignore checking the name. 052 * Realistically only useful if using the check to identify that match name and 053 * do not have the {@code abstract} modifier. 054 * Type is {@code boolean}. 055 * Default value is {@code false}. 056 * </li> 057 * </ul> 058 * 059 * <p> 060 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 061 * </p> 062 * 063 * <p> 064 * Violation Message Keys: 065 * </p> 066 * <ul> 067 * <li> 068 * {@code illegal.abstract.class.name} 069 * </li> 070 * <li> 071 * {@code no.abstract.class.modifier} 072 * </li> 073 * </ul> 074 * 075 * @since 3.2 076 */ 077@StatelessCheck 078public final class AbstractClassNameCheck extends AbstractCheck { 079 080 /** 081 * A key is pointing to the warning message text in "messages.properties" 082 * file. 083 */ 084 public static final String MSG_ILLEGAL_ABSTRACT_CLASS_NAME = "illegal.abstract.class.name"; 085 086 /** 087 * A key is pointing to the warning message text in "messages.properties" 088 * file. 089 */ 090 public static final String MSG_NO_ABSTRACT_CLASS_MODIFIER = "no.abstract.class.modifier"; 091 092 /** 093 * Control whether to ignore checking for the {@code abstract} modifier on 094 * classes that match the name. 095 */ 096 private boolean ignoreModifier; 097 098 /** 099 * Control whether to ignore checking the name. Realistically only useful 100 * if using the check to identify that match name and do not have the 101 * {@code abstract} modifier. 102 */ 103 private boolean ignoreName; 104 105 /** Specify valid identifiers. */ 106 private Pattern format = Pattern.compile("^Abstract.+$"); 107 108 /** 109 * Setter to control whether to ignore checking for the {@code abstract} modifier on 110 * classes that match the name. 111 * 112 * @param value new value 113 * @since 5.3 114 */ 115 public void setIgnoreModifier(boolean value) { 116 ignoreModifier = value; 117 } 118 119 /** 120 * Setter to control whether to ignore checking the name. Realistically only useful if 121 * using the check to identify that match name and do not have the {@code abstract} modifier. 122 * 123 * @param value new value. 124 * @since 5.3 125 */ 126 public void setIgnoreName(boolean value) { 127 ignoreName = value; 128 } 129 130 /** 131 * Setter to specify valid identifiers. 132 * 133 * @param pattern the new pattern 134 * @since 3.2 135 */ 136 public void setFormat(Pattern pattern) { 137 format = pattern; 138 } 139 140 @Override 141 public int[] getDefaultTokens() { 142 return getRequiredTokens(); 143 } 144 145 @Override 146 public int[] getRequiredTokens() { 147 return new int[] {TokenTypes.CLASS_DEF}; 148 } 149 150 @Override 151 public int[] getAcceptableTokens() { 152 return getRequiredTokens(); 153 } 154 155 @Override 156 public void visitToken(DetailAST ast) { 157 visitClassDef(ast); 158 } 159 160 /** 161 * Checks class definition. 162 * 163 * @param ast class definition for check. 164 */ 165 private void visitClassDef(DetailAST ast) { 166 final String className = 167 ast.findFirstToken(TokenTypes.IDENT).getText(); 168 if (isAbstract(ast)) { 169 // if class has abstract modifier 170 if (!ignoreName && !isMatchingClassName(className)) { 171 log(ast, MSG_ILLEGAL_ABSTRACT_CLASS_NAME, className, format.pattern()); 172 } 173 } 174 else if (!ignoreModifier && isMatchingClassName(className)) { 175 log(ast, MSG_NO_ABSTRACT_CLASS_MODIFIER, className); 176 } 177 } 178 179 /** 180 * Checks if declared class is abstract or not. 181 * 182 * @param ast class definition for check. 183 * @return true if a given class declared as abstract. 184 */ 185 private static boolean isAbstract(DetailAST ast) { 186 final DetailAST abstractAST = ast.findFirstToken(TokenTypes.MODIFIERS) 187 .findFirstToken(TokenTypes.ABSTRACT); 188 189 return abstractAST != null; 190 } 191 192 /** 193 * Returns true if class name matches format of abstract class names. 194 * 195 * @param className class name for check. 196 * @return true if class name matches format of abstract class names. 197 */ 198 private boolean isMatchingClassName(String className) { 199 return format.matcher(className).find(); 200 } 201 202}