spring-projects / spring-projects/spring-data-commons

Add support for Immutables

Open
#3,157 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type: enhancement
Dominant language
Java
Stars
838
Forks
730
PR merge metrics
No merged PRs in 30d

Description

Example:

// User.java
import org.immutables.value.Value;

@Value.Immutable
public interface User {
    @Value.Default
    default String getName() {
        return NameGenerator.random();
    }
}
// ImmutableUser.java
import org.immutables.value.Generated;

@Generated(from = "User", generator = "Immutables")
@SuppressWarnings({"all"})
@javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor")
public final class ImmutableUser implements User {
  private final String name;

  private ImmutableUser(ImmutableUser.Builder builder) { // What if we ignore this?
    this.name = builder.name != null
        ? builder.name
        : Objects.requireNonNull(User.super.getName(), "name");
  }

  private ImmutableUser(String name) { // Should use this
    this.name = name;
  }

  // ...

  @Generated(from = "User", generator = "Immutables")
  public static final class Builder {
    // ...
  }
}

In this code, it can't find constructor because there is a constructor that uses Builder as a parameter.

I don't want something big like creating constructor using Builder. If the class is auto-generated and the constructor parameter is builder, I want it to ignore it.

I could add @Value.Style(privateNoargConstructor = true) annotation to interface, so there would be noArg constructor but that's just workaround. It's not working with transiant types, etc. I prefer first solution if it's okay.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java around line 112, where constructor discovery fails for the generated Immutables class. Compare the builder-parameter constructor with the direct String constructor and define completion as ignoring the generated builder constructor without creating a constructor from Builder.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.