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.sizes; 021 022import com.puppycrawl.tools.checkstyle.StatelessCheck; 023import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 024import com.puppycrawl.tools.checkstyle.api.DetailAST; 025import com.puppycrawl.tools.checkstyle.api.TokenTypes; 026 027/** 028 * <p> 029 * Checks for long anonymous inner classes. 030 * </p> 031 * <p> 032 * Rationale: If an anonymous inner class becomes very long 033 * it is hard to understand and to see the flow of the method 034 * where the class is defined. Therefore long anonymous inner 035 * classes should usually be refactored into a named inner class. 036 * See also Bloch, Effective Java, p. 93. 037 * </p> 038 * <ul> 039 * <li> 040 * Property {@code max} - Specify the maximum number of lines allowed. 041 * Type is {@code int}. 042 * Default value is {@code 20}. 043 * </li> 044 * </ul> 045 * <p> 046 * To configure the check to accept anonymous classes with up to 20 lines: 047 * </p> 048 * <pre> 049 * <module name="AnonInnerLength"/> 050 * </pre> 051 * <p> 052 * To configure the check to accept anonymous classes with up to 60 lines: 053 * </p> 054 * <pre> 055 * <module name="AnonInnerLength"> 056 * <property name="max" value="60"/> 057 * </module> 058 * </pre> 059 * <p> 060 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 061 * </p> 062 * <p> 063 * Violation Message Keys: 064 * </p> 065 * <ul> 066 * <li> 067 * {@code maxLen.anonInner} 068 * </li> 069 * </ul> 070 * 071 * @since 3.2 072 */ 073@StatelessCheck 074public class AnonInnerLengthCheck extends AbstractCheck { 075 076 /** 077 * A key is pointing to the warning message text in "messages.properties" 078 * file. 079 */ 080 public static final String MSG_KEY = "maxLen.anonInner"; 081 082 /** Default maximum number of lines. */ 083 private static final int DEFAULT_MAX = 20; 084 085 /** Specify the maximum number of lines allowed. */ 086 private int max = DEFAULT_MAX; 087 088 @Override 089 public int[] getDefaultTokens() { 090 return getRequiredTokens(); 091 } 092 093 @Override 094 public int[] getAcceptableTokens() { 095 return getRequiredTokens(); 096 } 097 098 @Override 099 public int[] getRequiredTokens() { 100 return new int[] {TokenTypes.LITERAL_NEW}; 101 } 102 103 @Override 104 public void visitToken(DetailAST ast) { 105 final DetailAST openingBrace = ast.findFirstToken(TokenTypes.OBJBLOCK); 106 if (openingBrace != null) { 107 final DetailAST closingBrace = 108 openingBrace.findFirstToken(TokenTypes.RCURLY); 109 final int length = 110 closingBrace.getLineNo() - openingBrace.getLineNo() + 1; 111 if (length > max) { 112 log(ast, MSG_KEY, length, max); 113 } 114 } 115 } 116 117 /** 118 * Setter to specify the maximum number of lines allowed. 119 * 120 * @param length the maximum length of an anonymous inner class. 121 */ 122 public void setMax(int length) { 123 max = length; 124 } 125 126}