复杂对象 prblm stateme2 的常规闭包
我有一个 java 接口
public interface IPerson {
Person addPerson(String name );
Person addPerson(String name1,String name2);
Person addPerson(String name,String[] details);
Person addPerson(String name,String name1,String[] details);
Person addPerson(String name,List<String> details);
}
PersonImpl .java
是:
class PersonImpl implemets Iperson {
..
// and interface methods implemtation
}
我的 person.java 看起来像
class Person {
def firstName;
def lastName;
}
我的 PersonTest.groovy
看起来像
def PersonImpl person = new PersonImpl();
person.addPerson("anish")
person.addPerson("anish","nath")
person.addPerson("john","smith")
person.addPerson("tim","yates")
def list=[];
list.add("abc")
list.add("qpr")
person.addPerson("anish",list)
person.addPerson("nath","11", [".docsDevNmAccessStatus.1", "Integer", "4"])
person.addPerson("nath","11", [".docsDevNmAccessStatus.1", "String", "4"])
有什么办法为这个接口定义 DSL,以便我轻松调用 addOperation?
问题是IPerson
接口无法更改。
我怎样才能写 dsl 类似的东西
addPerson "anihs" "nath" //call to person.addPerson("anish","nath")
addPerson "tim" "kates"
//simlary of other interface method any suggestion
I have an java interface
public interface IPerson {
Person addPerson(String name );
Person addPerson(String name1,String name2);
Person addPerson(String name,String[] details);
Person addPerson(String name,String name1,String[] details);
Person addPerson(String name,List<String> details);
}
With PersonImpl .java
being:
class PersonImpl implemets Iperson {
..
// and interface methods implemtation
}
and my person.java looks like
class Person {
def firstName;
def lastName;
}
And my PersonTest.groovy
looks like
def PersonImpl person = new PersonImpl();
person.addPerson("anish")
person.addPerson("anish","nath")
person.addPerson("john","smith")
person.addPerson("tim","yates")
def list=[];
list.add("abc")
list.add("qpr")
person.addPerson("anish",list)
person.addPerson("nath","11", [".docsDevNmAccessStatus.1", "Integer", "4"])
person.addPerson("nath","11", [".docsDevNmAccessStatus.1", "String", "4"])
Is there any way to define the DSL for this interface so that i easily called addOperation easily?
The problem is that the IPerson
interface cannot be changed.
how can i write dsl something like
addPerson "anihs" "nath" //call to person.addPerson("anish","nath")
addPerson "tim" "kates"
//simlary of other interface method any suggestion
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Groovy 中,像
addPerson "anihs" "nath"
这样的东西实际上是不允许的。如果您想要两个参数,则必须使用逗号。所以你能得到的最好的就是addPerson "anihs", "nath"
。但这个方法调用只是悬而未决,上下文丢失了。当然,一个非常简单的版本是:尽管我不确定这对您来说是否足够。
In Groovy something like
addPerson "anihs" "nath"
is not really allowed. If you wanted two arguments you would have to use a comma. So the best you can get isaddPerson "anihs", "nath"
. But this method calls is just hanging in the air, the context is missing. One quite easy version would be of course:though I am not sure this is enough for you.