Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-2750 rewrite GroovyScriptEngine
  3. GROOVY-4234

GroovyScriptEngine.loadScriptByName loads the wrong class when scripts contain class definitions

    XMLWordPrintableJSON

Details

    • Sub-task
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.7.2
    • 1.7.3, 1.8-beta-1
    • None
    • None
    • WinXP - Groovy 1.7.x

    Description

      When a script defines and uses another class, the scriptCache entry in GroovyScriptEngine gets overwritten with the class defined inside the script rather than just containing the class of the script. This causes the script to fail when it is executed the second time because the wrong class is getting loaded from the cache. For example:

      File FooScript.groovy
      -----------------------
      println "Running FooScript.groovy"

      class Foo {
      String bar()

      { "bar" }

      }

      assert new Foo().bar() == "bar"

      My test script is defined as:

      GseTest.groovy
      --------------
      println "Groovy Version = ${GroovySystem.version}"
      def gse = new GroovyScriptEngine(".")

      println "Run 1"
      gse.run("FooScript.groovy", new Binding())

      println "Run 2"
      gse.run("FooScript.groovy", new Binding())

      This produces the following output:

      Groovy Version = 1.7.2
      Run 1
      Running FooScript.groovy
      Run 2
      Caught: groovy.lang.MissingMethodException: No signature of method: Foo.main() is applicable for argument types: ([Ljava.lang.String values: [[]]
      Possible solutions: wait(), wait(long), bar(), any(), wait(long, int), each(groovy.lang.Closure)
      at GseTest.run(GseTest.groovy:8)

      We have a number of Groovlets that use this approach, and the bug was exposed when we upgraded from Groovy 1.6.x to 1.7.x.

      Attachments

        1. FooScript.groovy
          0.1 kB
          Doug Bender
        2. GseTest.groovy
          0.2 kB
          Doug Bender
        3. v17x_4234_Patch.txt
          2 kB
          Roshan Dawrani

        Activity

          People

            roshandawrani Roshan Dawrani
            dougbender Doug Bender
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: