Pass the context instance itself instead of its class.

In GWT we can't use Class.isAssignableFrom. We need the original instance so
that we can do (ctx instanceof Map<?,?>).
This commit is contained in:
Michael Bayne
2011-10-29 15:29:18 -07:00
parent 69826a707e
commit f6c6f4916c
4 changed files with 11 additions and 8 deletions
@@ -23,14 +23,14 @@ public class BasicCollector implements Mustache.Collector
} }
@Override @Override
public Mustache.VariableFetcher createFetcher (Class<?> cclass, String name) public Mustache.VariableFetcher createFetcher (Object ctx, String name)
{ {
// support both .name and this.name to fetch members // support both .name and this.name to fetch members
if (name == Template.DOT_NAME || name == Template.THIS_NAME) { if (name == Template.DOT_NAME || name == Template.THIS_NAME) {
return THIS_FETCHER; return THIS_FETCHER;
} }
if (Map.class.isAssignableFrom(cclass)) { if (ctx instanceof Map<?,?>) {
return MAP_FETCHER; return MAP_FETCHER;
} }
@@ -34,11 +34,12 @@ public class DefaultCollector extends BasicCollector
} }
@Override @Override
public Mustache.VariableFetcher createFetcher (Class<?> cclass, String name) public Mustache.VariableFetcher createFetcher (Object ctx, String name)
{ {
Mustache.VariableFetcher fetcher = super.createFetcher(cclass, name); Mustache.VariableFetcher fetcher = super.createFetcher(ctx, name);
if (fetcher != null) return fetcher; if (fetcher != null) return fetcher;
Class<?> cclass = ctx.getClass();
final Method m = getMethod(cclass, name); final Method m = getMethod(cclass, name);
if (m != null) { if (m != null) {
return new Mustache.VariableFetcher() { return new Mustache.VariableFetcher() {
@@ -144,8 +144,10 @@ public class Mustache
* not a collection. */ * not a collection. */
Iterator<?> toIterator (final Object value); Iterator<?> toIterator (final Object value);
/** Creates a fetcher for a so-named variable on instances of the given class. */ /** Creates a fetcher for a so-named variable in the supplied context object, which will
VariableFetcher createFetcher (Class<?> cclass, String name); * never be null. The fetcher will be cached and reused for future contexts for which
* {@code octx.getClass().equals(nctx.getClass()}. */
VariableFetcher createFetcher (Object ctx, String name);
} }
/** /**
@@ -186,10 +186,10 @@ public class Template
return fetcher.get(data, name); return fetcher.get(data, name);
} catch (Exception e) { } catch (Exception e) {
// zoiks! non-monomorphic call site, update the cache and try again // zoiks! non-monomorphic call site, update the cache and try again
fetcher = _compiler.collector.createFetcher(key.cclass, key.name); fetcher = _compiler.collector.createFetcher(data, key.name);
} }
} else { } else {
fetcher = _compiler.collector.createFetcher(key.cclass, key.name); fetcher = _compiler.collector.createFetcher(data, key.name);
} }
// if we were unable to create a fetcher, just return null and our caller can either try // if we were unable to create a fetcher, just return null and our caller can either try