001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2020 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.coding;
021
022import java.util.Deque;
023import java.util.LinkedList;
024
025import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
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.ScopeUtil;
030
031/**
032 * <p>
033 * Abstract class for checking that an overriding method with no parameters
034 * invokes the super method.
035 * </p>
036 */
037@FileStatefulCheck
038public abstract class AbstractSuperCheck
039        extends AbstractCheck {
040
041    /**
042     * A key is pointing to the warning message text in "messages.properties"
043     * file.
044     */
045    public static final String MSG_KEY = "missing.super.call";
046
047    /** Stack of methods. */
048    private final Deque<MethodNode> methodStack = new LinkedList<>();
049
050    /**
051     * Returns the name of the overriding method.
052     *
053     * @return the name of the overriding method.
054     */
055    protected abstract String getMethodName();
056
057    @Override
058    public int[] getAcceptableTokens() {
059        return getRequiredTokens();
060    }
061
062    @Override
063    public int[] getDefaultTokens() {
064        return getRequiredTokens();
065    }
066
067    @Override
068    public int[] getRequiredTokens() {
069        return new int[] {
070            TokenTypes.METHOD_DEF,
071            TokenTypes.LITERAL_SUPER,
072        };
073    }
074
075    @Override
076    public void beginTree(DetailAST rootAST) {
077        methodStack.clear();
078    }
079
080    @Override
081    public void visitToken(DetailAST ast) {
082        if (isOverridingMethod(ast)) {
083            methodStack.add(new MethodNode(ast));
084        }
085        else if (isSuperCall(ast)) {
086            final MethodNode methodNode = methodStack.getLast();
087            methodNode.setCallingSuper();
088        }
089    }
090
091    /**
092     * Determines whether a 'super' literal is a call to the super method
093     * for this check.
094     *
095     * @param literalSuperAst the AST node of a 'super' literal.
096     * @return true if ast is a call to the super method for this check.
097     */
098    private boolean isSuperCall(DetailAST literalSuperAst) {
099        boolean superCall = false;
100
101        if (literalSuperAst.getType() == TokenTypes.LITERAL_SUPER) {
102            // dot operator?
103            final DetailAST dotAst = literalSuperAst.getParent();
104
105            if (!isSameNameMethod(literalSuperAst)
106                && !hasArguments(dotAst)) {
107                superCall = isSuperCallInOverridingMethod(dotAst);
108            }
109        }
110        return superCall;
111    }
112
113    /**
114     * Determines whether a super call in overriding method.
115     *
116     * @param ast The AST node of a 'dot operator' in 'super' call.
117     * @return true if super call in overriding method.
118     */
119    private boolean isSuperCallInOverridingMethod(DetailAST ast) {
120        boolean inOverridingMethod = false;
121        DetailAST dotAst = ast;
122
123        while (dotAst.getType() != TokenTypes.CTOR_DEF
124                && dotAst.getType() != TokenTypes.INSTANCE_INIT) {
125            if (dotAst.getType() == TokenTypes.METHOD_DEF) {
126                inOverridingMethod = isOverridingMethod(dotAst);
127                break;
128            }
129            dotAst = dotAst.getParent();
130        }
131        return inOverridingMethod;
132    }
133
134    /**
135     * Does method have any arguments.
136     *
137     * @param methodCallDotAst DOT DetailAST
138     * @return true if any parameters found
139     */
140    private static boolean hasArguments(DetailAST methodCallDotAst) {
141        final DetailAST argumentsList = methodCallDotAst.getNextSibling();
142        return argumentsList.hasChildren();
143    }
144
145    /**
146     * Is same name of method.
147     *
148     * @param ast method AST
149     * @return true if method name is the same
150     */
151    private boolean isSameNameMethod(DetailAST ast) {
152        DetailAST sibling = ast.getNextSibling();
153        // ignore type parameters
154        if (sibling != null
155            && sibling.getType() == TokenTypes.TYPE_ARGUMENTS) {
156            sibling = sibling.getNextSibling();
157        }
158        return sibling == null || !getMethodName().equals(sibling.getText());
159    }
160
161    @Override
162    public void leaveToken(DetailAST ast) {
163        if (isOverridingMethod(ast)) {
164            final MethodNode methodNode =
165                methodStack.removeLast();
166            if (!methodNode.isCallingSuper()) {
167                final DetailAST methodAST = methodNode.getMethod();
168                final DetailAST nameAST =
169                    methodAST.findFirstToken(TokenTypes.IDENT);
170                log(nameAST, MSG_KEY, nameAST.getText());
171            }
172        }
173    }
174
175    /**
176     * Determines whether an AST is a method definition for this check,
177     * without any parameters.
178     *
179     * @param ast the method definition AST.
180     * @return true if the method of ast is a method for this check.
181     */
182    private boolean isOverridingMethod(DetailAST ast) {
183        boolean overridingMethod = false;
184
185        if (ast.getType() == TokenTypes.METHOD_DEF
186                && !ScopeUtil.isInInterfaceOrAnnotationBlock(ast)) {
187            final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT);
188            final String name = nameAST.getText();
189            final DetailAST modifiersAST = ast.findFirstToken(TokenTypes.MODIFIERS);
190
191            if (getMethodName().equals(name)
192                    && modifiersAST.findFirstToken(TokenTypes.LITERAL_NATIVE) == null) {
193                final DetailAST params = ast.findFirstToken(TokenTypes.PARAMETERS);
194                overridingMethod = !params.hasChildren();
195            }
196        }
197        return overridingMethod;
198    }
199
200    /**
201     * Stack node for a method definition and a record of
202     * whether the method has a call to the super method.
203     */
204    private static class MethodNode {
205
206        /** Method definition. */
207        private final DetailAST method;
208
209        /** True if the overriding method calls the super method. */
210        private boolean callingSuper;
211
212        /**
213         * Constructs a stack node for a method definition.
214         *
215         * @param ast AST for the method definition.
216         */
217        /* package */ MethodNode(DetailAST ast) {
218            method = ast;
219            callingSuper = false;
220        }
221
222        /**
223         * Records that the overriding method has a call to the super method.
224         */
225        public void setCallingSuper() {
226            callingSuper = true;
227        }
228
229        /**
230         * Determines whether the overriding method has a call to the super
231         * method.
232         *
233         * @return true if the overriding method has a call to the super method.
234         */
235        public boolean isCallingSuper() {
236            return callingSuper;
237        }
238
239        /**
240         * Returns the overriding method definition AST.
241         *
242         * @return the overriding method definition AST.
243         */
244        public DetailAST getMethod() {
245            return method;
246        }
247
248    }
249
250}