Add null guard for AddCommentToMethod and NumberToDuration#6834
Merged
Add null guard for AddCommentToMethod and NumberToDuration#6834
Conversation
zoewangg
approved these changes
Mar 31, 2026
|
|
This pull request has been closed and the conversation has been locked. Comments on closed PRs are hard for our team to see. If you need more assistance, please open a new issue that references this one. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Motivation and Context
When OpenRewrite scans the classpath for recipes, it discovers and instantiates all recipe classes, including ones that are only meant to be used as sub-recipes with YAML-provided options (e.g.,
methodPattern). These bare instances are created with null fields since no options are provided during classpath scanning.In newer OpenRewrite versions, the rewritten
MethodMatcherparser no longer tolerates null patterns, causingNullPointerExceptionwhen these bare instances are invoked during recipe execution.This affects recipes that use
AddCommentToMethodandNumberToDuration, which are configured via YAML inchange-config-types.ymlandchange-exception-types.yml.This issue occurs when recipes are loaded via OpenRewrite's
ClasspathScanningLoader(e.g., when running recipes from a standalone jar). It does not affect the Maven or Gradle plugin paths, which handle recipe instantiation differently.Modifications
Added null check in
getVisitor()forAddCommentToMethodandNumberToDuration- returnsTreeVisitor.noop()whenmethodPatternisnull. YAML-configured instances with actual method patterns are unaffected.Testing
Tested locally, NPE disappears
License