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

Method call on result of ternary conditional operator causes NoSuchMethodError

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Duplicate
    • Affects Version/s: 5.0
    • Fix Version/s: None
    • Component/s: tools
    • Labels:
    • Subcomponent:
    • CPU:
      x86
    • OS:
      linux

      Description

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


      ADDITIONAL OS VERSION INFORMATION :
      Red Hat Linux, release 9.0

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      Nothing considered noteworthy

      A DESCRIPTION OF THE PROBLEM :
      If the types of the two alternatives in a ? : -operator expression both implement two interfaces A and B, such that the type of the result is inferred as A & B, an attempt to call a method from the second interface generates a NoSuchMethodError to be thrown at runtime. No compile-time error or warning is issued. See the provided source.

      Calling a method from the first interface does not cause this problem. Without knowing too much about the implementation details of the JVM, my guess is that javac is correctly inferring that the result type supports multiple interfaces, but is not casting the result type correctly so as to be able to call the chosen method.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. compile test case
      2. attempt to execute "TestClass"
      3. observe NoSuchMethodError is thrown.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The code should execute with no problems, and no output generated.
      ACTUAL -
      A NoSuchMethodError stack trace is printed to the console.


      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      [davmac@bluej-linux davmac]$ java -cp . TestClass
      Exception in thread "main" java.lang.NoSuchMethodError: DavInt.davInt2Method()V
              at TestClass.main(TestClass.java:5)
      [davmac@bluej-linux davmac]$


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class TestClass implements DavInt, DavInt2
      {
          public static void main(String [] args)
          {
              (true ? new TestClass() : new TestClass2()).davInt2Method();
              // THE ABOVE LINE THROWS A NoSuchMethodError EXCEPTION
          }

          public void davInt2Method()
          {
          }
          
          public void davIntMethod()
          {
          }
      }
         
      class TestClass2 implements DavInt, DavInt2
      {
          public void davInt2Method()
          {
          }
          
          public void davIntMethod()
          {
          }
      }

      interface DavInt
      {
      void davIntMethod();
      }

      interface DavInt2
      {
      void davInt2Method();
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The obvious workaround is to avoid use of the ? : operator and use an "if () { } else { }" style construct instead.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                ahe Peter Ahe
                Reporter:
                ndcosta Nelson Dcosta (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: