Skip to content

Commit 0db57f2

Browse files
committed
Validate characters in potential enum values
Added regex check to ensure the value only contains valid Java identifier characters (letters, digits, underscores, dollar signs, and dots). Values containing spaces, quotes, or other special characters are not valid enum references.
1 parent 82bf9f2 commit 0db57f2

1 file changed

Lines changed: 3 additions & 10 deletions

File tree

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -292,19 +292,12 @@ private static String getFullyQualifiedClass(String fqn) {
292292
}
293293

294294
private boolean isFullyQualifiedEnumValue(J.Annotation annotation) {
295-
// A fully qualified enum value has at least 2 dots in a single value: package.EnumClass.CONSTANT
296-
// e.g., org.example.Values.ONE
297-
// We check that the enum class portion (everything before the last dot) itself contains a dot
298-
// Also verify the attribute type is not a String to avoid treating "some.dotted.string" as an enum
299295
if (attributeValue == null || attributeValue.endsWith(".class") || attributeValue.contains(",")) {
300296
return false;
301297
}
302-
// Require type information to safely determine if this is an enum value
303-
// Without type info, we can't distinguish between a dotted string and an enum reference
304-
if (!findMethod(annotation, attributeName()).isPresent()) {
305-
return false;
306-
}
307-
if (attributeIsString(annotation)) {
298+
if (!attributeValue.matches("[\\w.$]+") ||
299+
!findMethod(annotation, attributeName()).isPresent() ||
300+
attributeIsString(annotation)) {
308301
return false;
309302
}
310303
int lastDot = attributeValue.lastIndexOf('.');

0 commit comments

Comments
 (0)