freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject [freemarker] branch 2.3-gae updated: Some code cleanup, mostly around function and macro calls. Also added generic parameters at some places.
Date Sat, 16 Feb 2019 20:04:58 GMT
This is an automated email from the ASF dual-hosted git repository.

ddekany pushed a commit to branch 2.3-gae
in repository https://gitbox.apache.org/repos/asf/freemarker.git


The following commit(s) were added to refs/heads/2.3-gae by this push:
     new 8783907  Some code cleanup, mostly around function and macro calls. Also added generic
parameters at some places.
8783907 is described below

commit 87839079337e1a0fefec884ff3ac85a00df12d78
Author: ddekany <ddekany@apache.org>
AuthorDate: Sat Feb 16 20:58:48 2019 +0100

    Some code cleanup, mostly around function and macro calls. Also added generic parameters
at some places.
---
 src/main/java/freemarker/core/Environment.java     | 60 ++++++++++++++++------
 src/main/java/freemarker/core/Expression.java      | 14 ++---
 src/main/java/freemarker/core/ListLiteral.java     |  4 +-
 src/main/java/freemarker/core/MethodCall.java      | 26 ++--------
 .../java/freemarker/core/NonMethodException.java   | 21 ++++++--
 .../freemarker/core/UnexpectedTypeException.java   |  2 +-
 src/main/java/freemarker/core/UnifiedCall.java     |  2 +-
 7 files changed, 78 insertions(+), 51 deletions(-)

diff --git a/src/main/java/freemarker/core/Environment.java b/src/main/java/freemarker/core/Environment.java
index a6eb6eb..376b838 100644
--- a/src/main/java/freemarker/core/Environment.java
+++ b/src/main/java/freemarker/core/Environment.java
@@ -670,7 +670,7 @@ public final class Environment extends Configurable {
         try {
             TemplateModel macroOrTransform = getNodeProcessor(node);
             if (macroOrTransform instanceof Macro) {
-                invoke((Macro) macroOrTransform, null, null, null, null);
+                invokeMacro((Macro) macroOrTransform, null, null, null, null);
             } else if (macroOrTransform instanceof TemplateTransformModel) {
                 visitAndTransform(null, (TemplateTransformModel) macroOrTransform, null);
             } else {
@@ -726,19 +726,48 @@ public final class Environment extends Configurable {
     void fallback() throws TemplateException, IOException {
         TemplateModel macroOrTransform = getNodeProcessor(currentNodeName, currentNodeNS,
nodeNamespaceIndex);
         if (macroOrTransform instanceof Macro) {
-            invoke((Macro) macroOrTransform, null, null, null, null);
+            invokeMacro((Macro) macroOrTransform, null, null, null, null);
         } else if (macroOrTransform instanceof TemplateTransformModel) {
             visitAndTransform(null, (TemplateTransformModel) macroOrTransform, null);
         }
     }
 
     /**
-     * Calls the macro or function with the given arguments and nested block.
+     * Calls a macro with the given arguments and nested block.
      */
-    void invoke(Macro macro,
-            Map namedArgs, List positionalArgs,
+    void invokeMacro(Macro macro,
+            Map namedArgs, List<? extends Expression> positionalArgs,
             List bodyParameterNames, TemplateObject callPlace) throws TemplateException,
IOException {
-        if (macro == Macro.DO_NOTHING_MACRO) {
+        invokeMacroOrFunctionCommonPart(macro, namedArgs, positionalArgs, bodyParameterNames,
callPlace);
+    }
+
+    /**
+     * Calls an FTL function, and returns its return value.
+     */
+    TemplateModel invokeFunction(
+            Environment env, Macro func, List<? extends Expression> argumentExps, TemplateObject
callPlace)
+            throws TemplateException {
+        env.setLastReturnValue(null);
+        if (!func.isFunction()) {
+            throw new _MiscTemplateException(env, "A macro cannot be called in an expression.
(Functions can be.)");
+        }
+        Writer prevOut = env.getOut();
+        try {
+            env.setOut(NullWriter.INSTANCE);
+            env.invokeMacro(func, null, argumentExps, null, callPlace);
+        } catch (IOException e) {
+            // Should not occur
+            throw new TemplateException("Unexpected exception during function execution",
e, env);
+        } finally {
+            env.setOut(prevOut);
+        }
+        return env.getLastReturnValue();
+    }
+
+    private void invokeMacroOrFunctionCommonPart(Macro macroOrFunction,
+            Map namedArgs, List<? extends Expression> positionalArgs,
+            List<Expression> bodyParameterNames, TemplateObject callPlace) throws TemplateException,
IOException {
+        if (macroOrFunction == Macro.DO_NOTHING_MACRO) {
             return;
         }
 
@@ -746,18 +775,18 @@ public final class Environment extends Configurable {
         if (!incompatibleImprovementsGE2328) {
             // Doing this so early is wrong, as now the arguments will be evaluated while
the called macro/function is
             // in the element stack. Thus .current_template_name will be wrong for example.
-            pushElement(macro);
+            pushElement(macroOrFunction);
             elementPushed = true;
         } else {
             elementPushed = false;
         }
         try {
-            final Macro.Context macroCtx = macro.new Context(this, callPlace, bodyParameterNames);
+            final Macro.Context macroCtx = macroOrFunction.new Context(this, callPlace, bodyParameterNames);
             // Causes the evaluation of argument expressions:
-            setMacroContextLocalsFromArguments(macroCtx, macro, namedArgs, positionalArgs);
-            
+            setMacroContextLocalsFromArguments(macroCtx, macroOrFunction, namedArgs, positionalArgs);
+
             if (!elementPushed) { // When incompatibleImprovements >= 2.3.28
-                pushElement(macro);
+                pushElement(macroOrFunction);
                 elementPushed = true;
             }
 
@@ -768,11 +797,11 @@ public final class Environment extends Configurable {
             localContextStack = null;
 
             final Namespace prevNamespace = currentNamespace;
-            currentNamespace = (Namespace) macroToNamespaceLookup.get(macro);
+            currentNamespace = (Namespace) macroToNamespaceLookup.get(macroOrFunction);
 
             try {
                 macroCtx.sanityCheck(this);
-                visit(macro.getChildBuffer());
+                visit(macroOrFunction.getChildBuffer());
             } catch (ReturnInstruction.Return re) {
                 // Not an error, just a <#return>
             } catch (TemplateException te) {
@@ -795,7 +824,8 @@ public final class Environment extends Configurable {
     private void setMacroContextLocalsFromArguments(
             final Macro.Context macroCtx,
             final Macro macro,
-            final Map namedArgs, final List positionalArgs) throws TemplateException, _MiscTemplateException
{
+            final Map namedArgs, final List<? extends Expression> positionalArgs) throws
TemplateException,
+            _MiscTemplateException {
         String catchAllParamName = macro.getCatchAll();
         if (namedArgs != null) {
             final SimpleHash catchAllParamValue;
@@ -842,7 +872,7 @@ public final class Environment extends Configurable {
                         new _DelayedToString(argsCnt), ".");
             }
             for (int i = 0; i < argsCnt; i++) {
-                Expression argValueExp = (Expression) positionalArgs.get(i);
+                Expression argValueExp = positionalArgs.get(i);
                 TemplateModel argValue = argValueExp.eval(this);
                 try {
                     if (i < argNames.length) {
diff --git a/src/main/java/freemarker/core/Expression.java b/src/main/java/freemarker/core/Expression.java
index 45da3c9..0c3575c 100644
--- a/src/main/java/freemarker/core/Expression.java
+++ b/src/main/java/freemarker/core/Expression.java
@@ -59,7 +59,7 @@ abstract public class Expression extends TemplateObject {
     // Hook in here to set the constant value if possible.
     
     @Override
-    void setLocation(Template template, int beginColumn, int beginLine, int endColumn, int
endLine) {
+    final void setLocation(Template template, int beginColumn, int beginLine, int endColumn,
int endLine) {
         super.setLocation(template, beginColumn, beginLine, endColumn, endLine);
         if (isLiteral()) {
             try {
@@ -129,13 +129,13 @@ abstract public class Expression extends TemplateObject {
     String evalAndCoerceToStringOrUnsupportedMarkup(Environment env, String seqTip) throws
TemplateException {
         return EvalUtil.coerceModelToStringOrUnsupportedMarkup(eval(env), this, seqTip, env);
     }
-    
+
     Number evalToNumber(Environment env) throws TemplateException {
         TemplateModel model = eval(env);
         return modelToNumber(model, env);
     }
 
-    Number modelToNumber(TemplateModel model, Environment env) throws TemplateException {
+    final Number modelToNumber(TemplateModel model, Environment env) throws TemplateException
{
         if (model instanceof TemplateNumberModel) {
             return EvalUtil.modelToNumber((TemplateNumberModel) model, this);
         } else {
@@ -151,7 +151,7 @@ abstract public class Expression extends TemplateObject {
         return evalToBoolean(null, cfg);
     }
 
-    TemplateModel evalToNonMissing(Environment env) throws TemplateException {
+    final TemplateModel evalToNonMissing(Environment env) throws TemplateException {
         TemplateModel result = this.eval(env);
         assertNonNull(result, env);
         return result;
@@ -162,11 +162,11 @@ abstract public class Expression extends TemplateObject {
         return modelToBoolean(model, env, cfg);
     }
     
-    boolean modelToBoolean(TemplateModel model, Environment env) throws TemplateException
{
+    final boolean modelToBoolean(TemplateModel model, Environment env) throws TemplateException
{
         return modelToBoolean(model, env, null);
     }
 
-    boolean modelToBoolean(TemplateModel model, Configuration cfg) throws TemplateException
{
+    final boolean modelToBoolean(TemplateModel model, Configuration cfg) throws TemplateException
{
         return modelToBoolean(model, null, cfg);
     }
     
@@ -229,7 +229,7 @@ abstract public class Expression extends TemplateObject {
         }
     }
     
-    void assertNonNull(TemplateModel model, Environment env) throws InvalidReferenceException
{
+    final void assertNonNull(TemplateModel model, Environment env) throws InvalidReferenceException
{
         if (model == null) throw InvalidReferenceException.getInstance(this, env);
     }
     
diff --git a/src/main/java/freemarker/core/ListLiteral.java b/src/main/java/freemarker/core/ListLiteral.java
index c25191c..02b42be 100644
--- a/src/main/java/freemarker/core/ListLiteral.java
+++ b/src/main/java/freemarker/core/ListLiteral.java
@@ -35,9 +35,9 @@ import freemarker.template.TemplateSequenceModel;
 
 final class ListLiteral extends Expression {
 
-    final ArrayList/*<Expression>*/ items;
+    final ArrayList<Expression> items;
 
-    ListLiteral(ArrayList items) {
+    ListLiteral(ArrayList<Expression> items) {
         this.items = items;
         items.trimToSize();
     }
diff --git a/src/main/java/freemarker/core/MethodCall.java b/src/main/java/freemarker/core/MethodCall.java
index 675c30c..afe3c83 100644
--- a/src/main/java/freemarker/core/MethodCall.java
+++ b/src/main/java/freemarker/core/MethodCall.java
@@ -23,8 +23,6 @@
 
 package freemarker.core;
 
-import java.io.IOException;
-import java.io.Writer;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -32,7 +30,6 @@ import freemarker.template.TemplateException;
 import freemarker.template.TemplateMethodModel;
 import freemarker.template.TemplateMethodModelEx;
 import freemarker.template.TemplateModel;
-import freemarker.template.utility.NullWriter;
 
 
 /**
@@ -60,29 +57,14 @@ final class MethodCall extends Expression {
             TemplateMethodModel targetMethod = (TemplateMethodModel) targetModel;
             List argumentStrings = 
             targetMethod instanceof TemplateMethodModelEx
-            ? arguments.getModelList(env)
-            : arguments.getValueList(env);
+                    ? arguments.getModelList(env)
+                    : arguments.getValueList(env);
             Object result = targetMethod.exec(argumentStrings);
             return env.getObjectWrapper().wrap(result);
         } else if (targetModel instanceof Macro) {
-            Macro func = (Macro) targetModel;
-            env.setLastReturnValue(null);
-            if (!func.isFunction()) {
-                throw new _MiscTemplateException(env, "A macro cannot be called in an expression.
(Functions can be.)");
-            }
-            Writer prevOut = env.getOut();
-            try {
-                env.setOut(NullWriter.INSTANCE);
-                env.invoke(func, null, arguments.items, null, this);
-            } catch (IOException e) {
-                // Should not occur
-                throw new TemplateException("Unexpected exception during function execution",
e, env);
-            } finally {
-                env.setOut(prevOut);
-            }
-            return env.getLastReturnValue();
+            return env.invokeFunction(env, (Macro) targetModel, arguments.items, this);
         } else {
-            throw new NonMethodException(target, targetModel, env);
+            throw new NonMethodException(target, targetModel, true, null, env);
         }
     }
 
diff --git a/src/main/java/freemarker/core/NonMethodException.java b/src/main/java/freemarker/core/NonMethodException.java
index dcda03e..a6eff89 100644
--- a/src/main/java/freemarker/core/NonMethodException.java
+++ b/src/main/java/freemarker/core/NonMethodException.java
@@ -30,7 +30,8 @@ import freemarker.template.TemplateModel;
 public class NonMethodException extends UnexpectedTypeException {
 
     private static final Class[] EXPECTED_TYPES = new Class[] { TemplateMethodModel.class
};
-    
+    private static final Class[] EXPECTED_TYPES_WITH_FUNCTION = new Class[] { TemplateMethodModel.class,
Macro.class };
+
     public NonMethodException(Environment env) {
         super(env, "Expecting method value here");
     }
@@ -58,7 +59,21 @@ public class NonMethodException extends UnexpectedTypeException {
 
     NonMethodException(
             Expression blamed, TemplateModel model, String[] tips, Environment env) throws
InvalidReferenceException {
-        super(blamed, model, "method", EXPECTED_TYPES, tips, env);
-    }    
+        this(blamed, model, false, tips, env);
+    }
+
+    /**
+     * @param allowFTLFunction Whether FTL functions are also acceptable
+     *
+     * @since 2.3.29
+     */
+    NonMethodException(
+            Expression blamed, TemplateModel model, boolean allowFTLFunction, String[] tips,
Environment env)
+            throws InvalidReferenceException {
+        super(blamed, model,
+                allowFTLFunction ? "method or function" : "method",
+                allowFTLFunction ? EXPECTED_TYPES_WITH_FUNCTION : EXPECTED_TYPES,
+                tips, env);
+    }
 
 }
diff --git a/src/main/java/freemarker/core/UnexpectedTypeException.java b/src/main/java/freemarker/core/UnexpectedTypeException.java
index 9cb56f8..6caf9ce 100644
--- a/src/main/java/freemarker/core/UnexpectedTypeException.java
+++ b/src/main/java/freemarker/core/UnexpectedTypeException.java
@@ -98,7 +98,7 @@ public class UnexpectedTypeException extends TemplateException {
                         || Arrays.asList(expectedTypes).contains(TemplateCollectionModelEx.class)))
{
             errorDescBuilder.tip("As the problematic value contains a collection of items,
you could convert it "
                     + "to a sequence like someValue?sequence. Be sure though that you won't
have a large number of "
-                    + "items, as all will be held in memory one the same time.");
+                    + "items, as all will be held in memory the same time.");
         }
         return errorDescBuilder;
     }
diff --git a/src/main/java/freemarker/core/UnifiedCall.java b/src/main/java/freemarker/core/UnifiedCall.java
index 68777d3..186fb4e 100644
--- a/src/main/java/freemarker/core/UnifiedCall.java
+++ b/src/main/java/freemarker/core/UnifiedCall.java
@@ -80,7 +80,7 @@ final class UnifiedCall extends TemplateElement implements DirectiveCallPlace
{
                         + "Functions can only be called from expressions, like in ${f()},
${x + f()} or ",
                         "<@someDirective someParam=f() />", ".");
             }    
-            env.invoke(macro, namedArgs, positionalArgs, bodyParameterNames, this);
+            env.invokeMacro(macro, namedArgs, positionalArgs, bodyParameterNames, this);
         } else {
             boolean isDirectiveModel = tm instanceof TemplateDirectiveModel; 
             if (isDirectiveModel || tm instanceof TemplateTransformModel) {


Mime
View raw message