Skip to content

Commit

Permalink
Fix for #1492: MethodPointerExpression to ScriptBytecodeAdapter call
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jul 6, 2023
1 parent d996bea commit 97948e8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,44 @@ final class EvaluationEngineTests extends GroovyEclipseTestSuite {
}
}

@Test // https://github.com/groovy/groovy-eclipse/issues/1492
void testEvalSnippet4() {
def (launch, thread) = runToLine(3, '''\
|public class Main {
| public static void main(String[] args) {
| System.out.println("hello world");
| }
|}
|'''.stripMargin())

try {
IEvaluationResult result = evaluate('args.collect(String.&toUpperCase).first()', thread)
assert !result.hasErrors() : result.errorMessages[0]
assert result.value.valueString == 'FOO'
} finally {
launch.terminate()
}
}

@Test // https://github.com/groovy/groovy-eclipse/issues/1492
void testEvalSnippet5() {
def (launch, thread) = runToLine(3, '''\
|public class Main {
| public static void main(String[] args) {
| System.out.println("hello world");
| }
|}
|'''.stripMargin())

try {
IEvaluationResult result = evaluate('Arrays.stream(args).map(String::toUpperCase).skip(1).findFirst().get()', thread)
assert !result.hasErrors() : result.errorMessages[0]
assert result.value.valueString == 'BAR'
} finally {
launch.terminate()
}
}

//--------------------------------------------------------------------------

IEvaluationResult evaluate(String snippet, IThread thread) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.expr.BinaryExpression;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.expr.MethodPointerExpression;
import org.codehaus.groovy.ast.expr.StaticMethodCallExpression;
import org.codehaus.groovy.control.CompilationUnit;
import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.control.ParserPluginFactory;
import org.codehaus.groovy.control.Phases;
import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.runtime.ScriptBytecodeAdapter;

/**
* A class loader for the script to be evaluated that has a different parent
Expand Down Expand Up @@ -70,6 +73,9 @@ protected Class createClass(byte[] code, ClassNode classNode) {

@Override
protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource source) {
config = new CompilerConfiguration(config);
config.setPluginFactory(ParserPluginFactory.antlr4()); // for java parsing
config.getCompilationCustomizers().addAll(config.getCompilationCustomizers()); // GROOVY-9585
CompilationUnit compilationUnit = super.createCompilationUnit(config, source);

compilationUnit.addPhaseOperation(sourceUnit -> {
Expand All @@ -91,6 +97,11 @@ public Expression transform(Expression expression) {
return callX(varX("__comparator"), methodName, args(lhs, rhs));
}
}
if (expression instanceof MethodPointerExpression) {
MethodPointerExpression m = (MethodPointerExpression) expression;
return callX(classX(ScriptBytecodeAdapter.class), "getMethodPointer",
args(transform(m.getExpression()), transform(m.getMethodName())));
}
if (expression instanceof StaticMethodCallExpression) {
// to MethodCallExpression so that JDIMetaClass#invokeStaticMethod runs
StaticMethodCallExpression s = (StaticMethodCallExpression) expression;
Expand Down

0 comments on commit 97948e8

Please sign in to comment.