rewrite-static-analysis icon indicating copy to clipboard operation
rewrite-static-analysis copied to clipboard

Fix RemoveUnusedPrivateMethods deleting methods called with raw collection arguments

Open habiibullahm opened this issue 6 months ago • 0 comments

…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.

  • Fixes openrewrite/rewrite-static-analysis#877 (https://github.com/openrewrite/rewrite-static-analysis/issues/877).

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

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

habiibullahm avatar May 20 '26 13:05 habiibullahm