diff --git a/handlebars/src/main/java/com/github/jknack/handlebars/helper/StringHelpers.java b/handlebars/src/main/java/com/github/jknack/handlebars/helper/StringHelpers.java index e63fc524..f695d137 100644 --- a/handlebars/src/main/java/com/github/jknack/handlebars/helper/StringHelpers.java +++ b/handlebars/src/main/java/com/github/jknack/handlebars/helper/StringHelpers.java @@ -10,6 +10,7 @@ import static org.apache.commons.lang3.Validate.validIndex; import java.io.IOException; +import java.lang.reflect.Array; import java.math.RoundingMode; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; @@ -170,7 +171,13 @@ protected CharSequence safeApply(final Object context, final Options options) { return prefix + StringUtils.join((Iterator) context, (String) separator) + suffix; } if (context.getClass().isArray()) { - return prefix + StringUtils.join((Object[]) context, (String) separator) + suffix; + // arrays of primitives (int[], long[], ...) cannot be cast to Object[] + int length = Array.getLength(context); + Object[] items = new Object[length]; + for (int i = 0; i < length; i++) { + items[i] = Array.get(context, i); + } + return prefix + StringUtils.join(items, (String) separator) + suffix; } // join everything as single values Object[] values = new Object[options.params.length]; diff --git a/handlebars/src/test/java/com/github/jknack/handlebars/helper/StringHelpersTest.java b/handlebars/src/test/java/com/github/jknack/handlebars/helper/StringHelpersTest.java index 84aba124..363c12e6 100644 --- a/handlebars/src/test/java/com/github/jknack/handlebars/helper/StringHelpersTest.java +++ b/handlebars/src/test/java/com/github/jknack/handlebars/helper/StringHelpersTest.java @@ -168,6 +168,12 @@ public void joinArray() throws IOException { "6, 7, 8"); } + @Test + public void joinPrimitiveArray() throws IOException { + shouldCompileTo( + "{{{join this \", \"}}}", new int[] {6, 7, 8}, $("join", StringHelpers.join), "6, 7, 8"); + } + @Test public void joinValues() throws IOException { shouldCompileTo(