Randgalt / record-builder

Record builder generator for Java records
Apache License 2.0
756 stars 55 forks source link

Generic records produce code with `rawtypes` and `unchecked` warnings #101

Closed daveschoutens closed 2 years ago

daveschoutens commented 2 years ago

At work, we are making great use of your library. Thanks!

That said, we introduced our first record using generics the other day, and found the generated Builder class to suffer from a few javac warnings, namely unchecked and rawtypes. We have a very low tolerance for compiler warnings, so this blocks us from using @RecordBuilder for these types.

We use JDK 17

Minimal example:

@RecordBuilder
public record Foo<T>(List<T> items) {}

Problematic generated code snippets:

// FooBuilder

@Generated("io.soabase.recordbuilder.core.RecordBuilder")
@Override
public boolean equals(Object o) {
    return (this == o) || ((o instanceof FooBuilder r)    // <- javac flags [rawtypes] warning here
            && Objects.equals(items, r.items));
}

// ...

@Generated("io.soabase.recordbuilder.core.RecordBuilder")
default FooBuilder<T> with() {
    return new FooBuilder(items());  // <- javac flags [rawtypes] and [unchecked] warnings here
}

It would be ideal if equals() could appease the compiler with (o instanceof FooBuilder<?> r) and with() could similarly with new FooBuilder<>(items()).

Barring that, perhaps a @SuppressWarnings({"unchecked","rawtypes"}) on these methods or on the class.