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 2018/03/20 17:59:32 UTC

[1/2] incubator-freemarker git commit: Forward ported from 2.3-gae: Better error message when <#assign x++> and such fails because x doesn't exist in the same scope as the target scope.

Repository: incubator-freemarker
Updated Branches:
  refs/heads/3 0d21cbb77 -> 421f4747b


Forward ported from 2.3-gae: Better error message when <#assign x++> and such fails because x doesn't exist in the same scope as the target scope.


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

Branch: refs/heads/3
Commit: 1af2e0c219e4cb0ef37252412275a5c7eab525ab
Parents: 0d21cbb
Author: ddekany <dd...@apache.org>
Authored: Tue Mar 20 18:56:44 2018 +0100
Committer: ddekany <dd...@apache.org>
Committed: Tue Mar 20 18:56:44 2018 +0100

----------------------------------------------------------------------
 .../apache/freemarker/core/MiscErrorMessagesTest.java  |  7 +++++++
 .../org/apache/freemarker/core/ASTDirAssignment.java   | 13 +++++++++++--
 .../freemarker/core/InvalidReferenceException.java     | 10 +++++-----
 3 files changed, 23 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/1af2e0c2/freemarker-core-test/src/test/java/org/apache/freemarker/core/MiscErrorMessagesTest.java
----------------------------------------------------------------------
diff --git a/freemarker-core-test/src/test/java/org/apache/freemarker/core/MiscErrorMessagesTest.java b/freemarker-core-test/src/test/java/org/apache/freemarker/core/MiscErrorMessagesTest.java
index 9f73968..81b9710 100644
--- a/freemarker-core-test/src/test/java/org/apache/freemarker/core/MiscErrorMessagesTest.java
+++ b/freemarker-core-test/src/test/java/org/apache/freemarker/core/MiscErrorMessagesTest.java
@@ -55,4 +55,11 @@ public class MiscErrorMessagesTest extends TemplateTest {
         assertEquals((Integer) 2, ((TemplateException) e).getLineNumber());
     }
     
+    @Test
+    public void incrementalAssignmentsTest() throws Exception {
+        assertErrorContains("<#assign x++>", "\"x\"", "++", "template namespace");
+        assertErrorContains("<#global x += 2>", "\"x\"", "+=", "global scope");
+        assertErrorContains("<#macro m><#local x--></#macro><@m/>", "\"x\"", "--", "local scope");
+    }
+    
 }

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/1af2e0c2/freemarker-core/src/main/java/org/apache/freemarker/core/ASTDirAssignment.java
----------------------------------------------------------------------
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/ASTDirAssignment.java b/freemarker-core/src/main/java/org/apache/freemarker/core/ASTDirAssignment.java
index 7efa95c..26a6d69 100644
--- a/freemarker-core/src/main/java/org/apache/freemarker/core/ASTDirAssignment.java
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/ASTDirAssignment.java
@@ -149,7 +149,7 @@ final class ASTDirAssignment extends ASTDirective {
             if (operatorType == OPERATOR_TYPE_PLUS_EQUALS) {  // Add or concat operation
                 if (lhoValue == null) {
                     throw InvalidReferenceException.getInstance(
-                            variableName, getOperatorTypeAsString(), env);
+                            scope, variableName, getOperatorTypeAsString(), env);
                 }
                 
                 value = valueExp.eval(env);
@@ -160,7 +160,7 @@ final class ASTDirAssignment extends ASTDirective {
                 if (lhoValue instanceof TemplateNumberModel) {
                     lhoNumber = _EvalUtils.modelToNumber((TemplateNumberModel) lhoValue, null);
                 } else if (lhoValue == null) {
-                    throw InvalidReferenceException.getInstance(variableName, getOperatorTypeAsString(), env);
+                    throw InvalidReferenceException.getInstance(scope, variableName, getOperatorTypeAsString(), env);
                 } else {
                     throw MessageUtils.newUnexpectedAssignmentTargetTypeException(
                             variableName, lhoValue, TemplateNumberModel.class, env);
@@ -281,4 +281,13 @@ final class ASTDirAssignment extends ASTDirective {
         }
     }
     
+    static String scopeAsString(int scope) {
+        switch (scope) {
+        case NAMESPACE: return "template namespace";
+        case LOCAL: return "local scope";
+        case GLOBAL: return "global scope";
+        default: throw new AssertionError("Unsupported scope: " + scope);
+        }
+    }
+    
 }

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/1af2e0c2/freemarker-core/src/main/java/org/apache/freemarker/core/InvalidReferenceException.java
----------------------------------------------------------------------
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/InvalidReferenceException.java b/freemarker-core/src/main/java/org/apache/freemarker/core/InvalidReferenceException.java
index 2cd4fd1..7edd087 100644
--- a/freemarker-core/src/main/java/org/apache/freemarker/core/InvalidReferenceException.java
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/InvalidReferenceException.java
@@ -139,16 +139,16 @@ public class InvalidReferenceException extends TemplateException {
     /**
      * Used for assignments that use operators like {@code +=}, when the target variable was null/missing. 
      */
-    static InvalidReferenceException getInstance(String missingAssignedVarName, String assignmentOperator,
+    static InvalidReferenceException getInstance(int scope, String missingAssignedVarName, String assignmentOperator,
             Environment env) {
         if (env != null && env.getFastInvalidReferenceExceptions()) {
             return FAST_INSTANCE;
         } else {
             final _ErrorDescriptionBuilder errDescBuilder = new _ErrorDescriptionBuilder(
-                            "The target variable of the assignment, ",
-                            new _DelayedJQuote(missingAssignedVarName),
-                            ", was null or missing, but the \"",
-                            assignmentOperator, "\" operator needs to get its value before assigning to it."
+                    "The target variable of the assignment, ",
+                    new _DelayedJQuote(missingAssignedVarName),
+                    ", was null or missing in the " + ASTDirAssignment.scopeAsString(scope) + ", and the \"",
+                    assignmentOperator, "\" operator must get its value from there before assigning to it."
                     );
             if (missingAssignedVarName.startsWith("$")) {
                 errDescBuilder.tips(TIP_NO_DOLLAR, TIP_MISSING_ASSIGNMENT_TARGET);


[2/2] incubator-freemarker git commit: Forward ported from 2.3-gae: Bug fixed: When string?split(separator) is called with "" as the argument, the string will be split to characters now. Earlier it has thrown an IllegalArgumentException (unless the r fla

Posted by dd...@apache.org.
Forward ported from 2.3-gae: Bug fixed: When string?split(separator) is called with "" as the argument, the string will be split to characters now. Earlier it has thrown an IllegalArgumentException (unless the r flag was specified).


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

Branch: refs/heads/3
Commit: 421f4747b2668329664bf3506100bcfc4a01259b
Parents: 1af2e0c
Author: ddekany <dd...@apache.org>
Authored: Tue Mar 20 18:58:37 2018 +0100
Committer: ddekany <dd...@apache.org>
Committed: Tue Mar 20 18:58:37 2018 +0100

----------------------------------------------------------------------
 .../core/BuiltInsForStringsBasic.java           |  2 +-
 .../freemarker/core/util/_StringUtils.java      | 56 ++++++++++++--------
 2 files changed, 35 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/421f4747/freemarker-core/src/main/java/org/apache/freemarker/core/BuiltInsForStringsBasic.java
----------------------------------------------------------------------
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/BuiltInsForStringsBasic.java b/freemarker-core/src/main/java/org/apache/freemarker/core/BuiltInsForStringsBasic.java
index 80429b8..1a5f1be 100644
--- a/freemarker-core/src/main/java/org/apache/freemarker/core/BuiltInsForStringsBasic.java
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/BuiltInsForStringsBasic.java
@@ -617,7 +617,7 @@ class BuiltInsForStringsBasic {
                         : 0;
                 String[] result;
                 if ((flags & RegexpHelper.RE_FLAG_REGEXP) == 0) {
-                    RegexpHelper.checkNonRegexpFlags("split", flags);
+                    RegexpHelper.checkNonRegexpFlags(key, flags);
                     result = _StringUtils.split(s, splitString,
                             (flags & RegexpHelper.RE_FLAG_CASE_INSENSITIVE) != 0);
                 } else {

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/421f4747/freemarker-core/src/main/java/org/apache/freemarker/core/util/_StringUtils.java
----------------------------------------------------------------------
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/util/_StringUtils.java b/freemarker-core/src/main/java/org/apache/freemarker/core/util/_StringUtils.java
index e553be1..3adf958 100644
--- a/freemarker-core/src/main/java/org/apache/freemarker/core/util/_StringUtils.java
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/util/_StringUtils.java
@@ -464,38 +464,50 @@ public class _StringUtils {
 
     /**
      * Splits a string at the specified string.
+     * 
+     * @param sep
+     *            The string that separates the items of the resulting array. If this is 0 length, then each character
+     *            will be a separate item in the array.
      */
     public static String[] split(String s, String sep, boolean caseInsensitive) {
-        String splitString = caseInsensitive ? sep.toLowerCase() : sep;
-        String input = caseInsensitive ? s.toLowerCase() : s;
-        int i, b, e;
-        int cnt;
-        String res[];
-        int ln = s.length();
-        int sln = sep.length();
+        int sepLn = sep.length();
 
-        if (sln == 0) throw new IllegalArgumentException(
-                "The separator string has 0 length");
+        String convertedS = caseInsensitive ? s.toLowerCase() : s;
+        int sLn = s.length();
+        
+        if (sepLn == 0) {
+            String[] res = new String[sLn];
+            for (int i = 0; i < sLn; i++) {
+                res[i] = String.valueOf(s.charAt(i));
+            }
+            return res;
+        }
 
-        i = 0;
-        cnt = 1;
-        while ((i = input.indexOf(splitString, i)) != -1) {
-            cnt++;
-            i += sln;
+        String splitString = caseInsensitive ? sep.toLowerCase() : sep;
+        String res[];
+        
+        {
+            int next = 0;
+            int count = 1;
+            while ((next = convertedS.indexOf(splitString, next)) != -1) {
+                count++;
+                next += sepLn;
+            }
+            res = new String[count];
         }
-        res = new String[cnt];
 
-        i = 0;
-        b = 0;
-        while (b <= ln) {
-            e = input.indexOf(splitString, b);
-            if (e == -1) e = ln;
-            res[i++] = s.substring(b, e);
-            b = e + sln;
+        int dst = 0;
+        int next = 0;
+        while (next <= sLn) {
+            int end = convertedS.indexOf(splitString, next);
+            if (end == -1) end = sLn;
+            res[dst++] = s.substring(next, end);
+            next = end + sepLn;
         }
         return res;
     }
 
+
     /**
      * Same as {@link #replace(String, String, String, boolean, boolean)} with two {@code false} parameters. 
      */