Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Groovy:如何计算在Java代码中包含函数的表达式?_Java_Groovy - Fatal编程技术网

Groovy:如何计算在Java代码中包含函数的表达式?

Groovy:如何计算在Java代码中包含函数的表达式?,java,groovy,Java,Groovy,下面是我的示例表达式: foo("hello") + bar("world") 我使用groovy(2.0.5)作为表达式解析器,因此不想在groovy中定义函数(foo和bar),而是希望在解析表达式时得到Java调用通知。我想这可以通过使用元类的一种风格来实现。不幸的是,我尝试了所有的组合,但我的meta类实现(即MyExpandMetaClass的invokeMissingMethod)从未被调用 感谢您的帮助 示例Java代码: MetaClassRegistryImpl.getIns

下面是我的示例表达式:

foo("hello") + bar("world")
我使用groovy(2.0.5)作为表达式解析器,因此不想在groovy中定义函数(foo和bar),而是希望在解析表达式时得到Java调用通知。我想这可以通过使用元类的一种风格来实现。不幸的是,我尝试了所有的组合,但我的meta类实现(即MyExpandMetaClass的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));

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

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)
是与编译器配置一起使用的示例,可能很有用

使用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
考虑到原始post请求的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示例中

谢谢Michael。正如我所说,我不想在groovy中定义foo和bar,而是在Java本身中定义(我甚至可以自己调用foo/bar 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; }
}