question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

java 15 record as generated immutable implementation

See original GitHub issue

How about generating the immutable implementation of a given interface as a java 15 record (JEP 359)?

This would allow all the benefits of records to be combined with the greatness and ecosystem integration of immutables (in my case: mainly compatibility with mapstruct and openapi).

WDYT?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mickrollcommented, Jan 11, 2021

Well, maybe what I really need is a builder for records. And thats already possible using @Builder.Constructor (https://immutables.github.io/factory.html)

Thanks for the discussion!

1reaction
mickrollcommented, Jan 6, 2021

I thought of something along the lines of:

Data object definition:

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import javax.validation.constraints.NotEmpty;
import org.immutables.value.Value;

@Value.Immutable(record = true)
@Value.Style(get = { "get*", "is*" }, init = "set*", allParameters = true)
@JsonDeserialize(builder = ImmutableLoginAttemptUser.Builder.class)
public interface LoginAttemptUser {

  @NotEmpty
  String getUsername();

  @NotEmpty
  String getPassword();
}

Record-style implementation, generated by immutables:

@javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor")
public record ImmutableLoginAttemptUser(String username, String password)
    implements LoginAttemptUser {

  @JsonProperty("username")
  @Override
  public String getUsername() {
    return username;
  }

  @JsonProperty("password")
  @Override
  public String getPassword() {
    return password;
  }
  
  public static ImmutableLoginAttemptUser.Builder builder() {
    return new ImmutableLoginAttemptUser.Builder();
  }

  @Generated(from = "LoginAttemptUser", generator = "Immutables")
  public static final class Builder {
    [... same as before]
  }
}

Usage, bean-style (these getters need to be generated by immutables to fulfill interface declarations):

  var user = ImmutableLoginAttemptUser.builder().setUsername("foo").setPassword("bar").build();
  String x = user.getUsername() + ":" + user.getPassword();

The jackson json deserializer would use the builder in a similar way. The jackson json serializer would use the bean-style getters.

Usage, record-style (these ‘getters’ are generated by compiler):

  var user = ImmutableLoginAttemptUser.builder().setUsername("foo").setPassword("bar").build();
  String x = user.username() + ":" + user.password();

I think the overall behaviour of the generated records are the same in comparison to created immutable classes.

Note the restrictions of records (https://openjdk.java.net/jeps/395#Rules-for-record-classes), that is why i would recommend to enable usage of records on an opt-in basis via @Value.Immutable(record = true).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create Immutable Value Objects using Java Record Classes
This is an Introduction to Java Record Classes. We will learn to use Java records to create Immutable data objects, or Value Objects....
Read more >
Bruce Eckel on Java records - Oracle Blogs
Records automatically generate. Immutable fields; A canonical constructor; An accessor method for each element; The equals() method ...
Read more >
Java Records - Evren Tan
Java Record is a new way to declare a class to pass immutable data. Records were first introduced in Java 14 but they...
Read more >
Java 14 Record Keyword - Baeldung
In this tutorial, we'll look at the fundamentals of records, including their purpose, generated methods, and customization techniques.
Read more >
Avoid Multithreading Bugs Using Immutable Java Records
In this example, we created a list of integers(integerList), added one element into it, and initialized the record class with this. Calling method...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found