[ https://issues.apache.org/jira/browse/GROOVY-9270?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Tommy Svensson updated GROOVY-9270: ----------------------------------- Description: Using "instanceof boolean" or using a Trait will cause the compiled code to not work with OSGi/maven-bundle-plugin/bnd. Edited text: I made 2 test classes one Ok and one that bnd says has code in default package: OK: public void test(Object o) { CallSite[] var2 = $getCallSiteArray(); if (o instanceof Boolean) { var2[0].callCurrent(this, "boolean"); } } Fail: public void test(Object o) { CallSite[] var2 = $getCallSiteArray(); if (o instanceof boolean) { var2[0].callCurrent(this, "boolean"); } } The rest of the class is identical. These comes from decompiled .class files and thus are Java, but they were compile from groovy classes. Now the question is, where do java handle autoboxing ? If it is the compiler then this would explain the result. The "boolean" is a primitive and have no package. If the compiler is responsible for handling this, the problem lies with Groovy which has clearly not boxed the "boolean" in a "Boolean". If its the JDK that handles autoboxing runtime then both cases should behave the same, which they are not. Bnd ([https://bnd.bndtools.org)|https://bnd.bndtools.org)/] tries to import from "." in the "Fail" case and reports: "The default package '.' is not permitted by the Import-Package syntax." I took these 2 test groovy files and moved them to a Java maven module, changed .groovy to .java, added ;, etc to make them valid java. Java will not compile the fail case complaining about "o instanceof boolean" no being valid. "Inconvertible types, cannot cast boolean to Object" is the message you get. I think that Groovy should report the same compilation error. was: Using "instanceof boolean" or using a Trait will cause the compiled code to not work with OSGi/maven-bundle-plugin/bnd. The "if ( val instanceof boolean)..." should cause a compiler error since "boolean" is not an object, and clearly not autoboxed here. This compiles fine and produced a .class file. maven-bundle-plugin or possibly Bnd used under the surface will however have nothing to do with this class since Groovy has produced some strange code. m-b-p/bnd complains about code in default package in this case. Making the "boolean" a "Boolean" makes the problem go away. But the first one is wrong, it should not compile, but cause an error. Groovy does not produce valid code for this case. Using a "trait" has exactly the same effect as described above. m-b-p/bnd complains about code in default package. If that is a side effect of using traits then traits cannot be used in an OSGi environment. This feels more of a less than optimal implementation of traits in groovy. I will not call this a bug :). The instanceof case however feels like a bug. > Using "instanceof boolean" or using a Trait will cause the compiled code to > not work with OSGi/maven-bundle-plugin/bnd > ----------------------------------------------------------------------------------------------------------------------- > > Key: GROOVY-9270 > URL: https://issues.apache.org/jira/browse/GROOVY-9270 > Project: Groovy > Issue Type: Bug > Components: class generator, Compiler > Environment: OSX (Mojave) JDK 11. > Reporter: Tommy Svensson > Priority: Major > Time Spent: 20m > Remaining Estimate: 0h > > Using "instanceof boolean" or using a Trait will cause the compiled code to > not work with OSGi/maven-bundle-plugin/bnd. > Edited text: > I made 2 test classes one Ok and one that bnd says has code in default > package: > OK: > public void test(Object o) { > CallSite[] var2 = $getCallSiteArray(); > if (o instanceof Boolean) { > var2[0].callCurrent(this, "boolean"); > } > } > Fail: > public void test(Object o) { > CallSite[] var2 = $getCallSiteArray(); > if (o instanceof boolean) { > var2[0].callCurrent(this, "boolean"); > } > } > The rest of the class is identical. > > These comes from decompiled .class files and thus are Java, but they were > compile from groovy classes. Now the question is, where do java handle > autoboxing ? If it is the compiler then this would explain the result. The > "boolean" is a primitive and have no package. > If the compiler is responsible for handling this, the problem lies with > Groovy which has clearly not boxed the "boolean" in a "Boolean". > If its the JDK that handles autoboxing runtime then both cases should behave > the same, which they are not. > Bnd ([https://bnd.bndtools.org)|https://bnd.bndtools.org)/] tries to import > from "." in the "Fail" case and reports: "The default package '.' is not > permitted by the Import-Package syntax." > I took these 2 test groovy files and moved them to a Java maven module, > changed .groovy to .java, added ;, etc to make them valid java. Java will not > compile the fail case complaining about "o instanceof boolean" no being > valid. "Inconvertible types, cannot cast boolean to Object" is the message > you get. > I think that Groovy should report the same compilation error. -- This message was sent by Atlassian Jira (v8.3.4#803005)