怎么编译grovy_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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值