| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| AnonInnerLengthCheck |
|
| 1.6666666666666667;1.667 |
| 1 | //////////////////////////////////////////////////////////////////////////////// | |
| 2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
| 3 | // Copyright (C) 2001-2014 Oliver Burn | |
| 4 | // | |
| 5 | // This library is free software; you can redistribute it and/or | |
| 6 | // modify it under the terms of the GNU Lesser General Public | |
| 7 | // License as published by the Free Software Foundation; either | |
| 8 | // version 2.1 of the License, or (at your option) any later version. | |
| 9 | // | |
| 10 | // This library is distributed in the hope that it will be useful, | |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 | // Lesser General Public License for more details. | |
| 14 | // | |
| 15 | // You should have received a copy of the GNU Lesser General Public | |
| 16 | // License along with this library; if not, write to the Free Software | |
| 17 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 18 | //////////////////////////////////////////////////////////////////////////////// | |
| 19 | package com.puppycrawl.tools.checkstyle.checks.sizes; | |
| 20 | ||
| 21 | import com.puppycrawl.tools.checkstyle.api.Check; | |
| 22 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
| 23 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
| 24 | ||
| 25 | /** | |
| 26 | * <p> | |
| 27 | * Checks for long anonymous inner classes. | |
| 28 | * </p> | |
| 29 | * <p> | |
| 30 | * Rationale: If an anonymous inner class becomes very long | |
| 31 | * it is hard to understand and to see the flow of the method | |
| 32 | * where the class is defined. Therefore long anonymous inner | |
| 33 | * classes should usually be refactored into a named inner class. | |
| 34 | * See also Bloch, Effective Java, p. 93. | |
| 35 | * </p> | |
| 36 | * <p> | |
| 37 | * The default maximum anonymous inner class length is 20 lines. | |
| 38 | * To change the maximum number of lines, set property max. | |
| 39 | * </p> | |
| 40 | * <p> | |
| 41 | * An example of how to configure the check is: | |
| 42 | * </p> | |
| 43 | * <pre> | |
| 44 | * <module name="AnonInnerLength"/> | |
| 45 | * </pre> | |
| 46 | * <p> | |
| 47 | * An example of how to configure the check so that it accepts anonymous | |
| 48 | * inner classes with up to 60 lines is: | |
| 49 | * </p> | |
| 50 | * <pre> | |
| 51 | * <module name="AnonInnerLength"> | |
| 52 | * <property name="max" value="60"/> | |
| 53 | * </module> | |
| 54 | * </pre> | |
| 55 | * | |
| 56 | * @author Rob Worth | |
| 57 | */ | |
| 58 | 2 | public class AnonInnerLengthCheck extends Check |
| 59 | { | |
| 60 | /** default maximum number of lines */ | |
| 61 | private static final int DEFAULT_MAX = 20; | |
| 62 | ||
| 63 | /** maximum number of lines */ | |
| 64 | 2 | private int mMax = DEFAULT_MAX; |
| 65 | ||
| 66 | @Override | |
| 67 | public int[] getDefaultTokens() | |
| 68 | { | |
| 69 | 2 | return new int[] {TokenTypes.LITERAL_NEW}; |
| 70 | } | |
| 71 | ||
| 72 | @Override | |
| 73 | public void visitToken(DetailAST aAST) | |
| 74 | { | |
| 75 | 14 | final DetailAST openingBrace = aAST.findFirstToken(TokenTypes.OBJBLOCK); |
| 76 | 14 | if (openingBrace != null) { |
| 77 | 8 | final DetailAST closingBrace = |
| 78 | openingBrace.findFirstToken(TokenTypes.RCURLY); | |
| 79 | 8 | final int length = |
| 80 | closingBrace.getLineNo() - openingBrace.getLineNo() + 1; | |
| 81 | 8 | if (length > mMax) { |
| 82 | 3 | log(aAST.getLineNo(), aAST.getColumnNo(), "maxLen.anonInner", |
| 83 | length, mMax); | |
| 84 | } | |
| 85 | } | |
| 86 | 14 | } |
| 87 | ||
| 88 | ||
| 89 | /** | |
| 90 | * @param aLength the maximum length of an anonymous inner class. | |
| 91 | */ | |
| 92 | public void setMax(int aLength) | |
| 93 | { | |
| 94 | 1 | mMax = aLength; |
| 95 | 1 | } |
| 96 | } |