Skip to content

Add null guard for AddCommentToMethod and NumberToDuration#6834

Merged
davidh44 merged 1 commit intomasterfrom
hdavidh/migration-tool-null-guard
Mar 31, 2026
Merged

Add null guard for AddCommentToMethod and NumberToDuration#6834
davidh44 merged 1 commit intomasterfrom
hdavidh/migration-tool-null-guard

Conversation

@davidh44
Copy link
Copy Markdown
Contributor

@davidh44 davidh44 commented Mar 31, 2026

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 MethodMatcher parser no longer tolerates null patterns, causing NullPointerException when these bare instances are invoked during recipe execution.

This affects recipes that use AddCommentToMethod and NumberToDuration, which are configured via YAML in change-config-types.yml and change-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() for AddCommentToMethod and NumberToDuration - returns TreeVisitor.noop() when methodPattern is null. YAML-configured instances with actual method patterns are unaffected.

Testing

Tested locally, NPE disappears

License

  • I confirm that this pull request can be released under the Apache 2 license

@davidh44 davidh44 requested a review from a team as a code owner March 31, 2026 21:27
@davidh44 davidh44 added the changelog-not-required Indicate changelog entry is not required for a specific PR label Mar 31, 2026
@davidh44 davidh44 enabled auto-merge March 31, 2026 21:35
@sonarqubecloud
Copy link
Copy Markdown

@davidh44 davidh44 added this pull request to the merge queue Mar 31, 2026
Merged via the queue into master with commit ba64ad5 Mar 31, 2026
49 of 52 checks passed
@github-actions
Copy link
Copy Markdown

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 31, 2026
@davidh44 davidh44 deleted the hdavidh/migration-tool-null-guard branch April 1, 2026 04:01
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

changelog-not-required Indicate changelog entry is not required for a specific PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants