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

Invalid reference to anonymous class in ?: operator with cast and a null value.

    XMLWordPrintable

Details

    • Bug
    • Resolution: Duplicate
    • P3
    • None
    • 1.4.2
    • tools
    • x86
    • linux

    Description

      FULL PRODUCT VERSION :
      java version "1.4.2_03"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_03-b02)
      Java HotSpot(TM) Client VM (build 1.4.2_03-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Linux xxx 2.6.4-52-default #1 Wed Apr 7 02:08:30 UTC 2004 i686 athlon i386 GNU/Linux
      SuSE 9.1

      A DESCRIPTION OF THE PROBLEM :
      Using a ?: operator with a constant as the condition, and an instanciation of an anonymous class and a null value as clauses causes the anonymous class not to be found at runtime if the resulting evaluation is the null value.

      Note: this used to work in previous versions of the Java compiler (1.2.x) because there was no optimisation to remove unused anonymous classes.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and execute the source provided.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The program should compile and execute without errors.
      ACTUAL -
      The program crashes with a NoClassDefFoundError when executed.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" java.lang.NoClassDefFoundError: Test1$1

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public final class Test1
        {
          private static final boolean DO_BUG=true;

            // Workaround: cast null to Base
          private static Base base= DO_BUG?
            //(Base)null
            null
              :
            new Base()
              {
                public final String test()
                {
                  return("anonymous");
                }
              };
          
          private Test1() {}

          public static void main(String[] argv)
          {
            if(base==null)
                   System.out.println("no base");
              else System.out.println(base.test());
          }

          private static abstract class Base
            {
              public Base() {}

              public abstract String test();
            }
        }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The null value (or the instanciation) must be casted to the class of the variable.
      ###@###.### 2005-03-16 12:35:35 GMT

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              ndcosta Nelson Dcosta (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: