001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2018 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 * <p> 031 * Ensures that the names of abstract classes conforming to some 032 * regular expression and check that {@code abstract} modifier exists. 033 * </p> 034 * <p> 035 * Rationale: Abstract classes are convenience base class 036 * implementations of interfaces, not types as such. As such 037 * they should be named to indicate this. Also if names of classes 038 * starts with 'Abstract' it's very convenient that they will 039 * have abstract modifier. 040 * </p> 041 * 042 * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a> 043 * @author <a href="mailto:solid.danil@gmail.com">Danil Lopatin</a> 044 */ 045@StatelessCheck 046public final class AbstractClassNameCheck extends AbstractCheck { 047 048 /** 049 * A key is pointing to the warning message text in "messages.properties" 050 * file. 051 */ 052 public static final String MSG_ILLEGAL_ABSTRACT_CLASS_NAME = "illegal.abstract.class.name"; 053 054 /** 055 * A key is pointing to the warning message text in "messages.properties" 056 * file. 057 */ 058 public static final String MSG_NO_ABSTRACT_CLASS_MODIFIER = "no.abstract.class.modifier"; 059 060 /** Whether to ignore checking the modifier. */ 061 private boolean ignoreModifier; 062 063 /** Whether to ignore checking the name. */ 064 private boolean ignoreName; 065 066 /** The regexp to match against. */ 067 private Pattern format = Pattern.compile("^Abstract.+$"); 068 069 /** 070 * Whether to ignore checking for the {@code abstract} modifier. 071 * @param value new value 072 */ 073 public void setIgnoreModifier(boolean value) { 074 ignoreModifier = value; 075 } 076 077 /** 078 * Whether to ignore checking the name. 079 * @param value new value. 080 */ 081 public void setIgnoreName(boolean value) { 082 ignoreName = value; 083 } 084 085 /** 086 * Set the format for the specified regular expression. 087 * @param pattern the new pattern 088 */ 089 public void setFormat(Pattern pattern) { 090 format = pattern; 091 } 092 093 @Override 094 public int[] getDefaultTokens() { 095 return getRequiredTokens(); 096 } 097 098 @Override 099 public int[] getRequiredTokens() { 100 return new int[] {TokenTypes.CLASS_DEF}; 101 } 102 103 @Override 104 public int[] getAcceptableTokens() { 105 return getRequiredTokens(); 106 } 107 108 @Override 109 public void visitToken(DetailAST ast) { 110 visitClassDef(ast); 111 } 112 113 /** 114 * Checks class definition. 115 * @param ast class definition for check. 116 */ 117 private void visitClassDef(DetailAST ast) { 118 final String className = 119 ast.findFirstToken(TokenTypes.IDENT).getText(); 120 if (isAbstract(ast)) { 121 // if class has abstract modifier 122 if (!ignoreName && !isMatchingClassName(className)) { 123 log(ast.getLineNo(), ast.getColumnNo(), 124 MSG_ILLEGAL_ABSTRACT_CLASS_NAME, className, format.pattern()); 125 } 126 } 127 else if (!ignoreModifier && isMatchingClassName(className)) { 128 log(ast.getLineNo(), ast.getColumnNo(), 129 MSG_NO_ABSTRACT_CLASS_MODIFIER, className); 130 } 131 } 132 133 /** 134 * Checks if declared class is abstract or not. 135 * @param ast class definition for check. 136 * @return true if a given class declared as abstract. 137 */ 138 private static boolean isAbstract(DetailAST ast) { 139 final DetailAST abstractAST = ast.findFirstToken(TokenTypes.MODIFIERS) 140 .findFirstToken(TokenTypes.ABSTRACT); 141 142 return abstractAST != null; 143 } 144 145 /** 146 * Returns true if class name matches format of abstract class names. 147 * @param className class name for check. 148 * @return true if class name matches format of abstract class names. 149 */ 150 private boolean isMatchingClassName(String className) { 151 return format.matcher(className).find(); 152 } 153 154}