Dmitry created GROOVY-10903:
-------------------------------

             Summary: 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
                 Key: GROOVY-10903
                 URL: https://issues.apache.org/jira/browse/GROOVY-10903
             Project: Groovy
          Issue Type: Bug
          Components: Compiler, Static compilation
    Affects Versions: 3.0.11, 3.0.10
            Reporter: Dmitry
         Attachments: ErrorWithCast.zip, TestJavaGroovyGradleProject-1.zip

Hi team!


I have 2 classes:
 * Invoice extends AbstractInvoice
 * Quote extends AbstractInvoice

And I try to run 
{code:java}
AbstractInvoice abstractInvoice = new Invoice()

if (abstractInvoice.newRecord || abstractInvoice instanceof Quote) {
    abstractInvoice.setContact(new Contact())
} {code}
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:
{code:java}
Invoice abstractInvoice = new Invoice();
Object var10000;
if (((Invoice)abstractInvoice).isNewRecord() || abstractInvoice instanceof 
Quote) {
    ((Quote)ScriptBytecodeAdapter.castToType(abstractInvoice, 
Quote.class)).setContact(new Contact());
    var10000 = null;
} {code}
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]



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to