RFR: 8361214: An anonymous class is erroneously being classify as an abstract class

Vicente Romero vromero at openjdk.org
Wed Jul 2 18:06:24 UTC 2025


Javac is failing to compile this code:

abstract class Base<T> {}
abstract class Derived1<T> extends Base<T> {}
abstract class Derived2<T> extends Base<T> {
    Derived2(Derived1<T> obj) {}
}

class Test {
    Base<String> obj = new Derived2<>(new Derived1<>() { /* not abstract */ }) { /* not abstract */ };
}

issuing error message:

Test.java:8: error: Derived1 is abstract; cannot be instantiated
    Base<String> obj = new Derived2<>(new Derived1<>() { /* not abstract */ }) { /* not abstract */ };

basically during speculative attribution we make a copy of the new class expression but the class body is nuked to avoid side-effects during the process. Javac is checking the error condition on a speculative version of the AST and thus it seems to it that the user is trying to create an abstract class. The proposed fix is to use an already existing predicate that indicates if the class body has been removed or not,

TIA

-------------

Commit messages:
 - adding test case
 - minor change to test
 - Merge branch 'master' into JDK-8361214
 - 8361214: An anonymous class is erroneously being classify as an abstract class

Changes: https://git.openjdk.org/jdk/pull/26103/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=26103&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8361214
  Stats: 45 lines in 2 files changed: 44 ins; 0 del; 1 mod
  Patch: https://git.openjdk.org/jdk/pull/26103.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/26103/head:pull/26103

PR: https://git.openjdk.org/jdk/pull/26103


More information about the compiler-dev mailing list