Skip to content

Commit 3c4c715

Browse files
committed
Remove reflective validation from ReplaceStringLiteralWithConstant
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.
1 parent a72ba76 commit 3c4c715

1 file changed

Lines changed: 0 additions & 19 deletions

File tree

rewrite-java/src/main/java/org/openrewrite/java/ReplaceStringLiteralWithConstant.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,6 @@ public Validated<Object> validate() {
6969
if (StringUtils.isBlank(fullyQualifiedConstantName)) {
7070
return result.and(invalid(CONSTANT_FQN_PARAM, fullyQualifiedConstantName, "The constant's fully qualified name may not be empty or blank."));
7171
}
72-
if (StringUtils.isBlank(literalValue)) {
73-
try {
74-
Object constantValue = getConstantValueByFullyQualifiedName(fullyQualifiedConstantName);
75-
if (constantValue == null) {
76-
return result.and(invalid(CONSTANT_FQN_PARAM, fullyQualifiedConstantName, "Provided constant should not be null."));
77-
}
78-
if (!(constantValue instanceof String)) {
79-
// currently, we only support string literals, also see visitor implementation
80-
return result.and(invalid(CONSTANT_FQN_PARAM, fullyQualifiedConstantName, "Unsupported type of constant provided. Only literals can be replaced."));
81-
}
82-
return result;
83-
} catch (ClassNotFoundException e) {
84-
return result.and(invalid(CONSTANT_FQN_PARAM, fullyQualifiedConstantName, "No class for specified name was found."));
85-
} catch (NoSuchFieldException e) {
86-
return result.and(invalid(CONSTANT_FQN_PARAM, fullyQualifiedConstantName, "No field with specified name was found."));
87-
} catch (IllegalAccessException e) {
88-
return result.and(invalid(CONSTANT_FQN_PARAM, fullyQualifiedConstantName, "Unable to access specified field."));
89-
}
90-
}
9172
return result;
9273
}
9374

0 commit comments

Comments
 (0)