Skip to content

Commit

Permalink
use try-with-resources pattern for interpreter.enterScope
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredstehler committed Oct 5, 2015
1 parent 8824331 commit 1e5f17b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void leaveScope() {
}
}

class InterpreterScopeClosable implements AutoCloseable {
public class InterpreterScopeClosable implements AutoCloseable {

@Override
public void close() {
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/com/hubspot/jinjava/lib/fn/MacroFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.hubspot.jinjava.el.ext.AbstractCallableMethod;
import com.hubspot.jinjava.interpret.Context;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.JinjavaInterpreter.InterpreterScopeClosable;
import com.hubspot.jinjava.tree.Node;

/**
Expand All @@ -25,8 +26,13 @@ public class MacroFunction extends AbstractCallableMethod {

private final Context localContextScope;

public MacroFunction(List<Node> content, String name, LinkedHashMap<String, Object> argNamesWithDefaults,
boolean catchKwargs, boolean catchVarargs, boolean caller, Context localContextScope) {
public MacroFunction(List<Node> content,
String name,
LinkedHashMap<String, Object> argNamesWithDefaults,
boolean catchKwargs,
boolean catchVarargs,
boolean caller,
Context localContextScope) {
super(name, argNamesWithDefaults);
this.content = content;
this.catchKwargs = catchKwargs;
Expand All @@ -39,14 +45,11 @@ public MacroFunction(List<Node> content, String name, LinkedHashMap<String, Obje
public Object doEvaluate(Map<String, Object> argMap, Map<String, Object> kwargMap, List<Object> varArgs) {
JinjavaInterpreter interpreter = JinjavaInterpreter.getCurrent();

interpreter.enterScope();

try {
try (InterpreterScopeClosable c = interpreter.enterScope()) {
for (Map.Entry<String, Object> scopeEntry : localContextScope.getScope().entrySet()) {
if (scopeEntry.getValue() instanceof MacroFunction) {
interpreter.getContext().addGlobalMacro((MacroFunction) scopeEntry.getValue());
}
else {
} else {
interpreter.getContext().put(scopeEntry.getKey(), scopeEntry.getValue());
}
}
Expand All @@ -67,8 +70,6 @@ public Object doEvaluate(Map<String, Object> argMap, Map<String, Object> kwargMa
}

return result.toString();
} finally {
interpreter.leaveScope();
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/main/java/com/hubspot/jinjava/lib/tag/AutoEscapeTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.hubspot.jinjava.doc.annotations.JinjavaDoc;
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.JinjavaInterpreter.InterpreterScopeClosable;
import com.hubspot.jinjava.tree.Node;
import com.hubspot.jinjava.tree.TagNode;

Expand All @@ -16,8 +17,7 @@
@JinjavaSnippet(
code = "{% autoescape %}\n" +
"<div>Code to escape</div>\n" +
"{% endautoescape %}"
)
"{% endautoescape %}")
})
public class AutoEscapeTag implements Tag {
public static final String AUTOESCAPE_CONTEXT_VAR = "__auto3sc@pe__";
Expand All @@ -35,8 +35,7 @@ public String getEndTagName() {

@Override
public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
interpreter.enterScope();
try {
try (InterpreterScopeClosable c = interpreter.enterScope()) {
String boolFlagStr = StringUtils.trim(tagNode.getHelpers());
boolean escapeFlag = BooleanUtils.toBoolean(StringUtils.isNotBlank(boolFlagStr) ? boolFlagStr : "true");
interpreter.getContext().put(AUTOESCAPE_CONTEXT_VAR, escapeFlag);
Expand All @@ -48,8 +47,6 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
}

return result.toString();
} finally {
interpreter.leaveScope();
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/main/java/com/hubspot/jinjava/lib/tag/CallTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.hubspot.jinjava.doc.annotations.JinjavaDoc;
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.JinjavaInterpreter.InterpreterScopeClosable;
import com.hubspot.jinjava.lib.fn.MacroFunction;
import com.hubspot.jinjava.tree.TagNode;

Expand All @@ -22,7 +23,7 @@
" </div>\n" +
" {% endmacro %}\n\n" +

" {% call render_dialog('Hello World') %}\n" +
" {% call render_dialog('Hello World') %}\n" +
" This is a simple dialog rendered by using a macro and\n" +
" a call block.\n" +
" {% endcall %}"),
Expand All @@ -38,7 +39,7 @@
" </ul>\n" +
" {% endmacro %}\n\n" +

" {% call(user) dump_users(list_of_user) %}\n" +
" {% call(user) dump_users(list_of_user) %}\n" +
" <dl>\n" +
" <dl>Realname</dl>\n" +
" <dd>{{ user.realname|e }}</dd>\n" +
Expand All @@ -65,15 +66,12 @@ public String getEndTagName() {
public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
String macroExpr = "{{" + tagNode.getHelpers().trim() + "}}";

interpreter.enterScope();
try {
try (InterpreterScopeClosable c = interpreter.enterScope()) {
LinkedHashMap<String, Object> args = new LinkedHashMap<>();
MacroFunction caller = new MacroFunction(tagNode.getChildren(), "caller", args, false, false, true, interpreter.getContext());
interpreter.getContext().addGlobalMacro(caller);

return interpreter.render(macroExpr);
} finally {
interpreter.leaveScope();
}
}

Expand Down
44 changes: 19 additions & 25 deletions src/main/java/com/hubspot/jinjava/lib/tag/ForTag.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/**********************************************************************
Copyright (c) 2014 HubSpot Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
* Copyright (c) 2014 HubSpot Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**********************************************************************/
package com.hubspot.jinjava.lib.tag;

Expand All @@ -29,6 +29,7 @@
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
import com.hubspot.jinjava.interpret.InterpretException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.JinjavaInterpreter.InterpreterScopeClosable;
import com.hubspot.jinjava.tree.Node;
import com.hubspot.jinjava.tree.TagNode;
import com.hubspot.jinjava.util.ForLoop;
Expand Down Expand Up @@ -78,8 +79,7 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {

if ("in".equals(val)) {
break;
}
else {
} else {
loopVars.add(val);
inPos++;
}
Expand All @@ -93,8 +93,7 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
Object collection = interpreter.resolveELExpression(loopExpr, tagNode.getLineNumber());
ForLoop loop = ObjectIterator.getLoop(collection);

interpreter.enterScope();
try {
try (InterpreterScopeClosable c = interpreter.enterScope()) {
interpreter.getContext().put(LOOP, loop);

StringBuilder buff = new StringBuilder();
Expand All @@ -104,23 +103,20 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
// set item variables
if (loopVars.size() == 1) {
interpreter.getContext().put(loopVars.get(0), val);
}
else {
} else {
for (String loopVar : loopVars) {
if (Map.Entry.class.isAssignableFrom(val.getClass())) {
Map.Entry<String, Object> entry = (Entry<String, Object>) val;
Object entryVal = null;

if ("key".equals(loopVar)) {
entryVal = entry.getKey();
}
else if ("value".equals(loopVar)) {
} else if ("value".equals(loopVar)) {
entryVal = entry.getValue();
}

interpreter.getContext().put(loopVar, entryVal);
}
else {
} else {
try {
PropertyDescriptor[] valProps = Introspector.getBeanInfo(val.getClass()).getPropertyDescriptors();
for (PropertyDescriptor valProp : valProps) {
Expand All @@ -142,8 +138,6 @@ else if ("value".equals(loopVar)) {
}

return buff.toString();
} finally {
interpreter.leaveScope();
}

}
Expand Down

0 comments on commit 1e5f17b

Please sign in to comment.