You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2023/01/14 01:54:00 UTC

[jira] [Commented] (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

    [ https://issues.apache.org/jira/browse/GROOVY-10903?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17676819#comment-17676819 ] 

Eric Milles commented on GROOVY-10903:
--------------------------------------

I think this is a duplicate of 7971

> 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.10, 3.0.11
>            Reporter: Dmitry
>            Priority: Critical
>         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)