Skip to content

Commit 1cf383b

Browse files
carusologyjknack
authored andcommitted
Add @helperFunction annotation for overriding method names from Helper Sources (#829)
1 parent dfc2b90 commit 1cf383b

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

handlebars/src/main/java/com/github/jknack/handlebars/helper/DefaultHelperRegistry.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,11 @@ private void registerDynamicHelper(final Object source, final Class<?> clazz) {
234234
Method[] methods = clazz.getDeclaredMethods();
235235
for (Method method : methods) {
236236
boolean isPublic = Modifier.isPublic(method.getModifiers());
237-
String helperName = method.getName();
238237
if (isPublic) {
239238
boolean isStatic = Modifier.isStatic(method.getModifiers());
240239
if (source != null || isStatic) {
240+
HelperFunction annotation = method.getAnnotation(HelperFunction.class);
241+
String helperName = annotation != null ? annotation.value() : method.getName();
241242
isTrue(overloaded.add(helperName), "name conflict found: " + helperName);
242243
registerHelper(helperName, new MethodHelper(method, source));
243244
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.github.jknack.handlebars.helper;
2+
3+
import java.lang.annotation.Retention;
4+
import java.lang.annotation.RetentionPolicy;
5+
6+
/**
7+
* Decorates a method that represents a helper function extracted via a "helper source"
8+
* with metadata that cannot be inferred from its signature, such as a custom helper name.
9+
*/
10+
@Retention(RetentionPolicy.RUNTIME)
11+
public @interface HelperFunction {
12+
13+
/**
14+
* The name used to invoke the decorated helper function in a handlebars template.
15+
*/
16+
String value();
17+
18+
}

handlebars/src/test/java/com/github/jknack/handlebars/ReflectiveHelperTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import java.io.IOException;
1010

11+
import com.github.jknack.handlebars.helper.HelperFunction;
1112
import org.junit.Test;
1213

1314
import com.github.jknack.handlebars.Handlebars.SafeString;
@@ -53,6 +54,11 @@ public void testHelperWithParamsAndOptions() throws IOException {
5354
"helperWithParamsAndOptions:string:true:4");
5455
}
5556

57+
@Test
58+
public void testAnnotatedHelper() throws IOException {
59+
shouldCompileTo("{{this-is-annotated}}", null, "i am an annotated helper function");
60+
}
61+
5662
@Test
5763
public void testBlog() throws IOException {
5864
shouldCompileTo("{{blog this}}", new Blog("title", "body"),
@@ -155,6 +161,11 @@ public SafeString helperWithParamsAndOptions(final String context, final boolean
155161
return new SafeString(String.format("helperWithParamsAndOptions:%s:%s:%s", context, p0, p1));
156162
}
157163

164+
@HelperFunction("this-is-annotated")
165+
public CharSequence annotatedHelper() {
166+
return "i am an annotated helper function";
167+
}
168+
158169
public CharSequence blog(final Blog blog, final Options options) {
159170
assertNotNull(options);
160171
return "blog:" + blog.toString();

0 commit comments

Comments
 (0)