antlr / antlr/stringtemplate4

Templates in maps loaded via STGroupFile do not use registered ObjectModelAdaptors

Open
#223 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
1k
Forks
234
PR merge metrics
No merged PRs in 30d

Description

Given ST4 4.1 on the JVM and the following code, I would expect that the `View` would be dumped out identically both times. However, the following is produced:
```
before template = View(regularName=HIP! HIP!,special_name=HUZZAH!) and after map template = View(regularName=HIP! HIP!,special_name=)
```
It seems that the templates in the map create a new group based on the file they appear in but this new group does not inherit the model adaptor registrations from the original group. Much confusion ensues.

```kotlin
import org.stringtemplate.v4.*
import org.stringtemplate.v4.misc.*
import java.nio.charset.StandardCharsets

class Adaptor : ObjectModelAdaptor() {
override fun getProperty(interp: Interpreter, self: ST, o: Any, property: Any?, propertyName: String): Any? =
if (propertyName == "special_name")
"HUZZAH!"
else
super.getProperty(interp, self, o, property, propertyName)
}

class View(val regularName: String)

fun main(args: Array) {
val group = STGroupFile(View::class.java.getResource("/strepl/main.stg"), StandardCharsets.UTF_8.name(), '$', '$')
group.registerModelAdaptor(Object::class.java, Adaptor())
val st = group.getInstanceOf("main")
st.add("view", View("HIP! HIP!"))
println(st.render())
}
```

`/strepl/main`:
```
import "other.stg"

main(view) ::= "$other_main(view)$"
```

`/strepl/other`:
```
delimiters "$", "$"

other_main(view) ::= "before template = $dump(view)$ and after $map.(\"simple\")$"
map ::= [
"simple": <%map template = $dump(view)$%>
]
dump(view) ::= "View(regularName=$view.regularName$,special_name=$view.special_name$)"
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.