freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject [02/15] incubator-freemarker git commit: merge Environment#visit and Environment#visitByHiddingParent to further reduce call stack size
Date Sun, 13 Dec 2015 16:02:19 GMT
merge Environment#visit and Environment#visitByHiddingParent to further reduce call stack size


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

Branch: refs/heads/2.3-gae
Commit: 16f1559b222504964137060876085de5258b766a
Parents: 699d442
Author: Moritz Kleine <moritz.kleine@coremedia.com>
Authored: Thu Sep 10 12:57:20 2015 +0200
Committer: Moritz Kleine <moritz.kleine@coremedia.com>
Committed: Wed Oct 14 14:27:42 2015 +0200

----------------------------------------------------------------------
 src/main/java/freemarker/core/Environment.java  | 83 +++++++++-----------
 .../java/freemarker/core/IteratorBlock.java     | 10 +--
 src/main/java/freemarker/core/Macro.java        |  2 +-
 src/main/java/freemarker/core/SwitchBlock.java  |  4 +-
 .../java/freemarker/core/TemplateElement.java   |  2 +-
 5 files changed, 46 insertions(+), 55 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/16f1559b/src/main/java/freemarker/core/Environment.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/core/Environment.java b/src/main/java/freemarker/core/Environment.java
index 2e6b480..5a2dc88 100644
--- a/src/main/java/freemarker/core/Environment.java
+++ b/src/main/java/freemarker/core/Environment.java
@@ -298,7 +298,7 @@ public final class Environment extends Configurable {
             clearCachedValues();
             try {
                 doAutoImportsAndIncludes(this);
-                visit(getTemplate().getRootTreeNode());
+                visit(getTemplate().getRootTreeNode(), false);
                 // It's here as we must not flush if there was an exception.
                 if (getAutoFlush()) {
                     out.flush();
@@ -314,51 +314,42 @@ public final class Environment extends Configurable {
 
     /**
      * "Visit" the template element.
-     */
-    void visit(TemplateElement element)
-            throws TemplateException, IOException {
-        pushElement(element);
-        try {
-            accept(element);
-        } catch (TemplateException te) {
-            handleTemplateException(te);
-        } finally {
-            popElement();
-        }
-    }
-
-    private void accept(TemplateElement element) throws TemplateException, IOException {
-        TemplateElementsToVisit templateElementsToVisit = element.accept(this);
-        if(null != templateElementsToVisit) {
-            boolean hideInParent = templateElementsToVisit.isHideInParent();
-            for (TemplateElement templateElement : templateElementsToVisit.getTemplateElements())
{
-                if(null != templateElement) {
-                    if(hideInParent) {
-                        visitByHiddingParent(templateElement);
-                    } else {
-                        visit(templateElement);
-                    }
-                }
-            }
-        }
-    }
-
-    /**
-     * Instead of pushing into the element stack, we replace the top element for the time
the parameter element is
+     *
+     * Param 'hideInParent' controls how the instruction stack is handled. If it is set to
false, the current
+     * element is pushed to the instruction stack.
+     *
+     * If it is set to true, we replace the top element for the time the parameter element
is
      * visited, and then we restore the top element. The main purpose of this is to get rid
of elements in the error
      * stack trace that from user perspective shouldn't have a stack frame. The typical example
is
      * {@code [#if foo]...[@failsHere/]...[/#if]}, where the #if call shouldn't be in the
stack trace. (Simply marking
      * #if as hidden in stack traces would be wrong, because we still want to show #if when
its test expression fails.)
      */
-    void visitByHiddingParent(TemplateElement element)
-            throws TemplateException, IOException {
-        TemplateElement parent = replaceTopElement(element);
+    void visit(TemplateElement element, boolean hideInParent) throws IOException, TemplateException
{
+        TemplateElement parent = null;
+        if(hideInParent) {
+            parent = replaceTopElement(element);
+        } else {
+            pushElement(element);
+        }
         try {
-            accept(element);
+            TemplateElementsToVisit templateElementsToVisit = element.accept(this);
+            if(null != templateElementsToVisit) {
+                boolean hideInnerElementInParent = templateElementsToVisit.isHideInParent();
+                for (TemplateElement templateElementToVisit : templateElementsToVisit.getTemplateElements())
{
+                    if(null != templateElementToVisit) {
+                        visit(templateElementToVisit, hideInnerElementInParent);
+                    }
+                }
+            }
         } catch (TemplateException te) {
             handleTemplateException(te);
-        } finally {
-            replaceTopElement(parent);
+        }
+        finally {
+            if(null != parent) {
+                replaceTopElement(parent);
+            } else {
+                popElement();
+            }
         }
     }
 
@@ -432,7 +423,7 @@ public final class Environment extends Configurable {
                 if (tc == null || tc.onStart() != TransformControl.SKIP_BODY) {
                     do {
                         if (element != null) {
-                            visitByHiddingParent(element);
+                            visit(element, true);
                         }
                     } while (tc != null && tc.afterBody() == TransformControl.REPEAT_EVALUATION);
                 }
@@ -466,7 +457,7 @@ public final class Environment extends Configurable {
     /**
      * Visit a block using buffering/recovery
      */
-    void visitAttemptRecover(TemplateElement attemptBlock, RecoveryBlock recoveryBlock)
+     void visitAttemptRecover(TemplateElement attemptBlock, RecoveryBlock recoveryBlock)
             throws TemplateException, IOException {
         Writer prevOut = this.out;
         StringWriter sw = new StringWriter();
@@ -476,7 +467,7 @@ public final class Environment extends Configurable {
         boolean lastInAttemptBlock = inAttemptBlock;
         try {
             inAttemptBlock = true;
-            visitByHiddingParent(attemptBlock);
+            visit(attemptBlock, true);
         } catch (TemplateException te) {
             thrownException = te;
         } finally {
@@ -491,7 +482,7 @@ public final class Environment extends Configurable {
             }
             try {
                 recoveredErrorStack.add(thrownException);
-                visit(recoveryBlock);
+                visit(recoveryBlock, false);
             } finally {
                 recoveredErrorStack.remove(recoveredErrorStack.size() - 1);
             }
@@ -543,7 +534,7 @@ public final class Environment extends Configurable {
                 pushLocalContext(bodyCtx);
             }
             try {
-                visit(nestedContent);
+                visit(nestedContent, false);
             } finally {
                 if (invokingMacroContext.nestedContentParameterNames != null) {
                     popLocalContext();
@@ -2470,7 +2461,7 @@ public final class Environment extends Configurable {
 
         importMacros(includedTemplate);
         try {
-            visit(includedTemplate.getRootTreeNode());
+            visit(includedTemplate.getRootTreeNode(), false);
         } finally {
             if (parentReplacementOn) {
                 setParent(prevTemplate);
@@ -2591,7 +2582,7 @@ public final class Environment extends Configurable {
         try {
             StringWriter sw = new StringWriter();
             this.out = sw;
-            visit(te);
+            visit(te, false);
             return sw.toString();
         } finally {
             this.out = prevOut;
@@ -2689,7 +2680,7 @@ public final class Environment extends Configurable {
             Writer prevOut = out;
             out = newOut;
             try {
-                Environment.this.visit(element);
+                visit(element, false);
             } finally {
                 out = prevOut;
             }

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/16f1559b/src/main/java/freemarker/core/IteratorBlock.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/core/IteratorBlock.java b/src/main/java/freemarker/core/IteratorBlock.java
index d718aa1..a74b4fb 100644
--- a/src/main/java/freemarker/core/IteratorBlock.java
+++ b/src/main/java/freemarker/core/IteratorBlock.java
@@ -245,7 +245,7 @@ final class IteratorBlock extends TemplateElement {
                                 loopVar = iterModel.next();
                                 hasNext = iterModel.hasNext();
                                 if (nestedBlock != null) {
-                                    env.visitByHiddingParent(nestedBlock);
+                                    env.visit(nestedBlock, true);
                                 }
                                 index++;
                             }
@@ -258,7 +258,7 @@ final class IteratorBlock extends TemplateElement {
                         // allow one iterator() call.
                         openedIteratorModel = iterModel;
                         if (nestedBlock != null) {
-                            env.visitByHiddingParent(nestedBlock);
+                            env.visit(nestedBlock, true);
                         }
                     }
                 }
@@ -273,7 +273,7 @@ final class IteratorBlock extends TemplateElement {
                                 loopVar = seqModel.get(index);
                                 hasNext = (size > index + 1);
                                 if (nestedBlock != null) {
-                                    env.visitByHiddingParent(nestedBlock);
+                                    env.visit(nestedBlock, true);
                                 }
                             }
                         } catch (BreakInstruction.Break br) {
@@ -281,7 +281,7 @@ final class IteratorBlock extends TemplateElement {
                         }
                     } else {
                         if (nestedBlock != null) {
-                            env.visitByHiddingParent(nestedBlock);
+                            env.visit(nestedBlock, true);
                         }
                     }
                 }
@@ -293,7 +293,7 @@ final class IteratorBlock extends TemplateElement {
                 }
                 try {
                     if (nestedBlock != null) {
-                        env.visitByHiddingParent(nestedBlock);
+                        env.visit(nestedBlock, true);
                     }
                 } catch (BreakInstruction.Break br) {
                     // Silently exit "loop"

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/16f1559b/src/main/java/freemarker/core/Macro.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/core/Macro.java b/src/main/java/freemarker/core/Macro.java
index bfaab99..f2c9a08 100644
--- a/src/main/java/freemarker/core/Macro.java
+++ b/src/main/java/freemarker/core/Macro.java
@@ -188,7 +188,7 @@ public final class Macro extends TemplateElement implements TemplateModel
{
             sanityCheck(env);
             // Set default values for unspecified parameters
             if (getNestedBlock() != null) {
-                env.visit(getNestedBlock());
+                env.visit(getNestedBlock(), false);
             }
         }
 

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/16f1559b/src/main/java/freemarker/core/SwitchBlock.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/core/SwitchBlock.java b/src/main/java/freemarker/core/SwitchBlock.java
index e416b8d..b3dbe70 100644
--- a/src/main/java/freemarker/core/SwitchBlock.java
+++ b/src/main/java/freemarker/core/SwitchBlock.java
@@ -69,7 +69,7 @@ final class SwitchBlock extends TemplateElement {
                             EvalUtil.CMP_OP_EQUALS, "case==", cas.condition, cas.condition,
env);
                 }
                 if (processCase) {
-                    env.visitByHiddingParent(cas);
+                    env.visit(cas, true);
                     processedCase = true;
                 }
             }
@@ -77,7 +77,7 @@ final class SwitchBlock extends TemplateElement {
             // If we didn't process any nestedElements, and we have a default,
             // process it.
             if (!processedCase && defaultCase != null) {
-                env.visitByHiddingParent(defaultCase);
+                env.visit(defaultCase, true);
             }
         } catch (BreakInstruction.Break br) {}
         return null;

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/16f1559b/src/main/java/freemarker/core/TemplateElement.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/core/TemplateElement.java b/src/main/java/freemarker/core/TemplateElement.java
index 0f5be0f..21852b9 100644
--- a/src/main/java/freemarker/core/TemplateElement.java
+++ b/src/main/java/freemarker/core/TemplateElement.java
@@ -106,7 +106,7 @@ abstract public class TemplateElement extends TemplateObject {
     
     /**
      * Tells if the element should show up in error stack traces. If you think you need to
set this to {@code false} for
-     * an element, always consider if you should use {@link Environment#visitByHiddingParent(TemplateElement)}
instead.
+     * an element, always consider if you should pass true instead to {@link Environment#visit(TemplateElement,
boolean)}.
      * 
      * Note that this will be ignored for the top (current) element of a stack trace, as
that's always shown.
      */


Mime
View raw message