Groovy
  1. Groovy
  2. GROOVY-4813

GroovyScriptEngine does not use configured ImportCustomizer

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.8.0
    • Fix Version/s: 1.8.1, 1.9-beta-1
    • Component/s: None
    • Labels:
      None
    • Environment:
      Windows, JDK 1.6.0_21

      Description

      When running a groovy script using groovy.util.GroovyScriptEngine, the configured ImportCustomizer is not used. When running the script with GroovyShell, it works as expected.

      The problem can be reproduced using the following groovy script that tries to run a small script using groovy.util.GroovyScriptEngine and GroovyShell:

      import org.codehaus.groovy.control.CompilerConfiguration;
      import org.codehaus.groovy.control.customizers.ImportCustomizer;
      
      File script = File.createTempFile('test', '.groovy')
      script.deleteOnExit()
      script.write """
      println new SimpleDateFormat()
      """
      
      // Create compiler configuration with import customizer
      CompilerConfiguration config = new CompilerConfiguration();
      ImportCustomizer importCustomizer = new ImportCustomizer();
      importCustomizer.addImports 'java.text.SimpleDateFormat'
      config.addCompilationCustomizers importCustomizer
      
      // Run script with groovy shell: this will work
      GroovyShell shell = new GroovyShell(config)
      shell.run script, []
      
      // Run script with script engine: this will not work, import customizer is not used
      GroovyScriptEngine scriptEngine = new GroovyScriptEngine(script.getParent())
      scriptEngine.setConfig config
      scriptEngine.run script.getName(), new Binding()
      

      The output of this script looks like this:

      java.text.SimpleDateFormat@3dd497a
      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      /C:/Users/chpi2491/AppData/Local/Temp/test8494443541360579143.groovy: 2: unable to resolve class SimpleDateFormat 
       @ line 2, column 9.
         println new SimpleDateFormat()
                 ^
      
      1 error
      

      The expected output would be something like this:

      java.text.SimpleDateFormat@???????
      java.text.SimpleDateFormat@???????
      

        Activity

        Transition Time In Source Status Execution Times Last Executer Last Execution Date
        Open Open Resolved Resolved
        4d 2h 53m 1 Cédric Champeau 10/May/11 05:30
        Resolved Resolved Closed Closed
        72d 14h 35m 1 Paul King 21/Jul/11 20:06
        Mark Thomas made changes -
        Workflow jira [ 12973697 ] Default workflow, editable Closed status [ 12980819 ]
        Mark Thomas made changes -
        Project Import Mon Apr 06 02:11:23 UTC 2015 [ 1428286283443 ]
        Mark Thomas made changes -
        Workflow jira [ 12733741 ] Default workflow, editable Closed status [ 12745553 ]
        Mark Thomas made changes -
        Project Import Sun Apr 05 13:32:57 UTC 2015 [ 1428240777691 ]
        Paul King made changes -
        Status Resolved [ 5 ] Closed [ 6 ]
        Cédric Champeau made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Fixed [ 1 ]
        Hide
        Cédric Champeau added a comment -

        Fixed in 1.8.1 and 1.9-beta-1

        Show
        Cédric Champeau added a comment - Fixed in 1.8.1 and 1.9-beta-1
        Guillaume Delcroix made changes -
        Priority Critical [ 2 ] Major [ 3 ]
        Fix Version/s 1.9-beta-1 [ 17153 ]
        Fix Version/s 1.8.1 [ 17223 ]
        Cédric Champeau made changes -
        Field Original Value New Value
        Assignee Cédric Champeau [ melix ]
        Christoph Pirkl created issue -

          People

          • Assignee:
            Cédric Champeau
            Reporter:
            Christoph Pirkl
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development