如何在 StringTemplate4 中重置 ST 实例?

How do you reset an `ST` instance in `StringTemplate4`?

版本 3 中的 StringTemplate 实例有一个 .reset() 方法。

我在 for/each 循环中生成,并希望在每个循环结束时将实例重置为默认状态。 我搜索了 JavaDoc,但找不到如何重置 ST 的实例以重用它。

如何在 StringTemplate4 中重置 ST 实例?

This is here because this was a hard thing to find!

The .reset() method was removed and there is no direct documentation that hints to how to accomplish this in version 4.

By digging into the source code I discovered that .getInstanceOf() delivers a cached uninitialized copy of the template there is no need to call .reset() on the instances anymore, just get a fresh cached instance.

截至本回答时来自 STGroup.java 的相关代码:

/** The primary means of getting an instance of a template from this
 *  group. Names must be absolute, fully-qualified names like {@code /a/b}.
 */
public ST getInstanceOf(String name) {
    if ( name==null ) return null;
    if ( verbose ) System.out.println(getName()+".getInstanceOf("+name+")");
    if ( name.charAt(0)!='/' ) name = "/"+name;
    CompiledST c = lookupTemplate(name);
    if ( c!=null ) {
        return createStringTemplate(c);
    }
    return null;
}

/** Look up a fully-qualified name. */
public CompiledST lookupTemplate(String name) {
    if ( name.charAt(0)!='/' ) name = "/"+name;
    if ( verbose ) System.out.println(getName()+".lookupTemplate("+name+")");
    CompiledST code = rawGetTemplate(name);
    if ( code==NOT_FOUND_ST ) {
        if ( verbose ) System.out.println(name+" previously seen as not found");
        return null;
    }
    // try to load from disk and look up again
    if ( code==null ) code = load(name);
    if ( code==null ) code = lookupImportedTemplate(name);
    if ( code==null ) {
        if ( verbose ) System.out.println(name+" recorded not found");
        templates.put(name, NOT_FOUND_ST);
    }
    if ( verbose ) if ( code!=null ) System.out.println(getName()+".lookupTemplate("+name+") found");
    return code;
}