Can I use an Immutables class as a gradle plugin extension

249 Views Asked by At

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?

0

There are 0 best solutions below