Skip to content

Commit

Permalink
GroovyEngine: reviewed class statement evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Feb 22, 2020
1 parent d73cfea commit 60a831c
Showing 1 changed file with 4 additions and 26 deletions.
30 changes: 4 additions & 26 deletions groovy/src/main/java/org/jline/script/GroovyEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public class GroovyEngine implements ScriptEngine {
private Binding sharedData;
private Map<String,String> imports = new HashMap<>();
private Map<String,String> methods = new HashMap<>();
private Map<String,String> classes = new HashMap<>();

public GroovyEngine() {
this.sharedData = new Binding();
Expand Down Expand Up @@ -155,24 +154,15 @@ public Object execute(String statement) throws Exception {
if (methods.containsKey(name)) {
out = "def " + name + methods.get(name);
}
} else if (classDef(statement)) {
// do nothing
} else if (statement.equals("class")) {
out = classes.values();
} else if (statement.matches("class\\s+" + REGEX_VAR)) {
String name = statement.split("\\s+")[1];
if (classes.containsKey(name)) {
out = classes.get(name);
}
} else {
String e = "";
for (Map.Entry<String, String> entry : imports.entrySet()) {
e += entry.getValue()+"\n";
}
for (String c : classes.values()) {
e += c;
}
e += statement;
if (classDef(statement)) {
e += "; null";
}
out = shell.evaluate(e);
}
return out;
Expand Down Expand Up @@ -214,13 +204,7 @@ private boolean functionDef (String statement) throws Exception{
}

private boolean classDef (String statement) throws Exception{
boolean out = false;
Matcher m = PATTERN_CLASS_DEF.matcher(statement);
if(m.matches()){
out = true;
classes.put(m.group(1), statement);
}
return out;
return PATTERN_CLASS_DEF.matcher(statement).matches();
}

private void del(String var) {
Expand All @@ -242,14 +226,8 @@ private void del(String var) {
methods.remove(v);
}
}
if (classes.containsKey(v)) {
classes.remove(v);
}
}
}
if (classes.containsKey(var)) {
classes.remove(var);
}
}

@Override
Expand Down

0 comments on commit 60a831c

Please sign in to comment.