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

SimpleTemplateEngine cannot accept a read only map with binding vars

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • 2.4.12
    • None
    • Templating
    • None
    • Not OS etc specific.

    Description

      It's not possible to pass a read-only map into a simple template engine template.

      def engine = new groovy.text.SimpleTemplateEngine()
      def template = engine.createTemplate(new java.io.StringReader('foo = $foo'))
      println template.make(Collections.unmodifiableMap([foo: 'bar']))

      The above throws a very unhelpful UnsupportedOperationException whose stacktrace doesn't give the caller any idea of which method is throwing the exception. I had to dig into the groovy source code to understand what was going on.

      The source of the problem is that the engine tries to set the "out" variable to the PrintWriter that it uses.

      Potential alternative behaviour:

      • When initialising a new binding, create a copy of the passed-in map.
      • Catch the UnsupportedOperationException and don't bind the out variable in this case. The problem with this approach is that there's no obvious way to report this condition. Perhaps if the script tries to resolve the out variable we could report it then.

       

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            tomdcc Tom Dunstan
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: