Skip to content

Commit

Permalink
cleanup: use java.util.Deque instead of java.util.Stack
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandy committed Apr 27, 2024
1 parent 60f154b commit 0cfb8d5
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Stack;
import java.util.Deque;
import javassist.CannotCompileException;
import javassist.CtClass;
import javassist.CtField;
Expand All @@ -24,7 +24,7 @@
*/
public class ControllersEnhancer extends Enhancer {

public static final ThreadLocal<Stack<String>> currentAction = new ThreadLocal<>();
public static final ThreadLocal<Deque<String>> currentAction = new ThreadLocal<>();

@Override
public void enhanceThisClass(final ApplicationClass applicationClass) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package play.classloading.enhancers;

import java.lang.reflect.Field;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;

import javassist.CtClass;
import javassist.CtMethod;
Expand Down Expand Up @@ -172,7 +173,7 @@ public interface LocalVariablesSupport {
*/
public static class LocalVariablesNamesTracer {

static final ThreadLocal<Stack<Map<String, Object>>> localVariables = new ThreadLocal<>();
static final ThreadLocal<Deque<Map<String, Object>>> localVariables = new ThreadLocal<>();

public static void checkEmpty() {
if (localVariables.get() != null && !localVariables.get().isEmpty()) {
Expand All @@ -186,9 +187,9 @@ public static void clear() {

public static void enter() {
if (localVariables.get() == null) {
localVariables.set(new Stack<Map<String, Object>>());
localVariables.set(new ArrayDeque<>());
}
localVariables.get().push(new HashMap<String, Object>());
localVariables.get().push(new HashMap<>());
}

public static void exit() {
Expand All @@ -199,7 +200,7 @@ public static void exit() {
}

public static Map<String, Object> locals() {
if (localVariables.get() != null && !localVariables.get().empty()) {
if (localVariables.get() != null && !localVariables.get().isEmpty()) {
return localVariables.get().peek();
}
return new HashMap<>();
Expand Down Expand Up @@ -251,7 +252,7 @@ public static List<String> getAllLocalVariableNames(Object o) {
if (getLocalVariables().get(variable) == o) {
allNames.add(variable);
}
if (o != null && o instanceof Number && o.equals(getLocalVariables().get(variable))) {
if (o instanceof Number && o.equals(getLocalVariables().get(variable))) {
allNames.add(variable);
}
}
Expand All @@ -262,17 +263,17 @@ public static Object getLocalVariable(String variable) {
return getLocalVariables().get(variable);
}

public static Stack<Map<String, Object>> getLocalVariablesStateBeforeAwait() {
Stack<Map<String, Object>> state = localVariables.get();
public static Deque<Map<String, Object>> getLocalVariablesStateBeforeAwait() {
Deque<Map<String, Object>> state = localVariables.get();
// must clear the ThreadLocal to prevent destroying the state when exit() is called due to
// continuations-suspend
localVariables.set(new Stack<Map<String, Object>>());
localVariables.set(new ArrayDeque<>());
return state;
}

public static void setLocalVariablesStateAfterAwait(Stack<Map<String, Object>> state) {
public static void setLocalVariablesStateAfterAwait(Deque<Map<String, Object>> state) {
if (state == null) {
state = new Stack<>();
state = new ArrayDeque<>();
}
localVariables.set(state);
}
Expand Down
4 changes: 2 additions & 2 deletions framework/src/play/mvc/ActionInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.concurrent.Future;

/**
Expand Down Expand Up @@ -103,7 +103,7 @@ private static void initActionContext(Http.Request request, Http.Response respon
Scope.Flash.current.set(Scope.Flash.restore());
CachedBoundActionMethodArgs.init();

ControllersEnhancer.currentAction.set(new Stack<>());
ControllersEnhancer.currentAction.set(new ArrayDeque<>());
}

public static void invoke(Http.Request request, Http.Response response) {
Expand Down
4 changes: 2 additions & 2 deletions framework/src/play/mvc/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.concurrent.Future;

import org.apache.commons.javaflow.Continuation;
Expand Down Expand Up @@ -1122,7 +1122,7 @@ private static void storeOrRestoreDataStateForContinuations(Boolean isRestoring)
// we are restoring after suspend

// localVariablesState
Stack<Map<String, Object>> localVariablesState = (Stack<Map<String, Object>>) Http.Request.current().args
Deque<Map<String, Object>> localVariablesState = (Deque<Map<String, Object>>) Http.Request.current().args
.remove(ActionInvoker.CONTINUATIONS_STORE_LOCAL_VARIABLE_NAMES);
LocalvariablesNamesEnhancer.LocalVariablesNamesTracer.setLocalVariablesStateAfterAwait(localVariablesState);

Expand Down
2 changes: 1 addition & 1 deletion framework/src/play/templates/GroovyTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ protected String internalRender(Map<String, Object> args) {
binding.setVariable("_response_encoding", currentResponse.encoding);
}
StringWriter writer = null;
Boolean applyLayouts = false;
boolean applyLayouts = false;

// must check if this is the first template being rendered..
// If this template is called from inside another template,
Expand Down
62 changes: 42 additions & 20 deletions framework/src/play/templates/TagContext.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
package play.templates;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Stack;

/**
* Tag Context (retrieve who call you)
*/
public class TagContext {

private static final ThreadLocal<Stack<TagContext>> currentStack = new ThreadLocal<>();
private static final ThreadLocal<Deque<TagContext>> currentStack = new ThreadLocal<>();

public final String tagName;
public final Map<String, Object> data = new HashMap<>();

public final String tagName;

public TagContext(String tagName) {
this.tagName = tagName;
}

public static void init() {
currentStack.set(new Stack<TagContext>());
currentStack.set(new ArrayDeque<>());
enterTag("ROOT");
}

public static void enterTag(String name) {
currentStack.get().add(new TagContext(name));
currentStack.get().push(new TagContext(name));
}

public static void exitTag() {
Expand All @@ -36,35 +39,56 @@ public static TagContext current() {
}

public static TagContext parent() {
if(currentStack.get().size() < 2) {
return null;
Iterator<TagContext> it = currentStack.get().iterator();
if (it.hasNext()) {
// skip
it.next();

if (it.hasNext()) {
return it.next();
}
}
return currentStack.get().get(currentStack.get().size()-2);

return null;
}

public static boolean hasParentTag(String name) {
for(int i=currentStack.get().size()-1; i>=0; i--) {
if(name.equals(currentStack.get().get(i).tagName)) {
for (TagContext current : currentStack.get()) {
if (name.equals(current.tagName)) {
return true;
}
}
return false;
}

public static TagContext parent(String name) {
for(int i=currentStack.get().size()-2; i>=0; i--) {
if(name.equals(currentStack.get().get(i).tagName)) {
return currentStack.get().get(i);
Iterator<TagContext> it = currentStack.get().iterator();
if (it.hasNext()) {
// skip head
it.next();

while (it.hasNext()) {
TagContext parent = it.next();
if (name.equals(parent.tagName)) {
return parent;
}
}
}
return null;
}

public static TagContext parent(String... names) {
for (int i = currentStack.get().size() - 2; i >= 0; i--) {
for (String name : names) {
if (name.equals(currentStack.get().get(i).tagName)) {
return currentStack.get().get(i);
Iterator<TagContext> it = currentStack.get().iterator();
if (it.hasNext()) {
// skip head
it.next();

while (it.hasNext()) {
TagContext parent = it.next();
for (String name : names) {
if (name.equals(parent.tagName)) {
return parent;
}
}
}
}
Expand All @@ -73,9 +97,7 @@ public static TagContext parent(String... names) {

@Override
public String toString() {
return tagName+""+data;
return this.tagName + this.data;
}



}
9 changes: 5 additions & 4 deletions framework/src/play/templates/TemplateCompiler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package play.templates;

import java.util.Stack;
import java.util.ArrayDeque;
import java.util.Deque;
import play.Logger;
import play.vfs.VirtualFile;
import play.exceptions.TemplateCompilationException;
Expand Down Expand Up @@ -29,7 +30,7 @@ public BaseTemplate compile(VirtualFile file) {
}

protected final StringBuilder compiledSource = new StringBuilder();
protected final Stack<Tag> tagsStack = new Stack<>();
protected final Deque<Tag> tagsStack = new ArrayDeque<>();
protected BaseTemplate template;
protected TemplateParser parser;
protected boolean doNextScan = true;
Expand Down Expand Up @@ -99,7 +100,7 @@ protected void generate(BaseTemplate template) {
end();

// Check tags imbrication
if (!tagsStack.empty()) {
if (!tagsStack.isEmpty()) {
Tag tag = tagsStack.peek();
throw new TemplateCompilationException(template, tag.startLine, "#{" + tag.name + "} is not closed.");
}
Expand Down Expand Up @@ -139,7 +140,7 @@ protected void markLine(int line) {
}

protected void println() {
compiledSource.append("\n");
compiledSource.append('\n');
currentLine++;
}

Expand Down
Loading

0 comments on commit 0cfb8d5

Please sign in to comment.