Дилян Палаузов created GROOVY-11844:
---------------------------------------
Summary: 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: Дилян Палаузов
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)