001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
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////////////////////////////////////////////////////////////////////////////////
019package com.puppycrawl.tools.checkstyle.checks.sizes;
020
021import com.puppycrawl.tools.checkstyle.api.Check;
022import com.puppycrawl.tools.checkstyle.api.DetailAST;
023import com.puppycrawl.tools.checkstyle.api.FileContents;
024import com.puppycrawl.tools.checkstyle.api.TokenTypes;
025
026/**
027 * <p>
028 * Checks for long methods.
029 * </p>
030 * <p>
031 * Rationale: If a method becomes very long it is hard to understand.
032 * Therefore long methods should usually be refactored into several
033 * individual methods that focus on a specific task.
034 * </p>
035 *<p>
036 * The default maximum method length is 150 lines. To change the maximum
037 * number of lines, set property max.
038 * </p>
039 * <p>
040 * An example of how to configure the check is:
041 * </p>
042 * <pre>
043 * &lt;module name="MethodLength"/&gt;
044 * </pre>
045 * <p>
046 * An example of how to configure the check so that it accepts methods with at
047 * most 60 lines is:
048 * </p>
049 * <pre>
050 * &lt;module name="MethodLength"&gt;
051 *    &lt;property name="max" value="60"/&gt;
052 * &lt;/module&gt;
053 * </pre>
054 * @author Lars Kühne
055 */
056public class MethodLengthCheck extends Check
057{
058    /** whether to ignore empty lines and single line comments */
059    private boolean countEmpty = true;
060
061    /** default maximum number of lines */
062    private static final int DEFAULT_MAX_LINES = 150;
063
064    /** the maximum number of lines */
065    private int max = DEFAULT_MAX_LINES;
066
067    @Override
068    public int[] getDefaultTokens()
069    {
070        return new int[] {TokenTypes.METHOD_DEF, TokenTypes.CTOR_DEF};
071    }
072
073    @Override
074    public void visitToken(DetailAST ast)
075    {
076        final DetailAST openingBrace = ast.findFirstToken(TokenTypes.SLIST);
077        if (openingBrace != null) {
078            final DetailAST closingBrace =
079                openingBrace.findFirstToken(TokenTypes.RCURLY);
080            int length =
081                closingBrace.getLineNo() - openingBrace.getLineNo() + 1;
082
083            if (!countEmpty) {
084                final FileContents contents = getFileContents();
085                final int lastLine = closingBrace.getLineNo();
086                for (int i = openingBrace.getLineNo() - 1; i < lastLine; i++) {
087                    if (contents.lineIsBlank(i) || contents.lineIsComment(i)) {
088                        length--;
089                    }
090                }
091            }
092            if (length > max) {
093                log(ast.getLineNo(), ast.getColumnNo(), "maxLen.method",
094                        length, max);
095            }
096        }
097    }
098
099    /**
100     * @param length the maximum length of a method.
101     */
102    public void setMax(int length)
103    {
104        max = length;
105    }
106
107    /**
108     * @param countEmpty whether to count empty and single line comments
109     * of the form //.
110     */
111    public void setCountEmpty(boolean countEmpty)
112    {
113        this.countEmpty = countEmpty;
114    }
115}