Groovy在编译过程中生成构造函数 [英] Groovy generated constructor missing during compile

查看:254
本文介绍了Groovy在编译过程中生成构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个设置,我从java类(ProductService)中调用Groovy类(Product)的生成构造函数(@TupleConstructor)。 IDE显示生成的构造函数和用于编译的编译。
但是现在,由于未知的原因,编译失败了,因为编译器不再查找参数化的构造函数了:

  ProductService。 java:31:error:constructor类Product中的Product不能将
应用于给定的类型;
required:没有参数
found:String,boolean,boolean,float
reason:实际和形式参数列表长度不同

这是我当前的gradle(2.4)设置:

  apply插件:'groovy'
应用插件:'java'

project.sourceCompatibility = 1.8
project.targetCompatibility = 1.8

sourceSets.main.java .srcDirs = []
sourceSets.main.groovy.srcDir'src / main / java'
...
dependencies {
compile'org.codehaus.groovy:groovy- all:2.4。+'
testCompile'org.spockframework:spock-core:1.0-groovy-2.4'
}

Groovy类:

  @TupleConstructor 
类产品{
字符串名称
布尔bool1
布尔bool2
浮动价格
}


$ b $

  ... 
products.add(新产品(议会,真,假,10.50F));
...


解决方案

分析:



这看起来像是一个联合编译问题。很可能在Groovy创建.java存根文件后运行transform @TupleConstructor,导致java编译部分失败。它可能以前工作过,因为你编译独立的groovy部分,然后重用现有的类文件(不干净)。可悲的是,这是存根生成器的限制,如果转换应该保持在同一阶段,那么在Groovy中没有真正解决问题的方法。

解决方案: 使用groovy- eclipse批处理编译器

  • 不使用在存根生成器之后运行的转换
  • 在gradle中创建一个多模块版本,它将编译groovy部分独立


  • I have a setup in which I call the generated constructor(@TupleConstructor) of a Groovy class(Product) from a java class(ProductService). The IDE shows the generated constructors and compilation used to work. But now, for unknown reasons, the compilation fails because the compiler doesnt find the parameterized constructors anymore:

    ProductService.java:31: error: constructor Product in class Product cannot 
    be applied to given types; 
    required: no arguments  
    found: String,boolean,boolean,float 
    reason: actual and formal argument lists differ in length
    

    And this is my current gradle(2.4) setup:

    apply plugin: 'groovy'
    apply plugin: 'java'
    
    project.sourceCompatibility = 1.8
    project.targetCompatibility = 1.8
    
    sourceSets.main.java.srcDirs = []
    sourceSets.main.groovy.srcDir 'src/main/java'
    ...
    dependencies {
        compile 'org.codehaus.groovy:groovy-all:2.4.+'
        testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
    }
    

    Groovy class:

    @TupleConstructor
    class Product {
        String name
        boolean bool1
        boolean bool2
        float price
    }
    

    Constructor call in Java class(fails to compile):

    ...
    products.add(new Product("Parliament", true, false, 10.50F));
    ...
    

    解决方案

    Analysis:

    This looks to me like a joint compilation issue. Most likely the transform @TupleConstructor runs after Groovy did create the .java stub files, causing the java compilation part to fail. It could have worked before, because you compiled the groovy part independent and then reused existing class files (no clean). Sadly this is a limitation of the stub generator and there is not really a way to fix the issue in Groovy, if the transform is supposed to stay in the same phase.

    Solutions:

    • use the groovy-eclipse batch compiler
    • don't use transforms that run after the stub generator
    • create a multi module build in gradle, that will compile the groovy part independent

    这篇关于Groovy在编译过程中生成构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆