Skip to content

[UnusedMethod] Handle reflective method references#5525

Open
electrum wants to merge 1 commit intogoogle:masterfrom
electrum:unusedmethod
Open

[UnusedMethod] Handle reflective method references#5525
electrum wants to merge 1 commit intogoogle:masterfrom
electrum:unusedmethod

Conversation

@electrum
Copy link

@electrum electrum commented Feb 22, 2026

Handles Class.getMethod, getDeclaredMethod, getConstructor, getDeclaredConstructor, and MethodHandles.Lookup.findStatic, findVirtual, findSpecial, findConstructor. Only suppresses findings when the method name, owner class, and parameter types can all be statically resolved.

This is cleaner than adding @Keep everywhere, and avoids the problem that methods with @Keep can later become unused. IntelliJ IDEA understands static reflection as method usage, and this gives Error Prone the same ability.

@electrum electrum force-pushed the unusedmethod branch 2 times, most recently from 1a422d3 to 1371a8b Compare February 22, 2026 19:08
Handles Class.getMethod, getDeclaredMethod, getConstructor,
getDeclaredConstructor, and MethodHandles.Lookup.findStatic,
findVirtual, findSpecial, findConstructor. Only suppresses findings
when the method name, owner class, and parameter types can all be
statically resolved.
@cushon
Copy link
Collaborator

cushon commented Mar 10, 2026

This approach only handles reflective references from the same file as the method declaration. Error Prone only analyzes one file at a time, it won't be able to detect reflective references from other files. So this isn't a replacement for @Keep, @Keep is still necessary for references from other files, and reflective references to methods declared in the same file are usually rarer.

@electrum
Copy link
Author

@cushon For Trino, using references in the same file to lookup MethodHandle is very common. We have over 100 such usages, and probably zero usages of reflection across files. Here's an example file. This feature would allow us to remove ~173 usages of our @Keep-extending @UsedByGeneratedCode annotation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants