Groovy: How do I evaluate expression which has functions inside Java code? -


here's sample expression:

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

i using groovy (2.0.5) expression parser don't want define functions (foo , bar) in groovy rather notified java calls when expression parsed. suppose can done using 1 of flavors of meta class. unfortunately tried combinations implementation of meta class (i.e., myexpandometaclass's invokemissingmethod) never called.

any appreciated.

sample java code:

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];         }     } } 

when run code, get:

groovy.lang.missingmethodexception: no signature of method: script1.foo() applicable argument types: (java.lang.string) values: [hello] possible solutions: run(), run(), find(), any(), is(java.lang.object), find(groovy.lang.closure)     @ org.codehaus.groovy.runtime.scriptbytecodeadapter.unwrap(scriptbytecodeadapter.java:55)     @ org.codehaus.groovy.runtime.callsite.pogometaclasssite.callcurrent(pogometaclasssite.java:78)     @ org.codehaus.groovy.runtime.callsite.callsitearray.defaultcallcurrent(callsitearray.java:49)     @ org.codehaus.groovy.runtime.callsite.abstractcallsite.callcurrent(abstractcallsite.java:133)     @ org.codehaus.groovy.runtime.callsite.abstractcallsite.callcurrent(abstractcallsite.java:141)     @ script1.run(script1.groovy:1)     @ groovy.lang.groovyshell.evaluate(groovyshell.java:518)     @ groovy.lang.groovyshell.evaluate(groovyshell.java:556)     @ groovy.lang.groovyshell.evaluate(groovyshell.java:527)     @ groovy.lang.script.evaluate(script.java:208)     @ com.barcap.edt.amd.analytics.operation.postprocess.expressionparsertest.function(expressionparsertest.java:50)     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)     @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:39)     @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:25)     @ java.lang.reflect.method.invoke(method.java:597)     @ org.junit.runners.model.frameworkmethod$1.runreflectivecall(frameworkmethod.java:44)     @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:15)     @ org.junit.runners.model.frameworkmethod.invokeexplosively(frameworkmethod.java:41)     @ org.junit.internal.runners.statements.invokemethod.evaluate(invokemethod.java:20)     @ org.junit.runners.blockjunit4classrunner.runnotignored(blockjunit4classrunner.java:79)     @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:71)     @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:49)     @ org.junit.runners.parentrunner$3.run(parentrunner.java:193)     @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:52)     @ org.junit.runners.parentrunner.runchildren(parentrunner.java:191)     @ org.junit.runners.parentrunner.access$000(parentrunner.java:42)     @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:184)     @ org.junit.runners.parentrunner.run(parentrunner.java:236)     @ org.eclipse.jdt.internal.junit4.runner.junit4testreference.run(junit4testreference.java:50)     @ org.eclipse.jdt.internal.junit.runner.testexecution.run(testexecution.java:38)     @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:467)     @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:683)     @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.run(remotetestrunner.java:390)     @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.main(remotetestrunner.java:197) 

this post example of using groovyshell compilerconfiguration, may useful.

using groovy 2.0.5, code:

import org.codehaus.groovy.control.compilerconfiguration  abstract class foobarscript extends script {     def foo(def x) {          println "hello foo"         return x      }      def bar(def x) {          println "hello 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 

gives output:

hello foo hello bar helloworld 

given original post requested java, script can moved following java class:

import groovy.lang.script;  public class foobarscript extends script {     public string foo(string x) {         system.out.println("hello foo");         return x;     }      public string bar(string x) {         system.out.println("hello bar");         return x;     }      public object run() { return null; } } 

where can imported groovy example


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -