Lombok: [FEATURE] Value annotation should use already defined private constructor

Created on 18 Apr 2019  路  4Comments  路  Source: projectlombok/lombok

Describe the feature
When I use the @Value annotation with staticConstructor = "of" and a private Constructor is already defined, Lombok should use the private Constructor to generate a static of Constructor.

Lomboked version

@Value(staticConstructor = "of")
public final class SampleDocument {

    private final String value;

    private SampleDocument(final String value) {
        assertThat(value).isNotEmpty();
        this.value = value;
    }

}

Java version

public final class SampleDocument {

    private final String value;

    private SampleDocument(final String value) {
        assertThat(value).isNotEmpty();
        this.value = value;
    }

    public static SampleDocument of(final String value) {
        return new SampleDocument(value);
    }

[...]
}

Describe the target audience
Every programer who want's to do DDD and validate its value objects in the constructor to have only valid domain objects.

Additional context
Currently when a private constructor is already defined no static of constructor will be generated at all.

Most helpful comment

Liked the idea and have have a pull request up for this here https://github.com/rzwitserloot/lombok/pull/2119

All 4 comments

I understand the request, and think it is a good idea to generate the static method if staticConstructor is provided, even if we don't generate the (private) all-args constructor.

If someone would create a pull request with implementations for ecj and javac and create test files in our test infrastructure, this gets higher priority.

Liked the idea and have have a pull request up for this here https://github.com/rzwitserloot/lombok/pull/2119

It's fixed. We just need to add it to the changelog.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

x9nico picture x9nico  路  3Comments

merric picture merric  路  4Comments

mmoayyed picture mmoayyed  路  3Comments

michaelkuechler picture michaelkuechler  路  3Comments

lludwa picture lludwa  路  3Comments