Skip to content

[java] Rule to check for double brace initialisation #1854

@oowekyala

Description

@oowekyala

Double brace initialisation is a pattern to initialise eg collections concisely. But it implicitly generates a new .class file, and the object holds a strong reference to the enclosing object...

Example:

 new ArrayList<String>() {{ addAll("a", "b", "c"); }};

I think, we could detect that for collections as a first step. But any instance initialiser in an anonymous class that calls only public methods of the object could be considered an instance of this code smell, if no methods are overridden.

Metadata

Metadata

Assignees

Labels

a:new-ruleProposal to add a new built-in rule

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions