JDK-8046410 : AssertionError compiling type annotation on class literal
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Priority: P4
  • Status: Resolved
  • Resolution: Cannot Reproduce
  • Submitted: 2014-06-10
  • Updated: 2015-02-06
  • Resolved: 2015-02-02
Related Reports
Relates :  
Description
Compiling this test code crashes:

import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;

public class test {
	public static void main(String argv[]) {
        new test().foo();
	}

	public void foo() {
        Class c = int @A [] .class;
      //  System.out.println(c);
	}
}

@Target(TYPE_USE)  @interface A {}

An exception has occurred in the compiler (1.9.0-ea). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport)  after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report.  Thank you.
java.lang.AssertionError
        at com.sun.tools.javac.util.Assert.error(Assert.java:126)
        at com.sun.tools.javac.util.Assert.checkNonNull(Assert.java:61)
        at com.sun.tools.javac.comp.Attr.fromAnnotations(Attr.java:4315)
        at com.sun.tools.javac.comp.Attr.access$500(Attr.java:75)
        at com.sun.tools.javac.comp.Attr$12.run(Attr.java:4299)
        at com.sun.tools.javac.comp.Annotate.flush(Annotate.java:163)
        at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4365)
        at com.sun.tools.javac.comp.Attr.attrib(Attr.java:4341)
        at com.sun.tools.javac.main.JavaCompiler.attribute(JavaCompiler.java:1242)
        at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:887)
        at com.sun.tools.javac.main.Main.compile(Main.java:541)
        at com.sun.tools.javac.main.Main.compile(Main.java:392)
        at com.sun.tools.javac.main.Main.compile(Main.java:381)
        at com.sun.tools.javac.main.Main.compile(Main.java:372)
        at com.sun.tools.javac.Main.compile(Main.java:56)
        at com.sun.tools.javac.Main.main(Main.java:42)


If the println is uncommented, this get an "scoping construct cannot be annotated with type-use annotation" error (why isn't it a compiler.err.no.annotations.on.dot.class error?).



Comments
Much water has flown under the bridge since the problem has been reported. On jdk1.8.0_40 this program compiles silently and so was it on JDK 9 dev until https://bugs.openjdk.java.net/browse/JDK-8027888 was resolved. At the moment, on JDK 9 dev I see an error message: no annotations are allowed in the type of a class literal Class c = int @A [] .class; ^ 1 error which is as it should be. On 8u stream it still compiles incorrectly, but this issue is not serious enough to warrant a back port.
02-02-2015

Notes: Annotation of a class literal is invalid, as per the JSR 308 spec. Javac's enforcement of this rule currently happens in the parser, and does not properly enforce array annotations on class literals. This needs to be fixed eventually, but the scope is too large for 8u20 at this point, considering what is already planned for backport. See JDK-8044455 for details. The referenced JCK tests are incorrect, as they invalid uses of type annotations to compile, and need to be updated.
10-06-2014