问题描述
从我开始使用Groovy已经过去了几天。但是随着所有的阅读和冲浪,我还没有完全弄清楚如何实现我的想法。所以,请原谅我作为初学者。感谢您的帮助,一如既往。我想实现的是:我有一个Java类,比如<$ (code> getMethod(), postMethod()的c $ c> ServiceClass 等等)做出一些REST GET / POST请求(在它自己的情况下,它工作正常)。现在,我想公开一个DSL,以便最终用户可以这样说: callService ServiceClass方法getMethod 并且执行 ServiceClass .getMethod()
我一直在尝试的是:我得到了一个 userCommand 文件放在某处,现在只读: callService ServiceClass
我有一个<$ c $
class示例{
def sample.groovy srvc
def callService(srvc){
this.srvc = srvc
调用$ srvc
}
}
我得到了一个 integrator.groovy 文件:
//必要的输入
class Integrator {
def sample = new Sample()
def binding = new Binding([
sample:sample,
ServiceClass:new ServiceClass(),
callService:sample。& callService])
def sh ell = new GroovyShell(binding)
def runIt(){
shell.evaluate(userCommand)
}
}
$ c
然后从我的Java应用程序运行它,我这样做:
Integator i = new Integrator()
i.runIt();
}
但这不起作用。使用上面的语法说:
任何人都可以告诉我如何传递参数并创建对象实例?
解决方案更新:考虑以下 userCommand 文件:
$ b文件 userCommand :
callService ServiceClass getMethod
groovy将解析为 callService(ServiceClass) .getGetMethod()。因此,您需要一个 getProperty 方法,将调用重定向到正确的方法:
文件 Dsl.groovy :
class Dsl {
static void main(args){
新的Integrator()。runIt()
}
}
class DslDelegate {
def service
def callService(service){
this.service = service
this
}
$ b $ def getProperty(String prop){
if(prop ==getMethod){service.getMethod( )}
else {throw new RuntimeException(Unrecognized property'$ prop')}
}
}
class ServiceClass {
def getMethod( ){serviceClass getMethod}
}
$ b $ class Integrator {
def dslDelegate = new DslDelegate()
def binding = new Binding([
ServiceClass :new ServiceClass(),
callService:dslDelegate。& callService])
def shell = new GroovyShell(binding)
def run It(){
assert shell.evaluate(new File(userCommand))==
serviceClass getMethod
}
}
$ c $注意我重命名了 Sample 类,所以它变成了一个委托给<$ c $>的类。C>服务类。现在它分离了DSL /服务的职责。
您也可以执行 callService ServiceClass方法getMethod ,但它需要更多的代码。
It has been a couple of days since I started with Groovy. But with all the reading and surfing, I haven't quite been able to figure out how to accomplish what I have in mind. So, please excuse me as a beginner. Would be much grateful for your help, as always.
What I want to achieve is this: I have a Java class, say ServiceClass that has some methods (getMethod(), postMethod() etc) to make some REST GET/POST requests (on its own, it works fine). Now, I want to expose a DSL so that the end-user may just say something like: callService ServiceClass method getMethod and I have the execution of ServiceClass.getMethod()
What I have been trying so far is this: I got a userCommand file placed somewhere, that for now just reads: callService ServiceClass
I have a sample.groovy that just does this now:
class Sample { def srvc def callService(srvc) { this.srvc = srvc "Calling $srvc" } }I got an integrator.groovy file that has:
//necessary imports class Integrator{ def sample = new Sample() def binding = new Binding([ sample:sample, ServiceClass: new ServiceClass(), callService:sample.&callService ]) def shell = new GroovyShell(binding) def runIt() { shell.evaluate("userCommand") } }And then to run it from my Java application, I am doing:
public static void main(String[] args) { Integator i = new Integrator() i.runIt(); }But this is just not working. With the above syntax it says:
Could anyone please tell me how do I pass around parameters and create object instances?
解决方案Update: Consider the following userCommand file:
File userCommand:
callService ServiceClass getMethodWhich will be parsed by groovy as callService(ServiceClass).getGetMethod(). So you need a getProperty method which reroute the call to the correct method:
File Dsl.groovy:
class Dsl { static void main(args) { new Integrator().runIt() } } class DslDelegate { def service def callService(service) { this.service = service this } def getProperty(String prop) { if (prop == "getMethod") { service.getMethod() } else { throw new RuntimeException("Unrecognized property '$prop'") } } } class ServiceClass { def getMethod() { "serviceClass getMethod" } } class Integrator{ def dslDelegate = new DslDelegate() def binding = new Binding([ ServiceClass: new ServiceClass(), callService:dslDelegate.&callService ]) def shell = new GroovyShell(binding) def runIt() { assert shell.evaluate(new File("userCommand")) == "serviceClass getMethod" } }Note I renamed the Sample class, so it becomes a delegator to ServiceClass. Now it separates DSL/Service responsibilities.
You can do callService ServiceClass method getMethod too, but it requires more code.
这篇关于Groovy DSL与Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!