001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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.annotation; 021 022import java.util.HashSet; 023import java.util.Set; 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.utils.AnnotationUtil; 030 031/** 032 * <div> 033 * Verifies that explicitly declared record accessor methods include 034 * the {@code @Override} annotation. 035 * </div> 036 * 037 * <p> 038 * Per <a href="https://openjdk.org/jeps/395">JEP 395</a>, the meaning of the 039 * {@code @Override} annotation was extended to include explicitly declared 040 * accessor methods for record components. 041 * </p> 042 * 043 * <p> 044 * This check focuses only on record component accessor methods. It does not 045 * attempt to detect general method overrides from interfaces or superclasses, 046 * due to Checkstyle's single-file analysis limitations. 047 * </p> 048 * 049 * @since 13.1.0 050 */ 051@StatelessCheck 052public class MissingOverrideOnRecordAccessorCheck extends AbstractCheck { 053 054 /** 055 * A key is pointing to the warning message text in "messages.properties" file. 056 */ 057 public static final String MSG_KEY = "annotation.missing.override.record.accessor"; 058 059 @Override 060 public int[] getDefaultTokens() { 061 return getRequiredTokens(); 062 } 063 064 @Override 065 public int[] getAcceptableTokens() { 066 return getRequiredTokens(); 067 } 068 069 @Override 070 public int[] getRequiredTokens() { 071 return new int[] {TokenTypes.METHOD_DEF}; 072 } 073 074 @Override 075 public void visitToken(DetailAST ast) { 076 if (isRecordAccessorMethod(ast) && !AnnotationUtil.hasOverrideAnnotation(ast)) { 077 log(ast, MSG_KEY); 078 } 079 } 080 081 /** 082 * Checks if the given method is an explicitly declared accessor for a record component. 083 * 084 * @param ast the METHOD_DEF AST node 085 * @return true if this method is a record accessor 086 */ 087 private static boolean isRecordAccessorMethod(DetailAST ast) { 088 boolean result = false; 089 final DetailAST grandParent = ast.getParent().getParent(); 090 if (grandParent.getType() == TokenTypes.RECORD_DEF) { 091 final DetailAST parameters = ast.findFirstToken(TokenTypes.PARAMETERS); 092 if (parameters.getChildCount() == 0) { 093 final String methodName = ast.findFirstToken(TokenTypes.IDENT).getText(); 094 result = getRecordComponentNames(grandParent).contains(methodName); 095 } 096 } 097 return result; 098 } 099 100 /** 101 * Gets the set of record component names from a record definition. 102 * 103 * @param recordDef the RECORD_DEF AST node 104 * @return set of component names 105 */ 106 private static Set<String> getRecordComponentNames(DetailAST recordDef) { 107 final Set<String> names = new HashSet<>(); 108 final DetailAST recordComponents = recordDef.findFirstToken(TokenTypes.RECORD_COMPONENTS); 109 DetailAST child = recordComponents.getFirstChild(); 110 while (child != null) { 111 if (child.getType() == TokenTypes.RECORD_COMPONENT_DEF) { 112 names.add(child.findFirstToken(TokenTypes.IDENT).getText()); 113 } 114 child = child.getNextSibling(); 115 } 116 return names; 117 } 118}