The supertype of anonymous class is expected to be a raw type

Dan Smith daniel.smith at
Thu Sep 3 19:51:39 UTC 2015

> On Aug 4, 2015, at 10:08 AM, Georgiy Rakov <georgiy.rakov at> wrote:
> So the conclusion I made earlier seems to be wrong now. However it looks strange that, say, constructor invocation type, determines anonymous class supertypes for diamond cases only. So could you please tell if this is really the intention of the spec. For instance code below outputs that just for diamond case anonymous class super class is not parameterized:
> When class type arguments are given, the super type is parameterized.
> When class type arguments and ctor type arguments are given, the super type is parameterized.
> When diamond is used, the super type is not parameterized.

Yep, that's right.  When diamond is involved, we have to erase the return type.  When type arguments are explicit, javac does not perform erasure.  Javac's behavior is probably correct, though the spec doesn't spell this out.  As I just noted in the thread "The type of instance creation expression with types arguments provided is not affected by", there's an existing bug to investigate the special treatment of constructors with explicit type arguments: JDK-8034925.

Note that this question isn't specific to anonymous classes -- below is a case where erasure happens for a diamond constructor invocation but not for one with explicit type arguments:

import java.util.List;

class ErasedDiamond<T> {

  ErasedDiamond(List<String> arg) {}
  List<String> getList() { return null; }

  static void test(List l) {
      new ErasedDiamond<String>(l).getList().get(0).length(); // no error -- constructor produces a ErasedDiamond<String>, 'getList' returns a List<String>
      new ErasedDiamond<>(l).getList().get(0).length(); // error -- constructor produces a raw ErasedDiamond, 'getList' returns a raw List



More information about the compiler-dev mailing list