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

GroovyScriptEngine does not use configured ImportCustomizer

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.8.0
    • 1.8.1, 1.9-beta-1
    • None
    • None
    • 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@???????
      

      Attachments

        Activity

          People

            melix Cédric Champeau
            kaklakariada Christoph Pirkl
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: