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 * Default value is {@code 20}. 042 * </li> 043 * </ul> 044 * <p> 045 * To configure the check to accept anonymous classes with up to 20 lines: 046 * </p> 047 * <pre> 048 * <module name="AnonInnerLength"/> 049 * </pre> 050 * <p> 051 * To configure the check to accept anonymous classes with up to 60 lines: 052 * </p> 053 * <pre> 054 * <module name="AnonInnerLength"> 055 * <property name="max" value="60"/> 056 * </module> 057 * </pre> 058 * 059 * @since 3.2 060 */ 061@StatelessCheck 062public class AnonInnerLengthCheck extends AbstractCheck { 063 064 /** 065 * A key is pointing to the warning message text in "messages.properties" 066 * file. 067 */ 068 public static final String MSG_KEY = "maxLen.anonInner"; 069 070 /** Default maximum number of lines. */ 071 private static final int DEFAULT_MAX = 20; 072 073 /** Specify the maximum number of lines allowed. */ 074 private int max = DEFAULT_MAX; 075 076 @Override 077 public int[] getDefaultTokens() { 078 return getRequiredTokens(); 079 } 080 081 @Override 082 public int[] getAcceptableTokens() { 083 return getRequiredTokens(); 084 } 085 086 @Override 087 public int[] getRequiredTokens() { 088 return new int[] {TokenTypes.LITERAL_NEW}; 089 } 090 091 @Override 092 public void visitToken(DetailAST ast) { 093 final DetailAST openingBrace = ast.findFirstToken(TokenTypes.OBJBLOCK); 094 if (openingBrace != null) { 095 final DetailAST closingBrace = 096 openingBrace.findFirstToken(TokenTypes.RCURLY); 097 final int length = 098 closingBrace.getLineNo() - openingBrace.getLineNo() + 1; 099 if (length > max) { 100 log(ast, MSG_KEY, length, max); 101 } 102 } 103 } 104 105 /** 106 * Setter to specify the maximum number of lines allowed. 107 * 108 * @param length the maximum length of an anonymous inner class. 109 */ 110 public void setMax(int length) { 111 max = length; 112 } 113 114}