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

Cannot cast to non-static inner class of generic outer class

XMLWordPrintable

    • x86
    • linux

      FULL PRODUCT VERSION :
      java version "1.6.0_18"
      OpenJDK Runtime Environment (IcedTea6 1.8) (6b18-1.8-0ubuntu1)
      OpenJDK 64-Bit Server VM (build 14.0-b16, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux antigua 2.6.32-22-generic #36-Ubuntu SMP Thu Jun 3 19:31:57 UTC 2010 x86_64 GNU/Linux


      EXTRA RELEVANT SYSTEM CONFIGURATION :
      Using Ubuntu 10.04 with Java from package openjdk-6-jdk version 6b18-1.8-0ubuntu1

      A DESCRIPTION OF THE PROBLEM :
      The java compiler produces an error message when casting an object to a type, which is a non-static inner class of a generic outer class (e.g Test<?>.Inner). The same type is valid for variable declarations and for instanceof expression.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile the supplied java code with javac.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      a .class file
      ACTUAL -
      three error messages about syntax errors

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Test.java:12: ')' expected
                  i = (Test<?>.Inner) o;
                              ^
      Test.java:12: ';' expected
                  i = (Test<?>.Inner) o;
                               ^
      Test.java:12: illegal start of expression
                  i = (Test<?>.Inner) o;
                                    ^
      3 errors

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      class Test<T> {
          
          class Inner {}
          
          public static void main(String args[]) {
              Test<?> t = new Test<Object>();
              
              Object o = t.new Inner();
              
              Test<?>.Inner i;
              if (o instanceof Test<?>.Inner) {
                  i = (Test<?>.Inner) o;
              }
          }
      }
      ---------- END SOURCE ----------

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: