Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8057794

Compiler Error when obtaining .class property

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      b33
    • CPU:
      x86_64
    • OS:
      linux_ubuntu
    • Verification:
      Verified

      Backports

        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).

          Attachments

            Issue Links

              Activity

                People

                Assignee:
                jlahoda Jan Lahoda
                Reporter:
                webbuggrp Webbug Group
                Votes:
                0 Vote for this issue
                Watchers:
                5 Start watching this issue

                  Dates

                  Created:
                  Updated:
                  Resolved: