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.sizes; 021 022import java.util.Arrays; 023import java.util.concurrent.atomic.AtomicInteger; 024 025import com.puppycrawl.tools.checkstyle.StatelessCheck; 026import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 027import com.puppycrawl.tools.checkstyle.api.DetailAST; 028import com.puppycrawl.tools.checkstyle.api.TokenTypes; 029import com.puppycrawl.tools.checkstyle.checks.naming.AccessModifierOption; 030import com.puppycrawl.tools.checkstyle.utils.CheckUtil; 031import com.puppycrawl.tools.checkstyle.utils.TokenUtil; 032 033/** 034 * <p> 035 * Checks the number of record components in the 036 * <a href="https://docs.oracle.com/javase/specs/jls/se14/preview/specs/records-jls.html#jls-8.10.1"> 037 * header</a> of a record definition. 038 * </p> 039 * <ul> 040 * <li> 041 * Property {@code accessModifiers} - Access modifiers of record definitions where 042 * the number of record components should be checked. 043 * Type is {@code com.puppycrawl.tools.checkstyle.checks.naming.AccessModifierOption[]}. 044 * Default value is {@code public, protected, package, private}. 045 * </li> 046 * <li> 047 * Property {@code max} - Specify the maximum number of components allowed in the header of a 048 * record definition. 049 * Type is {@code int}. 050 * Default value is {@code 8}. 051 * </li> 052 * </ul> 053 * <p> 054 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 055 * </p> 056 * <p> 057 * Violation Message Keys: 058 * </p> 059 * <ul> 060 * <li> 061 * {@code too.many.components} 062 * </li> 063 * </ul> 064 * 065 * @since 8.36 066 */ 067@StatelessCheck 068public class RecordComponentNumberCheck extends AbstractCheck { 069 070 /** 071 * A key is pointing to the warning message text in "messages.properties" 072 * file. 073 */ 074 public static final String MSG_KEY = "too.many.components"; 075 076 /** Default maximum number of allowed components. */ 077 private static final int DEFAULT_MAX_COMPONENTS = 8; 078 079 /** Specify the maximum number of components allowed in the header of a record definition. */ 080 private int max = DEFAULT_MAX_COMPONENTS; 081 082 /** 083 * Access modifiers of record definitions where the number 084 * of record components should be checked. 085 */ 086 private AccessModifierOption[] accessModifiers = { 087 AccessModifierOption.PUBLIC, 088 AccessModifierOption.PROTECTED, 089 AccessModifierOption.PACKAGE, 090 AccessModifierOption.PRIVATE, 091 }; 092 093 /** 094 * Setter to specify the maximum number of components allowed in the header 095 * of a record definition. 096 * 097 * @param value the maximum allowed. 098 * @since 8.36 099 */ 100 public void setMax(int value) { 101 max = value; 102 } 103 104 /** 105 * Setter to access modifiers of record definitions where the number of record 106 * components should be checked. 107 * 108 * @param accessModifiers access modifiers of record definitions which should be checked. 109 * @since 8.36 110 */ 111 public void setAccessModifiers(AccessModifierOption... accessModifiers) { 112 this.accessModifiers = 113 Arrays.copyOf(accessModifiers, accessModifiers.length); 114 } 115 116 @Override 117 public int[] getDefaultTokens() { 118 return getAcceptableTokens(); 119 } 120 121 @Override 122 public int[] getAcceptableTokens() { 123 return new int[] { 124 TokenTypes.RECORD_DEF, 125 }; 126 } 127 128 @Override 129 public int[] getRequiredTokens() { 130 return getAcceptableTokens(); 131 } 132 133 @Override 134 public void visitToken(DetailAST ast) { 135 final AccessModifierOption accessModifier = 136 CheckUtil.getAccessModifierFromModifiersToken(ast); 137 138 if (matchAccessModifiers(accessModifier)) { 139 final DetailAST recordComponents = 140 ast.findFirstToken(TokenTypes.RECORD_COMPONENTS); 141 final int componentCount = countComponents(recordComponents); 142 143 if (componentCount > max) { 144 log(ast, MSG_KEY, componentCount, max); 145 } 146 } 147 } 148 149 /** 150 * Method to count the number of record components in this record definition. 151 * 152 * @param recordComponents the ast to check 153 * @return the number of record components in this record definition 154 */ 155 private static int countComponents(DetailAST recordComponents) { 156 final AtomicInteger count = new AtomicInteger(0); 157 TokenUtil.forEachChild(recordComponents, 158 TokenTypes.RECORD_COMPONENT_DEF, 159 node -> count.getAndIncrement()); 160 return count.get(); 161 } 162 163 /** 164 * Checks whether a record definition has the correct access modifier to be checked. 165 * 166 * @param accessModifier the access modifier of the record definition. 167 * @return whether the record definition matches the expected access modifier. 168 */ 169 private boolean matchAccessModifiers(final AccessModifierOption accessModifier) { 170 return Arrays.stream(accessModifiers) 171 .anyMatch(modifier -> modifier == accessModifier); 172 } 173}