Skip to content

Fix RemoveUnusedPrivateMethods deleting methods called with raw collection arguments#898

Open
habiibullahm wants to merge 1 commit into
openrewrite:mainfrom
habiibullahm:fix/remove-unused-private-methods-raw-collections-877
Open

Fix RemoveUnusedPrivateMethods deleting methods called with raw collection arguments#898
habiibullahm wants to merge 1 commit into
openrewrite:mainfrom
habiibullahm:fix/remove-unused-private-methods-raw-collections-877

Conversation

@habiibullahm
Copy link
Copy Markdown

@habiibullahm habiibullahm commented May 20, 2026

…ctions

Summary

• Fixes a false positive in RemoveUnusedPrivateMethods where a private method was removed even though it was still invoked.
• Adds relaxed method matching via referencesMethod() so call sites using raw types (e.g. raw Map → Set / Collection) still match
parameterized private method signatures (Set, Collection).
• Removes the fragile Generic{ string-based workaround in favor of consistent type-based matching.

Problem

When a private method is called with values from a raw Map (templates.keySet(), templates.values()), strict JavaType.Method#equals between
the declaration and TypesInUse.getUsedMethods() fails. The recipe treated the method as unused, deleted it, and left a non-compiling call
site.

Solution

Introduce referencesMethod(declaration, used) that considers a method used when:

• Names match
• Declaring class matches
• Parameter arity matches
• Each parameter is compatible via bidirectional TypeUtils.isOfType (handles raw vs parameterized mismatches)

Test plan

◼ RemoveUnusedPrivateMethodsTest.privateMethodCalledWithRawCollectionArguments (issue reproducer)
◼ RemoveUnusedPrivateMethodsTest.privateMethodCalledWithParameterizedMapArguments

  • ◼ Existing RemoveUnusedPrivateMethodsTest cases (unused removal, serialization, @MethodSource, @SuppressWarnings, bounded generics #1536)

What's changed?

What's your motivation?

Anything in particular you'd like reviewers to focus on?

Anyone you would like to review specifically?

Have you considered any alternatives or workarounds?

Any additional context

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

…ctions

Fix RemoveUnusedPrivateMethods removing methods called with raw collections
  Match used methods with relaxed parameter types so raw call sites
  (e.g. Map.keySet()/values()) are not treated as unused when the
  declaration uses parameterized types.
  Fixes openrewrite#877
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

RemoveUnusedPrivateMethods removes private method still called with raw collection arguments

1 participant