Details
-
Bug
-
Status: Open
-
Minor
-
Resolution: Unresolved
-
2.4.12
-
None
-
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.