You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by dd...@apache.org on 2017/09/13 09:49:12 UTC

[11/36] incubator-freemarker git commit: FREEMARKER-55: use vargs for code and args

FREEMARKER-55: use vargs for code and args


Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/b0acedda
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/b0acedda
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/b0acedda

Branch: refs/heads/3
Commit: b0aceddab9ac55c622fb596d2ed03444c2ff0884
Parents: 8f5eaaa
Author: Woonsan Ko <wo...@apache.org>
Authored: Sun Sep 3 00:27:08 2017 -0400
Committer: Woonsan Ko <wo...@apache.org>
Committed: Sun Sep 3 00:27:08 2017 -0400

----------------------------------------------------------------------
 .../spring/model/MessageFunction.java           | 34 +++++++++++---------
 1 file changed, 18 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/b0acedda/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/MessageFunction.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/MessageFunction.java b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/MessageFunction.java
index 7bee8c1..9579366 100644
--- a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/MessageFunction.java
+++ b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/MessageFunction.java
@@ -42,15 +42,14 @@ import org.springframework.web.servlet.support.RequestContext;
 
 public class MessageFunction extends AbstractSpringTemplateFunctionModel {
 
-    private static final int CODE_PARAM_IDX = 0;
-    private static final int MESSAGE_PARAM_IDX = 1;
-    private static final int MESSAGE_ARGS_PARAM_IDX = 2;
+    private static final int MESSAGE_PARAM_IDX = 0;
+    private static final int MESSAGE_ARGS_PARAM_IDX = 1;
 
     private static final String MESSAGE_PARAM_NAME = "message";
 
     private static final ArgumentArrayLayout ARGS_LAYOUT =
             ArgumentArrayLayout.create(
-                    1,
+                    0,
                     true,
                     StringToIndexMap.of(
                             MESSAGE_PARAM_NAME, MESSAGE_PARAM_IDX),
@@ -72,23 +71,24 @@ public class MessageFunction extends AbstractSpringTemplateFunctionModel {
 
         String message = null;
 
-        final String code = CallableUtils.getStringArgument(args, CODE_PARAM_IDX, this);
-
-        if (code != null && !code.isEmpty()) {
-            final TemplateCollectionModel messageArgsModel = (TemplateCollectionModel) args[MESSAGE_ARGS_PARAM_IDX];
-            List<Object> msgArgumentList = null;
-
-            if (!messageArgsModel.isEmptyCollection()) {
-                msgArgumentList = new ArrayList<>();
-                TemplateModel msgArgModel;
-                for (TemplateModelIterator tit = messageArgsModel.iterator(); tit.hasNext(); ) {
-                    msgArgModel = tit.next();
+        final TemplateCollectionModel messageArgsModel = (TemplateCollectionModel) args[MESSAGE_ARGS_PARAM_IDX];
+
+        if (!messageArgsModel.isEmptyCollection()) {
+            String code = null;
+            List<Object> msgArgumentList = new ArrayList<>();
+            TemplateModel msgArgModel;
+            int i = 0;
+            for (TemplateModelIterator tit = messageArgsModel.iterator(); tit.hasNext(); i++) {
+                msgArgModel = tit.next();
+                if (i == 0) {
+                    code = objectWrapperAndUnwrapper.unwrap(msgArgModel).toString();
+                } else {
                     msgArgumentList.add(objectWrapperAndUnwrapper.unwrap(msgArgModel));
                 }
             }
 
             // TODO: Is it okay to set the default value to null to avoid NoSuchMessageException from Spring MessageSource?
-            message = messageSource.getMessage(code, (msgArgumentList == null) ? null : msgArgumentList.toArray(),
+            message = messageSource.getMessage(code, (msgArgumentList.isEmpty()) ? null : msgArgumentList.toArray(),
                     null, requestContext.getLocale());
         } else {
             final TemplateModel messageModel = CallableUtils.getOptionalArgument(args, MESSAGE_PARAM_IDX,
@@ -97,6 +97,8 @@ public class MessageFunction extends AbstractSpringTemplateFunctionModel {
                 MessageSourceResolvable messageResolvable = (MessageSourceResolvable) objectWrapperAndUnwrapper
                         .unwrap(messageModel);
                 message = messageSource.getMessage(messageResolvable, requestContext.getLocale());
+            } else {
+                throw new TemplateException("Neither message code nor message resolvable was set.");
             }
         }