[
https://issues.apache.org/jira/browse/GROOVY-11844?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18053122#comment-18053122
]
Eric Milles commented on GROOVY-11844:
--------------------------------------
This is a duplicate of GROOVY-7971, which has been addressed in Groovy 6. Can
you try the latest snapshot to see if it addresses your issue?
> Failed instanceof implies variable type incorrectly
> ---------------------------------------------------
>
> Key: GROOVY-11844
> URL: https://issues.apache.org/jira/browse/GROOVY-11844
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 4.0.29
> Reporter: Дилян Палаузов
> Priority: Major
>
> I use Groovy 4.0.29 as JSR223 plugin in openHAB. There Item.getState()
> returns interface org.openhab.core.types.State -
> https://www.openhab.org/javadoc/latest/org/openhab/core/types/state : The
> returned value could be StringType -
> [https://www.openhab.org/javadoc/latest/org/openhab/core/library/types/stringtype]
> - , UnDefType -
> [https://www.openhab.org/javadoc/latest/org/openhab/core/types/undeftype] ,
> something different .
> With this snippet, where ir.getItem("i1_Mode").getState().toString() is
> "B"/StringType, and getState() can return either UnDefType or StringType:
> {code:java}
> @groovy.transform.CompileStatic
> @groovy.transform.TypeChecked
> {
> State s = ir.getItem("i1_Mode").getState()
> logger.error("A0")
> if (s instanceof UnDefType)
> logger.error("A1")
> logger.error("A2")
> if (s instanceof UnDefType || "S".equals(s.toString()))
> logger.error("A3")
> logger.error("A4")
> if ("S".equals(s.toString()) || s instanceof UnDefType)
> logger.error("A5")
> logger.error("A6")
> }{code}
> the system logs:
> {quote}A0
> A2
> Failed to execute action: 1(Cannot cast object 'B' with class
> 'org.openhab.core.library.types.StringType' to class
> 'org.openhab.core.types.UnDefType')
> {quote}
> If I remove “if (s instanceof UnDefType || "S".equals(s.toString()))“, that
> is, if I switch the parameters of ||, it works:
>
> {code:java}
> @groovy.transform.CompileStatic
> @groovy.transform.TypeChecked
> {
> State s = ir.getItem("i1_Mode").getState()
> logger.error("A0")
> if (s instanceof UnDefType)
> logger.error("A1")
> logger.error("A2")
> if ("S".equals(s.toString()) || s instanceof UnDefType)
> logger.error("A5")
> logger.error("A6")
> }
> {code}
> the output is A0, A2, A6.
> My assumption is that after failed instanceof Groovy assumes the type of the
> variable, as if instanceof has not failed, and then comparing to other types
> does not work.
>
--
This message was sent by Atlassian Jira
(v8.20.10#820010)