Skip to content

Remove reflective validation from ReplaceStringLiteralWithConstant#6825

Closed
timtebeek wants to merge 1 commit intomainfrom
remove-reflective-validation-from-replace-string-literal
Closed

Remove reflective validation from ReplaceStringLiteralWithConstant#6825
timtebeek wants to merge 1 commit intomainfrom
remove-reflective-validation-from-replace-string-literal

Conversation

@timtebeek
Copy link
Member

@timtebeek timtebeek commented Feb 26, 2026

What's Changed

ReplaceStringLiteralWithConstant.validate() used Class.forName() to reflectively verify the target constant when literalValue was not provided. This fails when the target class isn't on the recipe classpath, producing validation errors like:

fullyQualifiedConstantName was 'org.springframework.http.MediaType.TEXT_HTML_VALUE'
but it No class for specified name was found.

This is the common case — rewrite-spring doesn't depend on spring-web, so org.springframework.http.MediaType and org.springframework.http.HttpHeaders are never on the recipe classpath.

The runtime already handles this gracefully: getLiteralValue() catches ClassNotFoundException and returns null, and getVisitor() returns a noop. Validation should not reject the recipe for a condition the runtime handles.

Fix

Remove the reflective validation block from validate(). Keep only the fullyQualifiedConstantName blank check. The reflective fallback in getLiteralValue() is retained as a best-effort mechanism for when the class happens to be on the classpath.

Test plan

  • All ReplaceStringLiteralWithConstantTest tests pass (11 tests)
  • Compilation succeeds

The validate() method used Class.forName() to reflectively verify that
the target constant exists when literalValue was not provided. This
fails when the target class is not on the recipe classpath, which is
the common case (e.g. org.springframework.http.MediaType is not a
dependency of rewrite-spring), producing errors like:

  fullyQualifiedConstantName was
  'org.springframework.http.MediaType.TEXT_HTML_VALUE' but it
  No class for specified name was found.

The reflective fallback in getLiteralValue() already handles this
gracefully by returning null, causing getVisitor() to return a noop.
Validation should not reject the recipe for a condition that the
runtime already handles.
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Feb 26, 2026
@timtebeek timtebeek closed this Feb 26, 2026
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Feb 26, 2026
@timtebeek timtebeek deleted the remove-reflective-validation-from-replace-string-literal branch February 26, 2026 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

1 participant