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