Groovy:我如何评估Java代码中包含函数的表达式?

时间:2014-03-10 21:31:11

标签: java groovy

这是我的示例表达式:

foo("hello") + bar("world")

我使用groovy(2.0.5)作为表达式解析器,因此不希望在groovy中定义函数(foo和bar),而是希望在解析表达式时以Java调用的方式获得通知。我想这可以通过使用元类的一种风格来完成。不幸的是我尝试了所有组合,但我的meta类实现(即MyExpandoMetaClass的invokeMissingMethod)永远不会被调用。

感谢任何帮助。

示例Java代码:

MetaClassRegistryImpl.getInstance(0).setMetaClass(Script.class, new MyExpandoMetaClass());

String expression = "foo(\"hello\") + bar (\"world\")";
Binding binding = new Binding();
Script script = new GroovyShell(binding).parse(expression);
assertEquals("helloworld", script.evaluate(expression));

...

class MyExpandoMetaClass extends ExpandoMetaClass {
    public MyExpandoMetaClass() {
        super(Script.class, true);
    }

    @Override
    public Object invokeMissingMethod(Object instance, String methodName, Object[] arguments) {
        if (("foo".equals(methodName) || "bar".equals(methodName)) {
            return arguments[0];
        }
    }
}

当我运行此代码时,我得到:

groovy.lang.MissingMethodException: No signature of method: Script1.foo() is applicable for argument types: (java.lang.String) values: [hello]
Possible solutions: run(), run(), find(), any(), is(java.lang.Object), find(groovy.lang.Closure)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:78)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
    at Script1.run(Script1.groovy:1)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:518)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:556)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:527)
    at groovy.lang.Script.evaluate(Script.java:208)
    at com.barcap.edt.amd.analytics.operation.postprocess.ExpressionParserTest.function(ExpressionParserTest.java:50)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

1 个答案:

答案 0 :(得分:1)

This post是将GroovyShell与CompilerConfiguration一起使用的示例,这可能很有用。

使用Groovy 2.0.5,此代码:

import org.codehaus.groovy.control.CompilerConfiguration

abstract class FooBarScript extends Script {
    def foo(def x) { 
        println "hello from foo"
        return x 
    }

    def bar(def x) { 
        println "hello from bar" 
        return x 
    }
}

def compilerConfiguration = new CompilerConfiguration()
compilerConfiguration.scriptBaseClass = FooBarScript.class.name

def binding = new Binding()

def shell = new GroovyShell(this.class.classLoader, binding, compilerConfiguration)

String expression = "foo(\"hello\") + bar (\"world\")"

def result = shell.evaluate expression
println result

给出了这个输出:

hello from foo
hello from bar
helloworld

鉴于原始帖子请求Java,脚本可以移动到以下Java类:

import groovy.lang.Script;

public class FooBarScript extends Script {
    public String foo(String x) {
        System.out.println("hello from foo");
        return x;
    }

    public String bar(String x) {
        System.out.println("hello from bar");
        return x;
    }

    public Object run() { return null; }
}

这可以简单地导入Groovy示例