【问题标题】:Can't compile trait using gmavenplus plugin无法使用 gmavenplus 插件编译特征
【发布时间】:2015-08-27 03:42:05
【问题描述】:

我有一个特点:

trait AbstractSender {

    abstract SentTrigger sendMail(Mail main)

    SentTrigger sentTrigger(Mail mail){
        //do smth here
    }
}

我有一堂课:

class EmailSender implements AbstractSender{

    @Override
    SentTrigger sendMail(Mail mail){
        //do some stuff
    }
}

我尝试使用 gmavenplus 插件编译它:

<plugin>
                    <groupId>org.codehaus.gmavenplus</groupId>
                    <artifactId>gmavenplus-plugin</artifactId>
                    <version>1.5</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>addSources</goal>
                                <goal>addTestSources</goal>
                                <goal>generateStubs</goal>
                                <goal>compile</goal>
                                <goal>testGenerateStubs</goal>
                                <goal>testCompile</goal>
                                <goal>removeStubs</goal>
                                <goal>removeTestStubs</goal>
                            </goals>
                        </execution>
                    </executions>
<dependencies>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-all</artifactId>
                        <!-- any version of Groovy \>= 1.5.0 should work here -->
                        <version>2.4.4</version>
                        <scope>runtime</scope>
                    </dependency>
                </dependencies>
                </plugin>

并获得编译异常:

EmailSender 不是抽象的,不会覆盖 AbstractSender 中的抽象方法 sentTrigger

方法 sentTrigger 已实现。生成的 java 代码是这样的:

@groovy.transform.Trait() public interface AbstractSender
 {
;
 SentTrigger sendMail(Mail mail);
 sentTrigger sentTrigger(Mail mail);
}

这确实解释了编译错误。我用 gmavenplus 插件做错了什么?

【问题讨论】:

    标签: groovy traits


    【解决方案1】:

    这不是 GMavenPlus 配置问题。你可以把它放在你的 GroovyConsole 中来演示这个

    trait AbstractSender {
      abstract SentTrigger send(Mail main)
    
      SentTrigger sentTrigger(Mail mail) { }
    }
    
    class EmailSender implements AbstractSender{
      @Override
      SentTrigger sendMail(Mail mail) { }
    }
    
    class Mail { }
    class SentTrigger { }
    
    new  EmailSender()
    

    您看到的错误消息是正确的。 EmailSender 仅实现 sendMail(),而不是 send()。如果将sendMail() 重命名为send(),它应该可以正确编译。

    【讨论】:

    • 对不起,我的错! sendMail 是抽象的,在具体类中被覆盖/实现。
    • 只是为了澄清:即使重命名方法后你仍然遇到这个问题?或者那是你的问题?
    • 是的,我还是明白了。我在准备问题描述时打印错了。这个想法是 trait 有抽象和具体的方法。类实现特征并覆盖/实现抽象方法。编译器抱怨特征中的第二个具体方法没有实现。
    • Okie dokie。我们错过了一些东西,因为它对我有用。看看github.com/keeganwitt/SO32224258,一旦你看到评论或公关有什么不同,我可以复制,然后我们会更新问题。
    • 我刚刚注意到的一件事是,您在插件中而不是在您的项目中(或者可能在您的项目中)有 Groovy 依赖项,这可能不是您在编译中想要的用例。
    猜你喜欢
    • 2016-02-07
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    相关资源
    最近更新 更多