Groovy DSL与Java [英] Groovy DSL with Java
问题描述
我想实现的是:我有一个Java类,比如<$ (code> getMethod(), postMethod()
的c $ c> ServiceClass 等等)做出一些REST GET / POST请求(在它自己的情况下,它工作正常)。现在,我想公开一个DSL,以便最终用户可以这样说: callService ServiceClass方法getMethod
并且执行 ServiceClass .getMethod()
我一直在尝试的是:我得到了一个 userCommand
文件放在某处,现在只读: callService ServiceClass
我有一个<$ c $
class示例{
srvc
defsample.groovy
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应用程序运行它,我这样做:
public static void main(String [] args){
Integator i = new Integrator()
i.runIt();
}
但这不起作用。使用上面的语法说:
线程main中的异常java.lang.NullPointerException:无法在null对象上调用方法setVariable() ....
任何人都可以告诉我如何传递参数并创建对象实例?
更新:考虑以下 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:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke method setVariable() on null object....
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 getMethod
Which 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!