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.TokenTypes;
024
025/**
026 * <p>
027 * Checks for long anonymous inner classes.
028 * </p>
029 * <p>
030 * Rationale: If an anonymous inner class becomes very long
031 * it is hard to understand and to see the flow of the method
032 * where the class is defined. Therefore long anonymous inner
033 * classes should usually be refactored into a named inner class.
034 * See also Bloch, Effective Java, p. 93.
035 * </p>
036 * <p>
037 * The default maximum anonymous inner class length is 20 lines.
038 * To change the maximum number of lines, set property max.
039 * </p>
040 * <p>
041 * An example of how to configure the check is:
042 * </p>
043 * <pre>
044 * &lt;module name="AnonInnerLength"/&gt;
045 * </pre>
046 * <p>
047 * An example of how to configure the check so that it accepts anonymous
048 * inner classes with up to 60 lines is:
049 * </p>
050 * <pre>
051 * &lt;module name="AnonInnerLength"&gt;
052 *    &lt;property name="max" value="60"/&gt;
053 * &lt;/module&gt;
054 * </pre>
055 *
056 * @author Rob Worth
057 */
058public class AnonInnerLengthCheck extends Check
059{
060    /** default maximum number of lines */
061    private static final int DEFAULT_MAX = 20;
062
063    /** maximum number of lines */
064    private int max = DEFAULT_MAX;
065
066    @Override
067    public int[] getDefaultTokens()
068    {
069        return new int[] {TokenTypes.LITERAL_NEW};
070    }
071
072    @Override
073    public void visitToken(DetailAST ast)
074    {
075        final DetailAST openingBrace = ast.findFirstToken(TokenTypes.OBJBLOCK);
076        if (openingBrace != null) {
077            final DetailAST closingBrace =
078                openingBrace.findFirstToken(TokenTypes.RCURLY);
079            final int length =
080                closingBrace.getLineNo() - openingBrace.getLineNo() + 1;
081            if (length > max) {
082                log(ast.getLineNo(), ast.getColumnNo(), "maxLen.anonInner",
083                        length, max);
084            }
085        }
086    }
087
088
089    /**
090     * @param length the maximum length of an anonymous inner class.
091     */
092    public void setMax(int length)
093    {
094        max = length;
095    }
096}