001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2015 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.regex.Matcher;
023import java.util.regex.Pattern;
024
025import com.puppycrawl.tools.checkstyle.api.Check;
026import com.puppycrawl.tools.checkstyle.api.DetailAST;
027import com.puppycrawl.tools.checkstyle.api.TextBlock;
028import com.puppycrawl.tools.checkstyle.api.TokenTypes;
029import com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTagInfo;
030import com.puppycrawl.tools.checkstyle.utils.AnnotationUtility;
031import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
032
033/**
034 * <p>
035 * This class is used to verify that the {@link Override Override}
036 * annotation is present when the inheritDoc javadoc tag is present.
037 * </p>
038 *
039 * <p>
040 * Rationale: The {@link Override Override} annotation helps
041 * compiler tools ensure that an override is actually occurring.  It is
042 * quite easy to accidentally overload a method or hide a static method
043 * and using the {@link Override Override} annotation points
044 * out these problems.
045 * </p>
046 *
047 * <p>
048 * This check will log a violation if using the inheritDoc tag on a method that
049 * is not valid (ex: private, or static method).
050 * </p>
051 *
052 * <p>
053 * There is a slight difference between the Override annotation in Java 5 versus
054 * Java 6 and above. In Java 5, any method overridden from an interface cannot
055 * be annotated with Override. In Java 6 this behavior is allowed.
056 * </p>
057 *
058 * <p>
059 * As a result of the aforementioned difference between Java 5 and Java 6, a
060 * property called {@code javaFiveCompatibility } is available. This
061 * property will only check classes, interfaces, etc. that do not contain the
062 * extends or implements keyword or are not anonymous classes. This means it
063 * only checks methods overridden from {@code java.lang.Object}
064 *
065 * <b>Java 5 Compatibility mode severely limits this check. It is recommended to
066 * only use it on Java 5 source</b>
067 * </p>
068 *
069 * <pre>
070 * &lt;module name=&quot;MissingOverride&quot;&gt;
071 *    &lt;property name=&quot;javaFiveCompatibility&quot;
072 *        value=&quot;true&quot;/&gt;
073 * &lt;/module&gt;
074 * </pre>
075 *
076 * @author Travis Schneeberger
077 */
078public final class MissingOverrideCheck extends Check {
079    /**
080     * A key is pointing to the warning message text in "messages.properties"
081     * file.
082     */
083    public static final String MSG_KEY_TAG_NOT_VALID_ON = "tag.not.valid.on";
084
085    /**
086     * A key is pointing to the warning message text in "messages.properties"
087     * file.
088     */
089    public static final String MSG_KEY_ANNOTATION_MISSING_OVERRIDE =
090        "annotation.missing.override";
091
092    /** {@link Override Override} annotation name. */
093    private static final String OVERRIDE = "Override";
094
095    /** Fully-qualified {@link Override Override} annotation name. */
096    private static final String FQ_OVERRIDE = "java.lang." + OVERRIDE;
097
098    /** Compiled regexp to match Javadoc tags with no argument and {}. */
099    private static final Pattern MATCH_INHERIT_DOC =
100            CommonUtils.createPattern("\\{\\s*@(inheritDoc)\\s*\\}");
101
102    /**
103     * Java 5 compatibility option.
104     * @see #setJavaFiveCompatibility(boolean)
105     */
106    private boolean javaFiveCompatibility;
107
108    /**
109     * Sets Java 5 compatibility mode.
110     *
111     * <p>
112     * In Java 5, this check could flag code that is not valid for the Override
113     * annotation even though it is a proper override. See the class
114     * documentation for more information.
115     * </p>
116     *
117     * <p>
118     * Set this to true to turn on Java 5 compatibility mode. Set this to
119     * false to turn off Java 5 compatibility mode.
120     * </p>
121     *
122     * @param compatibility compatibility or not
123     */
124    public void setJavaFiveCompatibility(final boolean compatibility) {
125        javaFiveCompatibility = compatibility;
126    }
127
128    @Override
129    public int[] getDefaultTokens() {
130        return getRequiredTokens();
131    }
132
133    @Override
134    public int[] getAcceptableTokens() {
135        return getRequiredTokens();
136    }
137
138    @Override
139    public int[] getRequiredTokens() {
140        return new int[]
141        {TokenTypes.METHOD_DEF, };
142    }
143
144    @Override
145    public void visitToken(final DetailAST ast) {
146        final TextBlock javadoc =
147            getFileContents().getJavadocBefore(ast.getLineNo());
148
149        final boolean containsTag = containsJavadocTag(javadoc);
150        if (containsTag && !JavadocTagInfo.INHERIT_DOC.isValidOn(ast)) {
151            log(ast.getLineNo(), MSG_KEY_TAG_NOT_VALID_ON,
152                JavadocTagInfo.INHERIT_DOC.getText());
153            return;
154        }
155
156        if (javaFiveCompatibility) {
157            final DetailAST defOrNew = ast.getParent().getParent();
158
159            if (defOrNew.branchContains(TokenTypes.EXTENDS_CLAUSE)
160                || defOrNew.branchContains(TokenTypes.IMPLEMENTS_CLAUSE)
161                || defOrNew.getType() == TokenTypes.LITERAL_NEW) {
162                return;
163            }
164        }
165
166        if (containsTag
167            && !AnnotationUtility.containsAnnotation(ast, OVERRIDE)
168            && !AnnotationUtility.containsAnnotation(ast, FQ_OVERRIDE)) {
169            log(ast.getLineNo(), MSG_KEY_ANNOTATION_MISSING_OVERRIDE);
170        }
171    }
172
173    /**
174     * Checks to see if the text block contains a inheritDoc tag.
175     *
176     * @param javadoc the javadoc of the AST
177     * @return true if contains the tag
178     */
179    private static boolean containsJavadocTag(final TextBlock javadoc) {
180        boolean javadocTag = false;
181
182        if (javadoc != null) {
183            final String[] lines = javadoc.getText();
184
185            for (final String line : lines) {
186                final Matcher matchInheritDoc =
187                    MATCH_INHERIT_DOC.matcher(line);
188
189                if (matchInheritDoc.find()) {
190                    javadocTag = true;
191                    break;
192                }
193            }
194        }
195        return javadocTag;
196    }
197}