From c98cd596be3209ac3b29fc89278b1493c82068ff Mon Sep 17 00:00:00 2001 From: Tyler Kindy Date: Wed, 5 Jun 2024 15:48:25 -0400 Subject: [PATCH 1/2] Add failing test --- .../ClassInitializationDeadlockTest.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/ClassInitializationDeadlockTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/ClassInitializationDeadlockTest.java index bce0b5f64a0..b1c51f7f1d1 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/ClassInitializationDeadlockTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/ClassInitializationDeadlockTest.java @@ -296,4 +296,17 @@ public void nestedInterface() { "}") .doTest(); } + + @Test + public void nonNestedSubclass() { + testHelper + .addSourceLines( + "Foo.java", + "class A {", + " // BUG: Diagnostic contains:", + " private static Object cycle = new B();", + "}", + "class B extends A {}") + .doTest(); + } } From c5cc883d515bc738a22dd3265faa8e117443ffbf Mon Sep 17 00:00:00 2001 From: Tyler Kindy Date: Wed, 5 Jun 2024 16:10:36 -0400 Subject: [PATCH 2/2] Check if inner class is enclosed by the class it extends --- .../errorprone/bugpatterns/ClassInitializationDeadlock.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/ClassInitializationDeadlock.java b/core/src/main/java/com/google/errorprone/bugpatterns/ClassInitializationDeadlock.java index de8ca29da8c..ea569ddb2c3 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/ClassInitializationDeadlock.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/ClassInitializationDeadlock.java @@ -144,7 +144,7 @@ private void handle(ExpressionTree tree) { if (!use.isSubClass(classSymbol, state.getTypes())) { return; } - if (!isStatic(use)) { + if (use.isEnclosedBy(classSymbol) && !isStatic(use)) { // Nested inner classes implicitly take the enclosing instance as a constructor parameter, // and can't be initialized without first initializing their containing class. return;