Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy generated constructor missing during compile

Tags:

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));
...
like image 582
PhilBa Avatar asked Jul 12 '26 19:07

PhilBa


1 Answers

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
like image 150
blackdrag Avatar answered Jul 18 '26 06:07

blackdrag



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!