不确定 Groovy 调用的输入参数:不明确的方法重载
Not sure of input parameters for Groovy call: Ambiguous method overloading
我对 groovy/grails 还是个新手,但是当我尝试调用 Grails 3.0.9 的邮件插件时,我无法弄清楚以下语法有什么问题:
class Email {
String subject;
List<String> toEmail;
List<String> ccEmail;
List<String> bccEmail;
String body;
}
Email email = new Email(toEmail: ["test@mail.com"], body: "Hi", subject: "Yo")
mailService.sendMail {
to email.toEmail.toArray()
from "no-reply@fake.com"
cc email.ccEmail?.toArray()
bcc email.bccEmail?.toArray()
subject email.subject
body email.body
}
当我没有人要抄送时,在 cc 参数行抛出以下异常。但是,我本以为 elvis 运算符只会返回 null,一切都会很好。我错过了什么?我觉得如果没有数据,我可能根本不应该设置 cc 和 bcc 字段,但我不确定为什么这会导致问题(也许它是插件内部的)以及我如何编码解决这个问题边缘情况(单独构建参数并将它们作为变量传递?)
groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method org.springframework.util.Assert#notEmpty.
Cannot resolve which method to invoke for [null, class java.lang.String] due to overlapping prototypes between:
[interface java.util.Collection, class java.lang.String]
[interface java.util.Map, class java.lang.String]
at groovy.lang.MetaClassImpl.chooseMostSpecificParams(MetaClassImpl.java:3241) ~[groovy-2.4.5.jar:2.4.5]
at groovy.lang.MetaClassImpl.chooseMethodInternal(MetaClassImpl.java:3194) ~[groovy-2.4.5.jar:2.4.5]
at groovy.lang.MetaClassImpl.chooseMethod(MetaClassImpl.java:3137) ~[groovy-2.4.5.jar:2.4.5]
at groovy.lang.MetaClassImpl.pickStaticMethod(MetaClassImpl.java:1508) ~[groovy-2.4.5.jar:2.4.5]
at groovy.lang.MetaClassImpl.retrieveStaticMethod(MetaClassImpl.java:1404) ~[groovy-2.4.5.jar:2.4.5]
at groovy.lang.MetaClassImpl.createStaticSite(MetaClassImpl.java:3383) ~[groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallStaticSite(CallSiteArray.java:77) [groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallSite(CallSiteArray.java:162) [groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) [groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) [groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133) [groovy-2.4.5.jar:2.4.5]
at grails.plugins.mail.MailMessageBuilder.cc(MailMessageBuilder.groovy:214) ~[mail-2.0.0.RC4.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_51]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_51]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_51]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_51]
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93) ~[groovy-2.4.5.jar:2.4.5]
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325) ~[groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:384) ~[groovy-2.4.5.jar:2.4.5]
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1019) ~[groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:69) ~[groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52) [groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154) [groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166) [groovy-2.4.5.jar:2.4.5]
at com.doctorsorders.email.EmailService$_sendEmail_closure1.doCall(EmailService.groovy:19) ~[main/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_51]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_51]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_51]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_51]
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93) ~[groovy-2.4.5.jar:2.4.5]
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325) ~[groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294) ~[groovy-2.4.5.jar:2.4.5]
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1019) ~[groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42) ~[groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) [groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) [groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125) [groovy-2.4.5.jar:2.4.5]
初始化您的列表
class Email {
String subject
List<String> toEmail = []
List<String> ccEmail = []
List<String> bccEmail = []
String body
}
我没能从社区得到正确的答案,但是,公平地说,我认为很多 finnicky 行为都归因于邮件插件的编写方式......或者可能是我使用它的方式。
我当时没有意识到,但我正在向这个插件传递一个闭包,其中包含用于初始化电子邮件参数的一系列语句。
闭包中有这样的语句:
bcc email.bccEmail?.toArray()
在没有指定电子邮件时会破坏插件,因为 "bcc" var 已创建并且此插件的实现现在需要一个值。我通过以下方式实现闭包来解决这个问题:
Closure mailServiceArgs = {
to email.toEmail.toArray()
from fromEmail
subject email.subject
body email.body
if(email.ccEmail) cc email.ccEmail.toArray()
if(email.bccEmail) bcc email.bccEmail.toArray()
}
然而,这提出了几个问题,因为我正在用一个 if 语句编写我的闭包,该语句是基于变量 (email.ccEmail) 的状态的条件,该变量是在我的范围内定义的应用。我假设此值是通过引用传递的,因此在调用闭包时,该值保持不变,但老实说,我不确定它是如何工作的,也会研究它。
可能有 'prettier' 或更优雅的方法来执行此操作,而不是使用 'if' 语句。
我对 groovy/grails 还是个新手,但是当我尝试调用 Grails 3.0.9 的邮件插件时,我无法弄清楚以下语法有什么问题:
class Email {
String subject;
List<String> toEmail;
List<String> ccEmail;
List<String> bccEmail;
String body;
}
Email email = new Email(toEmail: ["test@mail.com"], body: "Hi", subject: "Yo")
mailService.sendMail {
to email.toEmail.toArray()
from "no-reply@fake.com"
cc email.ccEmail?.toArray()
bcc email.bccEmail?.toArray()
subject email.subject
body email.body
}
当我没有人要抄送时,在 cc 参数行抛出以下异常。但是,我本以为 elvis 运算符只会返回 null,一切都会很好。我错过了什么?我觉得如果没有数据,我可能根本不应该设置 cc 和 bcc 字段,但我不确定为什么这会导致问题(也许它是插件内部的)以及我如何编码解决这个问题边缘情况(单独构建参数并将它们作为变量传递?)
groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method org.springframework.util.Assert#notEmpty.
Cannot resolve which method to invoke for [null, class java.lang.String] due to overlapping prototypes between:
[interface java.util.Collection, class java.lang.String]
[interface java.util.Map, class java.lang.String]
at groovy.lang.MetaClassImpl.chooseMostSpecificParams(MetaClassImpl.java:3241) ~[groovy-2.4.5.jar:2.4.5]
at groovy.lang.MetaClassImpl.chooseMethodInternal(MetaClassImpl.java:3194) ~[groovy-2.4.5.jar:2.4.5]
at groovy.lang.MetaClassImpl.chooseMethod(MetaClassImpl.java:3137) ~[groovy-2.4.5.jar:2.4.5]
at groovy.lang.MetaClassImpl.pickStaticMethod(MetaClassImpl.java:1508) ~[groovy-2.4.5.jar:2.4.5]
at groovy.lang.MetaClassImpl.retrieveStaticMethod(MetaClassImpl.java:1404) ~[groovy-2.4.5.jar:2.4.5]
at groovy.lang.MetaClassImpl.createStaticSite(MetaClassImpl.java:3383) ~[groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallStaticSite(CallSiteArray.java:77) [groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallSite(CallSiteArray.java:162) [groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) [groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) [groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133) [groovy-2.4.5.jar:2.4.5]
at grails.plugins.mail.MailMessageBuilder.cc(MailMessageBuilder.groovy:214) ~[mail-2.0.0.RC4.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_51]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_51]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_51]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_51]
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93) ~[groovy-2.4.5.jar:2.4.5]
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325) ~[groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:384) ~[groovy-2.4.5.jar:2.4.5]
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1019) ~[groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:69) ~[groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52) [groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154) [groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166) [groovy-2.4.5.jar:2.4.5]
at com.doctorsorders.email.EmailService$_sendEmail_closure1.doCall(EmailService.groovy:19) ~[main/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_51]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_51]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_51]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_51]
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93) ~[groovy-2.4.5.jar:2.4.5]
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325) ~[groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294) ~[groovy-2.4.5.jar:2.4.5]
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1019) ~[groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42) ~[groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) [groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) [groovy-2.4.5.jar:2.4.5]
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125) [groovy-2.4.5.jar:2.4.5]
初始化您的列表
class Email {
String subject
List<String> toEmail = []
List<String> ccEmail = []
List<String> bccEmail = []
String body
}
我没能从社区得到正确的答案,但是,公平地说,我认为很多 finnicky 行为都归因于邮件插件的编写方式......或者可能是我使用它的方式。
我当时没有意识到,但我正在向这个插件传递一个闭包,其中包含用于初始化电子邮件参数的一系列语句。
闭包中有这样的语句:
bcc email.bccEmail?.toArray()
在没有指定电子邮件时会破坏插件,因为 "bcc" var 已创建并且此插件的实现现在需要一个值。我通过以下方式实现闭包来解决这个问题:
Closure mailServiceArgs = {
to email.toEmail.toArray()
from fromEmail
subject email.subject
body email.body
if(email.ccEmail) cc email.ccEmail.toArray()
if(email.bccEmail) bcc email.bccEmail.toArray()
}
然而,这提出了几个问题,因为我正在用一个 if 语句编写我的闭包,该语句是基于变量 (email.ccEmail) 的状态的条件,该变量是在我的范围内定义的应用。我假设此值是通过引用传递的,因此在调用闭包时,该值保持不变,但老实说,我不确定它是如何工作的,也会研究它。
可能有 'prettier' 或更优雅的方法来执行此操作,而不是使用 'if' 语句。