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