不确定Groovy调用的输入参数:模糊方法重载

时间:2015-12-03 05:12:35

标签: grails groovy grails-3.0

我还是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]

2 个答案:

答案 0 :(得分:1)

初始化您的列表

class Email {
    String subject
    List<String> toEmail = []
    List<String> ccEmail = []
    List<String> bccEmail = []
    String body
}

答案 1 :(得分:0)

我无法从社区得到正确的答案,但是,公平地说,我认为很多这种吝啬的行为都归因于邮件插件的编写方式......或者可能是这样的我正在使用它。

我当时没有意识到这一点,但我正在向此插件传递一个闭包,其中包含用于初始化电子邮件参数的一系列语句。

在闭包内有这样的声明:

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)的条件,该变量是在我的应用程序范围内定义的。我假设这个值是通过引用传递的,所以在调用闭包时,值保持不变,但说实话,我不确定它是如何工作的,并且也会研究它。

这可能是一种“更漂亮”或更优雅的方式,而不是使用“if”语句。