Constructor of @Immutable class with parameters is not accessible from .java classes
Asked Answered
A

1

6

I clean generated .class files, and re-build my app. If the first call to generated AST constructor happens from a .java class, I get an error. But if I call a constructor generated by an @Immutable annotation in a .groovy class first, it becomes visible to .java classes since bytecode is already generated and no errors occur.

So I am wondering how do I set up gradle so it compiles all groovy classes with needed AST transformations first, so i can use it from .java classes properly? Also, how do I use @Builder-generated code from .java classes, since javac doesn't see any ClassName.bulder() method?

  1. Here is my .groovy class with ASTT

    @Immutable class A {
        String a;
    }
    
  2. Here is how I call its constructor from .java class

    public class Test {
         A b = new A("321");
    }
    
  3. And the error I get:

    Error: "constructor in class [skipped] cannot be applied to given types; required: no arguments found: [skipped] reason: actual and formal argument lists differ in length".

I am using Intellij IDEA 14.4 and Gradle to build.

Antigen answered 19/9, 2015 at 16:52 Comment(4)
Do you have a brief example?Woodrum
Create an instance of the class (object) which the constructor has. This will automatically call the constructor.Deductive
What i found is this question #30642290 which seems to be exactly the sameAntigen
It seems it is not possible to do this yet with Groovy. See this issue jira.apache.org/jira/browse/GROOVY-7764?attachmentOrder=descOssetia
T
0

I had a similar problem once. What worked for me was preventing Java to run before Groovy by setting its source sets in build.gradle to an empty list (see code below).

As far as I understood, this way the Groovy compiler will delegate the Java files to Java, but will do that in the correct order. The Java compile on the other hand will ignore Groovy files, which leads to the unfulfilled dependency you are seeing.

plugins {
    id 'groovy'
    id 'java'
}

sourceCompatibility = 11

sourceSets {
    main {
        java {
            srcDirs = [] // don't compile Java code twice
        }
        groovy {
            srcDirs += 'src/main/java'
        }
    }
}

// …
Taynatayra answered 17/12, 2019 at 9:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.