Skip to content

Commit

Permalink
Groovy REPL: improve closure vars type detection, add better test for…
Browse files Browse the repository at this point in the history
… immutable variables in ObjectCloner and fix ClassCastException in Utils.groovy
  • Loading branch information
mattirn committed Sep 29, 2020
1 parent 6ece81d commit f8cec7b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
4 changes: 4 additions & 0 deletions groovy/src/main/groovy/org/jline/groovy/Utils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/
package org.jline.groovy

import org.codehaus.groovy.runtime.HandleMetaClass

import java.nio.file.Path
import org.jline.script.GroovyEngine.Format
import groovy.json.JsonOutput
Expand Down Expand Up @@ -38,6 +40,8 @@ class Utils {
def out = [:]
if (object instanceof Closure) {
out['closure'] = object.getClass().getName()
} else if (object instanceof HandleMetaClass) {
out['object'] = object.toString()
} else {
out = object != null ? object.properties : null
}
Expand Down
16 changes: 11 additions & 5 deletions groovy/src/main/java/org/jline/script/GroovyEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.stream.Collectors;

import groovy.lang.*;
import org.apache.groovy.ast.tools.ImmutablePropertyUtils;
import org.codehaus.groovy.control.messages.SyntaxErrorMessage;
import org.codehaus.groovy.syntax.SyntaxException;
import org.jline.builtins.Nano.SyntaxHighlighter;
Expand Down Expand Up @@ -1102,7 +1103,12 @@ private String stripVarType(String statement) {
private String defineArgs(String[] args) {
StringBuilder out = new StringBuilder();
for (String v : args) {
out.append(v).append(" = null; ");
Matcher matcher = PATTERN_TYPE_VAR.matcher(v);
if (matcher.matches()) {
out.append(constructVariable(matcher.group(1), matcher.group(2)));
} else {
out.append(v).append(" = null; ");
}
}
return out.toString();
}
Expand All @@ -1113,9 +1119,9 @@ private String constructVariable(String type, String name) {
|| type.equals("int") || type.equals("Integer") || type.matches("[L|l]ong")
|| type.matches("[F|f]loat") || type.matches("[D|d]ouble")
|| type.matches("[B|b]oolean") || type.equals("char") || type.equals("Character")) {
out = name + " = (" + type + ")0";
out = name + " = (" + type + ")0; ";
} else if (type.matches("[A-Z].*")) {
out = "try {" + name + " = new " + type + "() } catch (Exception e) {" + name + " = null}";
out = "try {" + name + " = new " + type + "() } catch (Exception e) {" + name + " = null}; ";
}
return out;
}
Expand Down Expand Up @@ -1570,8 +1576,8 @@ public ObjectCloner() {
* Shallow copy of the object using java Cloneable clone() method.
*/
public Object clone(Object obj) {
if (obj == null || obj instanceof String || obj instanceof Integer || obj instanceof Exception
|| obj instanceof Closure) {
if (obj == null || ImmutablePropertyUtils.builtinOrMarkedImmutableClass(obj.getClass())
|| obj instanceof Exception || obj instanceof Closure) {
return obj;
}
Object out;
Expand Down

0 comments on commit f8cec7b

Please sign in to comment.