Skip to content

Immutable without fields does not generate a constructor/factory method despite allParameters = true #1604

@Adrodoc55

Description

@Adrodoc55

This is a weird edge case and not that important, but since I actually encountered it in a project of mine I thought it would be worth reporting on.

If you generate an Immutable with no fields there is no factory method (or in case of of = "new" no public constructor) to instantiate the type even if you set allParameters = true. I would have expected a factory method (or constructor) without parameters.

Example

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.SOURCE;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import org.immutables.value.Value;

@Retention(SOURCE)
@Target(TYPE)
@Value.Style(allParameters = true)
public @interface MyStyle {}
import org.immutables.value.Value;

@MyStyle
@Value.Immutable
public interface MyObject {}

Because of this I can use the factory method for all immutable objects in my project, except for this one weird object, that actually has no parameters. This one I have to instantiate using the builder: ImmutableMyObject.builder().build() instead of the typical ImmutableMyObject.of().
The Object has no parameters, because it's only relevant whether the instance exists or is null. Yes is most cases this would be better modeled with a boolean, but this is actually a JSON API where I may want to add fields in the future. It's a weird case I know.

My hope would be that allParameters = true and allMandatoryParameters = true would force generation of a constructor/factory even if there are no parameters, but if there are some issues with that approach I am fine with using the builder for this one weird case.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions