From a7984b159489c5b21e98ebf549f8275cf85822d7 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Thu, 21 Oct 2010 19:42:38 +0000 Subject: [PATCH] Javadoc fixes, properly skip void methods. --- .../com/samskivert/mustache/Template.java | 72 +++++++++---------- .../com/samskivert/mustache/MustacheTest.java | 7 ++ 2 files changed, 43 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/samskivert/mustache/Template.java b/src/main/java/com/samskivert/mustache/Template.java index 3123233..a006fba 100644 --- a/src/main/java/com/samskivert/mustache/Template.java +++ b/src/main/java/com/samskivert/mustache/Template.java @@ -15,50 +15,46 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** - * Represents a compiled template. + * Represents a compiled template. Templates are executed with a context to generate + * output. The context can be any tree of objects. Variables are resolved against the context. + * Given a name {@code foo}, the following mechanisms are supported for resolving its value + * (and are sought in this order): + * + *

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.

*/ public class Template { /** - * Executes this template with the supplied data, writing the results to the supplied writer. - * - *

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): - *

- *

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() {