org.kohsuke.groovy.sandbox.StackVariableSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of groovy-sandbox Show documentation
Show all versions of groovy-sandbox Show documentation
Executes untrusted Groovy script safely
package org.kohsuke.groovy.sandbox;
import java.util.HashSet;
import java.util.Set;
/**
* Keep track of in-scope variables on the stack.
*
* In groovy, various statements implicitly create new scopes (as in Java), so we track them
* in a chain.
*
* This only tracks variables on stack (as opposed to field access and closure accessing variables
* in the calling context.)
*
* @author Kohsuke Kawaguchi
*/
final class StackVariableSet {
final StackVariableSet parent;
private final Set names = new HashSet();
StackVariableSet(StackVariableSet parent) {
this.parent = parent;
}
void declare(String name) {
names.add(name);
}
/**
* Is the variable of the given name in scope?
*/
boolean has(String name) {
for (StackVariableSet s=this; s!=null; s=s.parent)
if (s.names.contains(name))
return true;
return false;
}
}