gradle / gradle

Adaptable, fast automation for all
https://gradle.org
Apache License 2.0
16.95k stars 4.75k forks source link

Overloading setter for abstract RegularFileProperty results in Cannot have abstract method MyClass.getXYZ(). #26691

Open MarkRx opened 1 year ago

MarkRx commented 1 year ago

Current Behavior

If a setter is defined for a property defined using decoration on an abstract getter Gradle will fail with the following error:

Cannot have abstract method MyClass.getXYZ()

 Caused by: java.lang.IllegalArgumentException: Cannot have abstract method MyClass.getMyFile().
    at org.gradle.internal.instantiation.generator.AbstractClassGenerator.assertNotAbstract(AbstractClassGenerator.java:396)
    at org.gradle.internal.instantiation.generator.AbstractClassGenerator.inspectType(AbstractClassGenerator.java:322)
    at org.gradle.internal.instantiation.generator.AbstractClassGenerator.generateUnderLock(AbstractClassGenerator.java:224)
    ... 224 more

Expected Behavior

If this pattern is valid then having both the auto generated decorated setter overloaded by a custom setter should work.

If this pattern is not valid then a better error message should be thrown indicating that a setter of an auto generated property cannot be overloaded.

Context (optional)

No response

Steps to Reproduce

  1. Create a decorated object:
public abstract class MyClass {
    private transient ObjectFactory objectFactory;

    @Inject
    public MyClass(ObjectFactory objectFactory) {
        this.objectFactory = objectFactory;
    }

    @InputFile
    public abstract RegularFileProperty getMyFile();

    // Overloaded to allow passing in String objects
    public void setMyFile(Object obj) {
        getMyFile().set(objectFactory.fileCollection().from(obj).getSingleFile());
    }
}
  1. Instantiate it using objectFactory

    objectFactory.newInstance(MyClass.class, objectFactory);
  2. Use the plugin and object and attempt to set myFile

    myFile = "file1.txt"

Gradle version

8.3

Build scan URL (optional)

No response

Your Environment (optional)

No response

ov7a commented 1 year ago

Thank you for providing a valid report.

The issue is in the backlog of the relevant team, but this area of Gradle is currently not a focus one, so it might take a while before a fix is made.

CRogers commented 3 months ago

I've also encountered this here, and it is very very frustrating. Overloading setters should be possible like this.