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.Objects;
023import java.util.Optional;
024import java.util.regex.Pattern;
025import java.util.stream.Stream;
026
027import com.puppycrawl.tools.checkstyle.StatelessCheck;
028import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
029import com.puppycrawl.tools.checkstyle.api.DetailAST;
030import com.puppycrawl.tools.checkstyle.api.TokenTypes;
031import com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTagInfo;
032import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil;
033import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
034import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
035
036/**
037 * <div>
038 * Verifies that the {@code @Override} annotation is present
039 * when the {@code @inheritDoc} javadoc tag is present.
040 * </div>
041 *
042 * <p>
043 * Rationale: The &#64;Override annotation helps
044 * compiler tools ensure that an override is actually occurring.  It is
045 * quite easy to accidentally overload a method or hide a static method
046 * and using the &#64;Override annotation points out these problems.
047 * </p>
048 *
049 * <p>
050 * This check will log a violation if using the &#64;inheritDoc tag on a method that
051 * is not valid (ex: private, or static method).
052 * </p>
053 *
054 * <p>
055 * There is a slight difference between the &#64;Override annotation in Java 5 versus
056 * Java 6 and above. In Java 5, any method overridden from an interface cannot
057 * be annotated with &#64;Override. In Java 6 this behavior is allowed.
058 * </p>
059 *
060 * <p>
061 * As a result of the aforementioned difference between Java 5 and Java 6, a
062 * property called {@code javaFiveCompatibility} is available. This
063 * property will only check classes, interfaces, etc. that do not contain the
064 * extends or implements keyword or are not anonymous classes. This means it
065 * only checks methods overridden from {@code java.lang.Object}.
066 * <b>Java 5 Compatibility mode severely limits this check. It is recommended to
067 * only use it on Java 5 source.</b>
068 * </p>
069 *
070 * @since 5.0
071 */
072@StatelessCheck
073public final class MissingOverrideCheck extends AbstractCheck {
074
075    /**
076     * A key is pointing to the warning message text in "messages.properties" file.
077     */
078    public static final String MSG_KEY_TAG_NOT_VALID_ON = "tag.not.valid.on";
079
080    /**
081     * A key is pointing to the warning message text in "messages.properties" file.
082     */
083    public static final String MSG_KEY_ANNOTATION_MISSING_OVERRIDE =
084        "annotation.missing.override";
085
086    /** Compiled regexp to match Javadoc tags with no argument and {}. */
087    private static final Pattern MATCH_INHERIT_DOC =
088            CommonUtil.createPattern("\\{\\s*@(inheritDoc)\\s*\\}");
089
090    /**
091     * Enable java 5 compatibility mode.
092     */
093    private boolean javaFiveCompatibility;
094
095    /**
096     * Creates a new {@code MissingOverrideCheck} instance.
097     */
098    public MissingOverrideCheck() {
099        // no code by default
100    }
101
102    /**
103     * Setter to enable java 5 compatibility mode.
104     *
105     * @param compatibility compatibility or not
106     * @since 5.0
107     */
108    public void setJavaFiveCompatibility(final boolean compatibility) {
109        javaFiveCompatibility = compatibility;
110    }
111
112    @Override
113    public int[] getDefaultTokens() {
114        return getRequiredTokens();
115    }
116
117    @Override
118    public int[] getAcceptableTokens() {
119        return getRequiredTokens();
120    }
121
122    @Override
123    public boolean isCommentNodesRequired() {
124        return true;
125    }
126
127    @Override
128    public int[] getRequiredTokens() {
129        return new int[]
130        {TokenTypes.METHOD_DEF, };
131    }
132
133    @Override
134    public void visitToken(final DetailAST ast) {
135        final boolean containsTag = containsInheritDocTag(ast);
136        if (containsTag && !JavadocTagInfo.INHERIT_DOC.isValidOn(ast)) {
137            log(ast, MSG_KEY_TAG_NOT_VALID_ON,
138                JavadocTagInfo.INHERIT_DOC.getText());
139        }
140        else if (containsTag
141                && !AnnotationUtil.hasOverrideAnnotation(ast)
142                && (!javaFiveCompatibility || doesOverrideOnlyObjectMethods(ast))) {
143            log(ast, MSG_KEY_ANNOTATION_MISSING_OVERRIDE);
144        }
145    }
146
147    /**
148     * Checks whether the method's enclosing type can only be overriding methods
149     * declared in {@code java.lang.Object}. This is the case when the enclosing type
150     * does not extend or implement anything and is not an anonymous class. A top-level
151     * method in a compact source file has no enclosing type node ({@code defOrNew} is
152     * null); its implicit class satisfies this condition as well.
153     *
154     * @param ast method AST node
155     * @return true if the method's enclosing type can only override
156     *     {@code java.lang.Object} methods
157     */
158    private static boolean doesOverrideOnlyObjectMethods(DetailAST ast) {
159        final DetailAST defOrNew = ast.getParent().getParent();
160        return defOrNew == null
161            || defOrNew.findFirstToken(TokenTypes.EXTENDS_CLAUSE) == null
162                && defOrNew.findFirstToken(TokenTypes.IMPLEMENTS_CLAUSE) == null
163                && defOrNew.getType() != TokenTypes.LITERAL_NEW;
164    }
165
166    /**
167     * Checks to see if the ast contains a inheritDoc tag.
168     *
169     * @param ast method AST node
170     * @return true if contains the tag
171     */
172    private static boolean containsInheritDocTag(DetailAST ast) {
173        final DetailAST modifiers = ast.getFirstChild();
174        final DetailAST startNode;
175        if (modifiers.hasChildren()) {
176            startNode = Optional.ofNullable(ast.getFirstChild()
177                    .findFirstToken(TokenTypes.ANNOTATION))
178                .orElse(modifiers);
179        }
180        else {
181            startNode = ast.findFirstToken(TokenTypes.TYPE);
182        }
183        final Optional<String> javadoc =
184            Stream.iterate(startNode.getLastChild(), Objects::nonNull,
185                    DetailAST::getPreviousSibling)
186            .filter(node -> node.getType() == TokenTypes.BLOCK_COMMENT_BEGIN)
187            .map(DetailAST::getFirstChild)
188            .map(DetailAST::getText)
189            .filter(JavadocUtil::isJavadocComment)
190            .findFirst();
191        return javadoc.isPresent()
192                && MATCH_INHERIT_DOC.matcher(javadoc.orElseThrow()).find();
193    }
194
195}