Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ class NonStaticClassInitializerCheckSample {
static {
}

static class Inner {
{ // Noncompliant
}
}

{ // Noncompliant {{Move the contents of this initializer to a standard constructor or to field initializers.}}
//^
System.out.println();
Expand All @@ -14,7 +19,7 @@ public NonStaticClassInitializerCheckSample() {

new Runnable() {

{ // Noncompliant
{
System.out.println();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public List<Tree.Kind> nodesToVisit() {

@Override
public void visitNode(Tree tree) {
reportIssue(((BlockTree) tree).openBraceToken(), "Move the contents of this initializer to a standard constructor or to field initializers.");
if (!isMemberOfAnonymousClass(tree)) {
reportIssue(((BlockTree) tree).openBraceToken(), "Move the contents of this initializer to a standard constructor or to field initializers.");
}
}

private static boolean isMemberOfAnonymousClass(Tree tree) {
Tree parent = tree.parent();
return parent.is(Tree.Kind.CLASS) && parent.parent() != null && parent.parent().is(Tree.Kind.NEW_CLASS);
}
}
Loading