From a7984b159489c5b21e98ebf549f8275cf85822d7 Mon Sep 17 00:00:00 2001
From: Michael Bayne The field type, method return type, or map value type should correspond to the desired
+ * behavior if the resolved name corresponds to a section. {@link Boolean} is used for showing or
+ * hiding sections without binding a sub-context. Arrays, {@link Iterator} and {@link Iterable}
+ * implementations are used for sections that repeat, with the context bound to the elements of the
+ * array, iterator or iterable. Lambdas are current unsupported, though they would be easy enough
+ * to add if desire exists. See the Mustache
+ * documentation for more details on section behavior. The data can be any tree of objects. Given a name
+ *
+ * foo, the following
- * mechanisms are supported for resolving its value (and are sought in this order):
- *
- *
- * foo as the key.
- * foo in the supplied object (with non-void return value).
- * getFoo in the supplied object (with non-void return value).
- * foo in the supplied object.
- *
The field type, method return type, or map value type should correspond to the - * desired behavior if the resolved name corresponds to a section. {@link Boolean} is used for - * showing or hiding sections without binding a sub-context. Arrays, {@link Iterator} and - * {@link Iterable} implementations are used for sections that repeat, with the context bound - * to the elements of the array, iterator or iterable. Lambdas are current unsupported, though - * they would be easy enough to add if desire exists. See the Mustache documentation for more - * details on section behavior.
- * - * @throws MustacheException if an error occurs while writing the template. + * Executes this template with the given context, writing the results to the supplied writer. + * @throws MustacheException if an error occurs while executing or writing the template. */ - public void execute (Object data, Writer out) throws MustacheException + public void execute (Object context, Writer out) throws MustacheException { for (Segment seg : _segs) { - seg.execute(this, data, out); + seg.execute(this, context, out); } } /** - * Executes this template with the supplied data, returning the results as a string. See {@link - * #execute(Object, Writer). - * - * @throws MustacheException if an error occurs while writing the template. + * Executes this template with the given context, returning the results as a string. + * @throws MustacheException if an error occurs while executing or writing the template. */ - public String execute (Object data) throws MustacheException + public String execute (Object context) throws MustacheException { StringWriter out = new StringWriter(); - execute(data, out); + execute(context, out); return out.toString(); } @@ -136,20 +132,24 @@ public class Template Method m; try { m = clazz.getDeclaredMethod(name); - if (!m.isAccessible()) { - m.setAccessible(true); + if (!m.getReturnType().equals(void.class)) { + if (!m.isAccessible()) { + m.setAccessible(true); + } + return m; } - return m; } catch (Exception e) { // fall through } try { m = clazz.getDeclaredMethod( "get" + Character.toUpperCase(name.charAt(0)) + name.substring(1)); - if (!m.isAccessible()) { - m.setAccessible(true); + if (!m.getReturnType().equals(void.class)) { + if (!m.isAccessible()) { + m.setAccessible(true); + } + return m; } - return m; } catch (Exception e) { // fall through } diff --git a/src/test/java/com/samskivert/mustache/MustacheTest.java b/src/test/java/com/samskivert/mustache/MustacheTest.java index b07c639..8c3e380 100644 --- a/src/test/java/com/samskivert/mustache/MustacheTest.java +++ b/src/test/java/com/samskivert/mustache/MustacheTest.java @@ -38,6 +38,13 @@ public class MustacheTest }); } + @Test public void testSkipVoidReturn () { + test("bar", "{{foo}}", new Object() { + void foo () {} + String getFoo () { return "bar"; } + }); + } + @Test public void testCallSiteReuse () { Template tmpl = Mustache.compile("{{foo}}"); Object ctx = new Object() {