001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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.coding; 021 022import java.util.BitSet; 023 024import com.puppycrawl.tools.checkstyle.StatelessCheck; 025import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 026import com.puppycrawl.tools.checkstyle.api.DetailAST; 027import com.puppycrawl.tools.checkstyle.api.TokenTypes; 028import com.puppycrawl.tools.checkstyle.utils.TokenUtil; 029 030/** 031 * <div> 032 * Detects double brace initialization. 033 * </div> 034 * 035 * <p> 036 * Rationale: Double brace initialization (set of 037 * <a href="https://docs.oracle.com/javase/specs/jls/se12/html/jls-8.html#jls-8.6"> 038 * Instance Initializers</a> in class body) may look cool, but it is considered as anti-pattern 039 * and should be avoided. 040 * This is also can lead to a hard-to-detect memory leak, if the anonymous class instance is 041 * returned outside and other object(s) hold reference to it. 042 * Created anonymous class is not static, it holds an implicit reference to the outer class 043 * instance. 044 * See this 045 * <a href="https://blog.jooq.org/dont-be-clever-the-double-curly-braces-anti-pattern/"> 046 * blog post</a> and 047 * <a href="https://www.baeldung.com/java-double-brace-initialization"> 048 * article</a> for more details. 049 * Check ignores any comments and semicolons in class body. 050 * </p> 051 * 052 * @since 8.30 053 */ 054@StatelessCheck 055public class AvoidDoubleBraceInitializationCheck extends AbstractCheck { 056 057 /** 058 * A key is pointing to the warning message text in "messages.properties" file. 059 */ 060 public static final String MSG_KEY = "avoid.double.brace.init"; 061 062 /** 063 * Set of token types that are ignored when checking class members. 064 */ 065 private static final BitSet IGNORED_TYPES = TokenUtil.asBitSet( 066 TokenTypes.INSTANCE_INIT, 067 TokenTypes.SEMI, 068 TokenTypes.LCURLY, 069 TokenTypes.RCURLY 070 ); 071 072 /** 073 * Creates a new {@code AvoidDoubleBraceInitializationCheck} instance. 074 */ 075 public AvoidDoubleBraceInitializationCheck() { 076 // no code by default 077 } 078 079 @Override 080 public int[] getDefaultTokens() { 081 return getRequiredTokens(); 082 } 083 084 @Override 085 public int[] getAcceptableTokens() { 086 return getRequiredTokens(); 087 } 088 089 @Override 090 public int[] getRequiredTokens() { 091 return new int[] {TokenTypes.OBJBLOCK}; 092 } 093 094 @Override 095 public void visitToken(DetailAST ast) { 096 if (ast.getParent().getType() == TokenTypes.LITERAL_NEW 097 && hasOnlyInitialization(ast)) { 098 log(ast, MSG_KEY); 099 } 100 } 101 102 /** 103 * Checks that block has at least one instance init block and no other class members. 104 * 105 * @param objBlock token to check 106 * @return true if there is at least one instance init block and no other class members, 107 * false otherwise 108 */ 109 private static boolean hasOnlyInitialization(DetailAST objBlock) { 110 final boolean hasInitBlock = objBlock.findFirstToken(TokenTypes.INSTANCE_INIT) != null; 111 return hasInitBlock 112 && TokenUtil.findFirstTokenByPredicate(objBlock, 113 AvoidDoubleBraceInitializationCheck::hasMember).isEmpty(); 114 } 115 116 /** 117 * Checks whether a token represents a class member other than initialization-related tokens. 118 * 119 * @param token the token to check 120 * @return true if the token represents a class member 121 */ 122 private static boolean hasMember(DetailAST token) { 123 return !IGNORED_TYPES.get(token.getType()); 124 } 125 126}