Skip to content

Conversation

@BoykoAlex
Copy link
Contributor

Support instanceof with types where type parameters shouldn't be present in the instanceof.

For example:

if (o instanceof List) {
  map.put("key", String.join(",", (List) o));
}

currently produces a compiler error when refactored in:

if (o instanceof List<?> list) {
  map.put("key", String.join(",", list));
}

The compiler error:

The method join(CharSequence, CharSequence...) in the type String is not applicable for the arguments (String, List<capture#1-of ?>)

Instead it should become:

if (o instanceof List list) {
  map.put("key", String.join(",", list));
}

(i.e. without the wildcard)

The fix is if the type has type parameters and the following type casts statements cast to type with type parameters then wildcard type parameter(s) will be added. Otherwise no type parameters will be added to the type in the instanceof

@BoykoAlex BoykoAlex marked this pull request as draft September 13, 2024 22:17
@timtebeek timtebeek self-requested a review September 13, 2024 22:24
@BoykoAlex BoykoAlex changed the title Instanceof pattern: add type parameters if type casts use type parame… Instanceof pattern: exclusion for Stream.collect(..) Sep 17, 2024
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@timtebeek
Copy link
Member

Thanks for the work done here @BoykoAlex ! Let me know when you're done by marking the PR as ready for review. :)

@BoykoAlex
Copy link
Contributor Author

Abandoned in favour of #338

@BoykoAlex BoykoAlex closed this Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants