openrewrite / openrewrite/rewrite

Marker::withId type param should probably be defined at the interface level

Open
#2,748 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Java
Stars
3.7k
Forks
570
Avg merge
13h 12m
Merged PRs (30d)
261

Description

See:
https://github.com/openrewrite/rewrite/blob/09ba3895ee40d1f25c16b57f0cebfcf797bfb441/rewrite-core/src/main/java/org/openrewrite/marker/Marker.java#L28-L36

And, try to write a class implementing this interface without using Lombok for the withId implementation.
It might look like this:

    class MyMarker implements Marker {

        @Override
        public UUID getId() {
            return UUID.randomUUID();
        }

        @Override
        public MyMarker withId(final UUID id) {
            return new MyMarker();
        }
    }

...which has an unchecked conversion warning on the withId return type, because it's not M. Using Lombok to generate the method covers that up, but if you're writing a custom Marker without Lombok, you're stuck with warnings.

It looks like when you define an interface method which declares a type param, then the use of generics becomes part of the method signature which subclasses must fulfill, and a class implementing the interface cannot specify the type param.

Instead, if we do public interface Marker<M extends Marker<M>>, then the signature of withId works as expected. But, this change would require a touch to ~every Marker implementation (to specify that type param at the class declaration), which might be disruptive.

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 rewrite-core/src/main/java/org/openrewrite/marker/Marker.java at the referenced interface definition, then inspect the Marker implementations that would need a type parameter. Reproduce the unchecked conversion with the custom implementation shown in the issue, and assess the interface change's impact before deciding whether it is viable. Done means the chosen API has no warning for custom implementations and affected code still compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend-api-design
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.