groovy
  1. groovy
  2. GROOVY-4813

GroovyScriptEngine does not use configured ImportCustomizer

    Details

    • Type: Bug Bug
    • Status: Closed 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
    • Number of attachments :
      0

      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

        CÚdric Champeau made changes -
        Field Original Value New Value
        Assignee Cédric Champeau [ melix ]
        Guillaume Laforge made changes -
        Fix Version/s 1.8.1 [ 17223 ]
        Fix Version/s 1.9-beta-1 [ 17153 ]
        Priority Critical [ 2 ] Major [ 3 ]
        CÚdric Champeau made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Fixed [ 1 ]
        Paul King made changes -
        Status Resolved [ 5 ] Closed [ 6 ]

          People

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

            Dates

            • Created:
              Updated:
              Resolved: