1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2026 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.checks.annotation;
21
22 import java.util.HashSet;
23 import java.util.Set;
24
25 import com.puppycrawl.tools.checkstyle.StatelessCheck;
26 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
27 import com.puppycrawl.tools.checkstyle.api.DetailAST;
28 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29 import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil;
30 import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
31
32 /**
33 * <div>
34 * Verifies that explicitly declared record accessor methods include
35 * the {@code @Override} annotation.
36 * </div>
37 *
38 * <p>
39 * Per <a href="https://openjdk.org/jeps/395">JEP 395</a>, the meaning of the
40 * {@code @Override} annotation was extended to include explicitly declared
41 * accessor methods for record components.
42 * </p>
43 *
44 * <p>
45 * This check focuses only on record component accessor methods. It does not
46 * attempt to detect general method overrides from interfaces or superclasses,
47 * due to Checkstyle's single-file analysis limitations.
48 * </p>
49 *
50 * @since 13.1.0
51 */
52 @StatelessCheck
53 public class MissingOverrideOnRecordAccessorCheck extends AbstractCheck {
54
55 /**
56 * A key is pointing to the warning message text in "messages.properties" file.
57 */
58 public static final String MSG_KEY = "annotation.missing.override.record.accessor";
59
60 /**
61 * Creates a new {@code MissingOverrideOnRecordAccessorCheck} instance.
62 */
63 public MissingOverrideOnRecordAccessorCheck() {
64 // no code by default
65 }
66
67 @Override
68 public int[] getDefaultTokens() {
69 return getRequiredTokens();
70 }
71
72 @Override
73 public int[] getAcceptableTokens() {
74 return getRequiredTokens();
75 }
76
77 @Override
78 public int[] getRequiredTokens() {
79 return new int[] {TokenTypes.METHOD_DEF};
80 }
81
82 @Override
83 public void visitToken(DetailAST ast) {
84 if (isRecordAccessorMethod(ast) && !AnnotationUtil.hasOverrideAnnotation(ast)) {
85 log(ast, MSG_KEY);
86 }
87 }
88
89 /**
90 * Checks if the given method is an explicitly declared accessor for a record component.
91 *
92 * @param ast the METHOD_DEF AST node
93 * @return true if this method is a record accessor
94 */
95 private static boolean isRecordAccessorMethod(DetailAST ast) {
96 boolean result = false;
97 final DetailAST grandParent = ast.getParent().getParent();
98 if (grandParent != null && grandParent.getType() == TokenTypes.RECORD_DEF) {
99 final DetailAST parameters = ast.findFirstToken(TokenTypes.PARAMETERS);
100 if (parameters.getChildCount() == 0) {
101 final String methodName = getMethodName(ast);
102 result = getRecordComponentNames(grandParent).contains(methodName);
103 }
104 }
105 return result;
106 }
107
108 /**
109 * Gets the method name from a method definition AST.
110 *
111 * @param methodDef the METHOD_DEF AST node
112 * @return the method name
113 */
114 private static String getMethodName(DetailAST methodDef) {
115 return TokenUtil.getIdent(methodDef).getText();
116 }
117
118 /**
119 * Gets the set of record component names from a record definition.
120 *
121 * @param recordDef the RECORD_DEF AST node
122 * @return set of component names
123 */
124 private static Set<String> getRecordComponentNames(DetailAST recordDef) {
125 final Set<String> names = new HashSet<>();
126 final DetailAST recordComponents = recordDef.findFirstToken(TokenTypes.RECORD_COMPONENTS);
127 DetailAST child = recordComponents.getFirstChild();
128 while (child != null) {
129 if (child.getType() == TokenTypes.RECORD_COMPONENT_DEF) {
130 names.add(TokenUtil.getIdent(child).getText());
131 }
132 child = child.getNextSibling();
133 }
134 return names;
135 }
136
137 }