001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2017 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.api.AbstractCheck; 023import com.puppycrawl.tools.checkstyle.api.DetailAST; 024import com.puppycrawl.tools.checkstyle.api.TokenTypes; 025 026/** 027 * <p> 028 * Checks for long anonymous inner classes. 029 * </p> 030 * <p> 031 * Rationale: If an anonymous inner class becomes very long 032 * it is hard to understand and to see the flow of the method 033 * where the class is defined. Therefore long anonymous inner 034 * classes should usually be refactored into a named inner class. 035 * See also Bloch, Effective Java, p. 93. 036 * </p> 037 * <p> 038 * The default maximum anonymous inner class length is 20 lines. 039 * To change the maximum number of lines, set property max. 040 * </p> 041 * <p> 042 * An example of how to configure the check is: 043 * </p> 044 * <pre> 045 * <module name="AnonInnerLength"/> 046 * </pre> 047 * <p> 048 * An example of how to configure the check so that it accepts anonymous 049 * inner classes with up to 60 lines is: 050 * </p> 051 * <pre> 052 * <module name="AnonInnerLength"> 053 * <property name="max" value="60"/> 054 * </module> 055 * </pre> 056 * 057 * @author Rob Worth 058 */ 059public class AnonInnerLengthCheck extends AbstractCheck { 060 061 /** 062 * A key is pointing to the warning message text in "messages.properties" 063 * file. 064 */ 065 public static final String MSG_KEY = "maxLen.anonInner"; 066 067 /** Default maximum number of lines. */ 068 private static final int DEFAULT_MAX = 20; 069 070 /** Maximum number of lines. */ 071 private int max = DEFAULT_MAX; 072 073 @Override 074 public int[] getDefaultTokens() { 075 return getAcceptableTokens(); 076 } 077 078 @Override 079 public int[] getAcceptableTokens() { 080 return new int[] {TokenTypes.LITERAL_NEW}; 081 } 082 083 @Override 084 public int[] getRequiredTokens() { 085 return getAcceptableTokens(); 086 } 087 088 @Override 089 public void visitToken(DetailAST ast) { 090 final DetailAST openingBrace = ast.findFirstToken(TokenTypes.OBJBLOCK); 091 if (openingBrace != null) { 092 final DetailAST closingBrace = 093 openingBrace.findFirstToken(TokenTypes.RCURLY); 094 final int length = 095 closingBrace.getLineNo() - openingBrace.getLineNo() + 1; 096 if (length > max) { 097 log(ast.getLineNo(), ast.getColumnNo(), MSG_KEY, 098 length, max); 099 } 100 } 101 } 102 103 /** 104 * Sets maximum length of an anonymous inner class. 105 * @param length the maximum length of an anonymous inner class. 106 */ 107 public void setMax(int length) { 108 max = length; 109 } 110}