001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 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.ArrayDeque;
023import java.util.Deque;
024import java.util.regex.Pattern;
025
026import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
027import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
028import com.puppycrawl.tools.checkstyle.api.DetailAST;
029import com.puppycrawl.tools.checkstyle.api.TokenTypes;
030
031/**
032 * <p>
033 * Restricts the number of return statements in methods, constructors and lambda expressions
034 * (2 by default). Ignores specified methods ({@code equals()} by default).
035 * </p>
036 * <p>
037 * <b>max</b> property will only check returns in methods and lambdas that
038 * return a specific value (Ex: 'return 1;').
039 * </p>
040 * <p>
041 * <b>maxForVoid</b> property will only check returns in methods, constructors,
042 * and lambdas that have no return type (IE 'return;'). It will only count
043 * visible return statements. Return statements not normally written, but
044 * implied, at the end of the method/constructor definition will not be taken
045 * into account. To disallow "return;" in void return type methods, use a value
046 * of 0.
047 * </p>
048 * <p>
049 * Rationale: Too many return points can be indication that code is
050 * attempting to do too much or may be difficult to understand.
051 * </p>
052 *
053 * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
054 */
055@FileStatefulCheck
056public final class ReturnCountCheck extends AbstractCheck {
057
058    /**
059     * A key is pointing to the warning message text in "messages.properties"
060     * file.
061     */
062    public static final String MSG_KEY = "return.count";
063
064    /** Stack of method contexts. */
065    private final Deque<Context> contextStack = new ArrayDeque<>();
066
067    /** The regexp to match against. */
068    private Pattern format = Pattern.compile("^equals$");
069
070    /** Maximum allowed number of return statements. */
071    private int max = 2;
072    /** Maximum allowed number of return statements for void methods. */
073    private int maxForVoid = 1;
074    /** Current method context. */
075    private Context context;
076
077    @Override
078    public int[] getDefaultTokens() {
079        return new int[] {
080            TokenTypes.CTOR_DEF,
081            TokenTypes.METHOD_DEF,
082            TokenTypes.LAMBDA,
083            TokenTypes.LITERAL_RETURN,
084        };
085    }
086
087    @Override
088    public int[] getRequiredTokens() {
089        return new int[] {TokenTypes.LITERAL_RETURN};
090    }
091
092    @Override
093    public int[] getAcceptableTokens() {
094        return new int[] {
095            TokenTypes.CTOR_DEF,
096            TokenTypes.METHOD_DEF,
097            TokenTypes.LAMBDA,
098            TokenTypes.LITERAL_RETURN,
099        };
100    }
101
102    /**
103     * Set the format for the specified regular expression.
104     * @param pattern a pattern.
105     */
106    public void setFormat(Pattern pattern) {
107        format = pattern;
108    }
109
110    /**
111     * Setter for max property.
112     * @param max maximum allowed number of return statements.
113     */
114    public void setMax(int max) {
115        this.max = max;
116    }
117
118    /**
119     * Setter for maxForVoid property.
120     * @param maxForVoid maximum allowed number of return statements for void methods.
121     */
122    public void setMaxForVoid(int maxForVoid) {
123        this.maxForVoid = maxForVoid;
124    }
125
126    @Override
127    public void beginTree(DetailAST rootAST) {
128        context = new Context(false);
129        contextStack.clear();
130    }
131
132    @Override
133    public void visitToken(DetailAST ast) {
134        switch (ast.getType()) {
135            case TokenTypes.CTOR_DEF:
136            case TokenTypes.METHOD_DEF:
137                visitMethodDef(ast);
138                break;
139            case TokenTypes.LAMBDA:
140                visitLambda();
141                break;
142            case TokenTypes.LITERAL_RETURN:
143                visitReturn(ast);
144                break;
145            default:
146                throw new IllegalStateException(ast.toString());
147        }
148    }
149
150    @Override
151    public void leaveToken(DetailAST ast) {
152        switch (ast.getType()) {
153            case TokenTypes.CTOR_DEF:
154            case TokenTypes.METHOD_DEF:
155            case TokenTypes.LAMBDA:
156                leave(ast);
157                break;
158            case TokenTypes.LITERAL_RETURN:
159                // Do nothing
160                break;
161            default:
162                throw new IllegalStateException(ast.toString());
163        }
164    }
165
166    /**
167     * Creates new method context and places old one on the stack.
168     * @param ast method definition for check.
169     */
170    private void visitMethodDef(DetailAST ast) {
171        contextStack.push(context);
172        final DetailAST methodNameAST = ast.findFirstToken(TokenTypes.IDENT);
173        final boolean check = !format.matcher(methodNameAST.getText()).find();
174        context = new Context(check);
175    }
176
177    /**
178     * Checks number of return statements and restore previous context.
179     * @param ast node to leave.
180     */
181    private void leave(DetailAST ast) {
182        context.checkCount(ast);
183        context = contextStack.pop();
184    }
185
186    /**
187     * Creates new lambda context and places old one on the stack.
188     */
189    private void visitLambda() {
190        contextStack.push(context);
191        context = new Context(true);
192    }
193
194    /**
195     * Examines the return statement and tells context about it.
196     * @param ast return statement to check.
197     */
198    private void visitReturn(DetailAST ast) {
199        // we can't identify which max to use for lambdas, so we can only assign
200        // after the first return statement is seen
201        if (ast.getFirstChild().getType() == TokenTypes.SEMI) {
202            context.visitLiteralReturn(maxForVoid);
203        }
204        else {
205            context.visitLiteralReturn(max);
206        }
207    }
208
209    /**
210     * Class to encapsulate information about one method.
211     * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
212     */
213    private class Context {
214        /** Whether we should check this method or not. */
215        private final boolean checking;
216        /** Counter for return statements. */
217        private int count;
218        /** Maximum allowed number of return statements. */
219        private Integer maxAllowed;
220
221        /**
222         * Creates new method context.
223         * @param checking should we check this method or not.
224         */
225        Context(boolean checking) {
226            this.checking = checking;
227        }
228
229        /**
230         * Increase the number of return statements.
231         * @param maxAssigned Maximum allowed number of return statements.
232         */
233        public void visitLiteralReturn(int maxAssigned) {
234            if (maxAllowed == null) {
235                maxAllowed = maxAssigned;
236            }
237
238            ++count;
239        }
240
241        /**
242         * Checks if number of return statements in the method are more
243         * than allowed.
244         * @param ast method def associated with this context.
245         */
246        public void checkCount(DetailAST ast) {
247            if (checking && maxAllowed != null && count > maxAllowed) {
248                log(ast.getLineNo(), ast.getColumnNo(), MSG_KEY, count, maxAllowed);
249            }
250        }
251    }
252}