JDK-8057794 : Compiler Error when obtaining .class property
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 8u20
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: linux_ubuntu
  • CPU: x86_64
  • Submitted: 2014-09-07
  • Updated: 2015-08-11
  • Resolved: 2014-09-22
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8 JDK 9
8u40Fixed 9 b33Fixed
Related Reports
Duplicate :  
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_20"
Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)


ADDITIONAL OS VERSION INFORMATION :
Linux grzpab-X550LC 3.13.0-35-generic #62-Ubuntu SMP Fri Aug 15 01:58:42 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

A DESCRIPTION OF THE PROBLEM :
If we have a generic interface and we use reflection on the parameterized type (I used Type.class.getSimpleName() ), the compiler will throw an exception. I bet it has something to do with the new functionality (default methods in interfaces) added in Java 8 (I wish I had more time to verify it, but it is 5 AM when I am writing this report).

REGRESSION.  Last worked in version 8u20

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Create a generic interface
2. The parameterized type should extend an interface
3. Create a default method
4. In the method, write a code using reflection on the parameterized type
5. Compile the code

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I was trying to achieve C++ behaviour - I thought I could get class information since the generic interface would be truly instantiated when declaring the exact type parameter - but my piece of code should not compile.
ACTUAL -
Included in the 'Error Message(s)' part.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
An exception has occurred in the compiler (1.8.0_20). 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.NullPointerException
	at com.sun.tools.javac.comp.DeferredAttr$DeferredChecker.quicklyResolveMethod(DeferredAttr.java:1333)
	at com.sun.tools.javac.comp.DeferredAttr$DeferredChecker.visitApply(DeferredAttr.java:1223)
	at com.sun.tools.javac.tree.JCTree$JCMethodInvocation.accept(JCTree.java:1465)
	at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
	at com.sun.tools.javac.comp.DeferredAttr$FilterScanner.scan(DeferredAttr.java:901)
	at com.sun.tools.javac.comp.DeferredAttr.isDeferred(DeferredAttr.java:1088)
	at com.sun.tools.javac.comp.Attr.attribArgs(Attr.java:701)
	at com.sun.tools.javac.comp.Attr.visitApply(Attr.java:1834)
	at com.sun.tools.javac.tree.JCTree$JCMethodInvocation.accept(JCTree.java:1465)
	at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:607)
	at com.sun.tools.javac.comp.Attr.attribExpr(Attr.java:656)
	at com.sun.tools.javac.comp.Attr.visitExec(Attr.java:1611)
	at com.sun.tools.javac.tree.JCTree$JCExpressionStatement.accept(JCTree.java:1296)
	at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:607)
	at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:676)
	at com.sun.tools.javac.comp.Attr.attribStats(Attr.java:692)
	at com.sun.tools.javac.comp.Attr.visitBlock(Attr.java:1142)
	at com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:909)
	at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:607)
	at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:676)
	at com.sun.tools.javac.comp.Attr.visitIf(Attr.java:1600)
	at com.sun.tools.javac.tree.JCTree$JCIf.accept(JCTree.java:1269)
	at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:607)
	at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:676)
	at com.sun.tools.javac.comp.Attr.attribStats(Attr.java:692)
	at com.sun.tools.javac.comp.Attr.visitBlock(Attr.java:1142)
	at com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:909)
	at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:607)
	at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:676)
	at com.sun.tools.javac.comp.Attr.visitMethodDef(Attr.java:1035)
	at com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:778)
	at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:607)
	at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:676)
	at com.sun.tools.javac.comp.Attr.attribClassBody(Attr.java:4342)
	at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4252)
	at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4181)
	at com.sun.tools.javac.comp.Attr.attrib(Attr.java:4156)
	at com.sun.tools.javac.main.JavaCompiler.attribute(JavaCompiler.java:1248)
	at com.sun.tools.javac.main.JavaCompiler.compile2(JavaCompiler.java:901)
	at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:860)
	at com.sun.tools.javac.main.Main.compile(Main.java:523)
	at com.sun.tools.javac.main.Main.compile(Main.java:381)
	at com.sun.tools.javac.main.Main.compile(Main.java:370)
	at com.sun.tools.javac.main.Main.compile(Main.java:361)
	at com.sun.tools.javac.Main.compile(Main.java:56)
	at com.sun.tools.javac.Main.main(Main.java:42)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
interface AnInterface extends java.io.Serializable { }

interface GenericInterface<SomeClass extends AnInterface>
{
default someMethod() { System.out.println(SomeClass.class.getSimpleName()); }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
I believe the code should not compile - the only solution would be not to use reflection inside a generic interface (I would say it happens since Java does not support reification - type arguments are erased upon compilation - we have a java.lang.Object instance). We could either used the methods of the ParametrizedType class or refactor the code (the second option is easier).


Comments
A simplified testcase that does not use default methods: --- class Test<T> { void t() { System.out.println(T.class.getSimpleName()); } } --- This is validly rejected with a compile-time error by 1.7.0_45-b35, but crashes with exceptions in both JDK 8 and JDK 8u20. The exceptions differ between these two, but the root cause appears to be the same. The exception from 1.8.0-b132 is: An exception has occurred in the compiler (1.8.0). 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.NullPointerException at com.sun.tools.javac.comp.DeferredAttr$DeferredChecker.visitApply(DeferredAttr.java:1108) at com.sun.tools.javac.tree.JCTree$JCMethodInvocation.accept(JCTree.java:1459) at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49) at com.sun.tools.javac.comp.DeferredAttr$FilterScanner.scan(DeferredAttr.java:762) at com.sun.tools.javac.comp.DeferredAttr.isDeferred(DeferredAttr.java:959) at com.sun.tools.javac.comp.Attr.attribArgs(Attr.java:690) at com.sun.tools.javac.comp.Attr.visitApply(Attr.java:1819) at com.sun.tools.javac.tree.JCTree$JCMethodInvocation.accept(JCTree.java:1459) at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:596) at com.sun.tools.javac.comp.Attr.attribExpr(Attr.java:645) at com.sun.tools.javac.comp.Attr.visitExec(Attr.java:1596) at com.sun.tools.javac.tree.JCTree$JCExpressionStatement.accept(JCTree.java:1290) at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:596) at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:665) at com.sun.tools.javac.comp.Attr.attribStats(Attr.java:681) at com.sun.tools.javac.comp.Attr.visitBlock(Attr.java:1127) at com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:903) at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:596) at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:665) at com.sun.tools.javac.comp.Attr.visitMethodDef(Attr.java:1020) at com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:772) at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:596) at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:665) at com.sun.tools.javac.comp.Attr.attribClassBody(Attr.java:4305) at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4215) at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4149) at com.sun.tools.javac.comp.Attr.attrib(Attr.java:4124) at com.sun.tools.javac.main.JavaCompiler.attribute(JavaCompiler.java:1251) at com.sun.tools.javac.main.JavaCompiler.compile2(JavaCompiler.java:904) at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:863) at com.sun.tools.javac.main.Main.compile(Main.java:523) at com.sun.tools.javac.main.Main.compile(Main.java:381) at com.sun.tools.javac.main.Main.compile(Main.java:370) at com.sun.tools.javac.main.Main.compile(Main.java:361) at com.sun.tools.javac.Main.compile(Main.java:56) at com.sun.tools.javac.Main.main(Main.java:42)
08-09-2014