1

I'm writing a gradle plugin in Java. To make it configurable, I want to define an extension object. Since this is essentially a value object, I figured I'd use Immutables to define the class, to reduce the amount of code. However, this is turning out to be difficult, and I'm wondering if it's possible?

The first thing I tried was define a straightforward immutable class:

@Value.Immutable
public abstract class MyExtension {
  public abstract String someField();
}

And then add it as an extension with:

project.getExtensions()
       .create("extensionName", MyExtension.class);

This fails with:

> Failed to apply plugin 'my.plugin.id'.
   > Could not create an instance of type com.palantir.gotham.ontology.GothamOntologyFragmentsPluginExtension.
      > Could not generate a decorated class for type MyExtension.
         > Cannot have abstract method MyExtension.someField().

Defining the immutable with an interface instead of an abstract class gives the same error. I then tried passing the immutable class instead, using:

project.getExtensions()
       .create(TypeOf.typeOf(MyExtension.class), "extensionName", ImmutableMyExtension.class);

However, this fails with a different error:

> Failed to apply plugin 'my.plugin.id'.
   > Could not create an instance of type ImmutableMyExtension.
      > Class ImmutableMyExtension is final.

It seems gradle requires the extension class to be extensible, and immutables always generates final classes. Is there any way to get this to work?

Erik Hesselink
  • 2,420
  • 21
  • 25
  • 1
    *Guessing*: the whole approach of gradle is that "everything" can be extended. I am wondering if you are over engineering here. And note that "fighting" the tool is most often the wrong strategy. Follow the rules that the tool comes with, instead of bending the tool. – GhostCat Apr 12 '21 at 09:14
  • Yeah you might be right. For now I'm manually defining the class with getters and setters, but I'd be happy to see if there's something more concise. Using Kotlin might help here but would be painful in our organisation due to existing tooling etc. Note that it seems the above issue would also exist with the upcoming java records, so perhaps I should file a gradle FR. – Erik Hesselink Apr 12 '21 at 09:33

0 Answers0