Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-10903

Cannot cast object 'A a' with class 'A' to class 'B'. Build file try to cast when use 'instanceof' and 'or' operator (||) in 'if' condition

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Critical
    • Resolution: Duplicate
    • 3.0.10, 3.0.11
    • None
    • None

    Description

      Hi team!

      I have 2 classes:

      • Invoice extends AbstractInvoice
      • Quote extends AbstractInvoice

      And I try to run 

      AbstractInvoice abstractInvoice = new Invoice()
      
      if (abstractInvoice.newRecord || abstractInvoice instanceof Quote) {
          abstractInvoice.setContact(new Contact())
      } 

      I get "Exception in thread "main" org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'groov.Invoice@3f2a3a5' with class 'groov.Invoice' to class 'groov.Quote'"

       

      I investigated build files and I saw this:

      Invoice abstractInvoice = new Invoice();
      Object var10000;
      if (((Invoice)abstractInvoice).isNewRecord() || abstractInvoice instanceof Quote) {
          ((Quote)ScriptBytecodeAdapter.castToType(abstractInvoice, Quote.class)).setContact(new Contact());
          var10000 = null;
      } 

      Groovy sees 'abstractInvoice instanceof Quote' and tries to cast Class to Quote. But It forgets about || condition.

      In our project we use 2.5.15 version and we upgrading it to 4.0.2 version. After investigation I found that this behavior appeared in 3.0.11 version. 3.0.10 and older work correct.

      If I use Main Java class it works correct as well.

      I localized issue here (minimum classes and dependancies): ErrorWithCast.zip

      Part from our project(more inheritance): [^TestJavaGroovyGradleProject 2.zip][^TestJavaGroovyGradleProject.zip]

      Attachments

        1. TestJavaGroovyGradleProject-1.zip
          149 kB
          Dmitry
        2. ErrorWithCast.zip
          102 kB
          Dmitry

        Issue Links

          Activity

            People

              Unassigned Unassigned
              Litvinko Dmitry
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: