Redo primitive array support in GWT-compatible way.

There's unfortunately no sane way to do the same reflection-like array access
in GWT, so we just have to enumerate all of the primitive types.

I could have just left this only in the Java backend, but I would prefer to
avoid differences between behavior on the JVM and behavior in GWT whenever
possible.
This commit is contained in:
Michael Bayne
2015-09-21 14:45:55 -07:00
parent 4ee24a6b19
commit c49426db85
2 changed files with 51 additions and 33 deletions
@@ -35,8 +35,16 @@ public abstract class BasicCollector implements Mustache.Collector
char c = name.charAt(0);
if (c >= '0' && c <= '9') {
if (ctx instanceof List<?>) return LIST_FETCHER;
if (ctx instanceof Object[]) return ARRAY_FETCHER;
if (ctx instanceof Iterator<?>) return ITER_FETCHER;
if (ctx instanceof Object[]) return OBJECT_ARRAY_FETCHER;
if (ctx instanceof boolean[]) return BOOLEAN_ARRAY_FETCHER;
if (ctx instanceof byte[]) return BYTE_ARRAY_FETCHER;
if (ctx instanceof char[]) return CHAR_ARRAY_FETCHER;
if (ctx instanceof short[]) return SHORT_ARRAY_FETCHER;
if (ctx instanceof int[]) return INT_ARRAY_FETCHER;
if (ctx instanceof long[]) return LONG_ARRAY_FETCHER;
if (ctx instanceof float[]) return FLOAT_ARRAY_FETCHER;
if (ctx instanceof double[]) return DOUBLE_ARRAY_FETCHER;
}
return null;
@@ -65,18 +73,6 @@ public abstract class BasicCollector implements Mustache.Collector
}
};
protected static final Mustache.VariableFetcher ARRAY_FETCHER = new Mustache.VariableFetcher() {
public Object get (Object ctx, String name) throws Exception {
try {
return ((Object[])ctx)[Integer.parseInt(name)];
} catch (NumberFormatException nfe) {
return Template.NO_FETCHER_FOUND;
} catch (IndexOutOfBoundsException e) {
return Template.NO_FETCHER_FOUND;
}
}
};
protected static final Mustache.VariableFetcher ITER_FETCHER = new Mustache.VariableFetcher() {
public Object get (Object ctx, String name) throws Exception {
try {
@@ -91,6 +87,47 @@ public abstract class BasicCollector implements Mustache.Collector
}
};
protected static abstract class ArrayFetcher implements Mustache.VariableFetcher {
public Object get (Object ctx, String name) throws Exception {
try {
return get(ctx, Integer.parseInt(name));
} catch (NumberFormatException nfe) {
return Template.NO_FETCHER_FOUND;
} catch (ArrayIndexOutOfBoundsException e) {
return Template.NO_FETCHER_FOUND;
}
}
protected abstract Object get (Object ctx, int index);
}
protected static final ArrayFetcher OBJECT_ARRAY_FETCHER = new ArrayFetcher() {
@Override protected Object get (Object ctx, int index) { return ((Object[])ctx)[index]; }
};
protected static final ArrayFetcher BOOLEAN_ARRAY_FETCHER = new ArrayFetcher() {
@Override protected Object get (Object ctx, int index) { return ((boolean[])ctx)[index]; }
};
protected static final ArrayFetcher BYTE_ARRAY_FETCHER = new ArrayFetcher() {
@Override protected Object get (Object ctx, int index) { return ((byte[])ctx)[index]; }
};
protected static final ArrayFetcher CHAR_ARRAY_FETCHER = new ArrayFetcher() {
@Override protected Object get (Object ctx, int index) { return ((char[])ctx)[index]; }
};
protected static final ArrayFetcher SHORT_ARRAY_FETCHER = new ArrayFetcher() {
@Override protected Object get (Object ctx, int index) { return ((short[])ctx)[index]; }
};
protected static final ArrayFetcher INT_ARRAY_FETCHER = new ArrayFetcher() {
@Override protected Object get (Object ctx, int index) { return ((int[])ctx)[index]; }
};
protected static final ArrayFetcher LONG_ARRAY_FETCHER = new ArrayFetcher() {
@Override protected Object get (Object ctx, int index) { return ((long[])ctx)[index]; }
};
protected static final ArrayFetcher FLOAT_ARRAY_FETCHER = new ArrayFetcher() {
@Override protected Object get (Object ctx, int index) { return ((float[])ctx)[index]; }
};
protected static final ArrayFetcher DOUBLE_ARRAY_FETCHER = new ArrayFetcher() {
@Override protected Object get (Object ctx, int index) { return ((double[])ctx)[index]; }
};
protected static final Mustache.VariableFetcher THIS_FETCHER = new Mustache.VariableFetcher() {
public Object get (Object ctx, String name) throws Exception {
return ctx;
@@ -43,15 +43,8 @@ public class DefaultCollector extends BasicCollector
Mustache.VariableFetcher fetcher = super.createFetcher(ctx, name);
if (fetcher != null) return fetcher;
Class<?> cclass = ctx.getClass();
// try generic 'indexing' fetcher for primitive arrays
char c = name.charAt(0);
if (c >= '0' && c <= '9') {
if (cclass.isArray()) return GENERIC_ARRAY_FETCHER;
}
// first check for a getter which provides the value
Class<?> cclass = ctx.getClass();
final Method m = getMethod(cclass, name);
if (m != null) {
return new Mustache.VariableFetcher() {
@@ -169,16 +162,4 @@ public class DefaultCollector extends BasicCollector
}
return null;
}
protected static final Mustache.VariableFetcher GENERIC_ARRAY_FETCHER = new Mustache.VariableFetcher() {
public Object get (Object ctx, String name) throws Exception {
try {
return Array.get(ctx, Integer.parseInt(name));
} catch (NumberFormatException nfe) {
return Template.NO_FETCHER_FOUND;
} catch (IndexOutOfBoundsException e) {
return Template.NO_FETCHER_FOUND;
}
}
};
}