Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-9270

Using "instanceof boolean" or using a Trait will cause the compiled code to not work with OSGi/maven-bundle-plugin/bnd

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 3.0.0-beta-3
    • 2.5.9, 3.0.0-rc-1
    • class generator, Compiler
    • None
    • OSX (Mojave) JDK 11.

    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 compiled 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) 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. 

      Running the "if(o instanceof booelan)" code that Groovy have happily compiled, results in: java.lang.NoClassDefFoundError: boolean

      I think that Groovy should report the same compilation error, rather than accept something clearly incorrect and fail runtime.

      Even with @CompileStatic and @TypeChecked annotations Groovy compiles the code without errors.

       

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              tombensve Tommy Svensson
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 40m
                  40m