AddOrUpdateAnnotationAttribute: Handle fully qualified enum values#6719
Merged
Jenson3210 merged 18 commits intomainfrom Feb 12, 2026
Merged
AddOrUpdateAnnotationAttribute: Handle fully qualified enum values#6719Jenson3210 merged 18 commits intomainfrom
Jenson3210 merged 18 commits intomainfrom
Conversation
When adding an enum value to an annotation attribute using a fully qualified name like org.example.Values.ONE, the recipe now: - Adds an import for the enum class (org.example.Values) - Uses the shortened form in the annotation (Values.ONE) This matches the existing behavior for fully qualified class references ending in .class.
The isFullyQualifiedEnumValue check now also verifies that the attribute type is not a String. This prevents dotted string values like "some.dotted.value" from being incorrectly treated as fully qualified enum references.
Without type information, we cannot reliably distinguish between a dotted string value and a fully qualified enum reference. This change ensures we only treat values as enums when we have the annotation method's return type available.
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.
c318962 to
0db57f2
Compare
When appendArray=true and the annotation attribute is a Class<?>[] array type with an existing single class value (e.g. @foo(Integer.class)), the recipe now properly converts it to an array containing both the existing and new values (e.g. @foo({Integer.class, Long.class})). This works for both implicit value attributes (@foo(Integer.class)) and named attributes (@foo(classes = Integer.class)).
- Add test for appending string to existing value array - Add test for replacing string in existing value array without append - Fix ImportResource mock to include all attributes (value, locations, reader)
The condition was incorrectly checking attributeValue against "value" when it should check attributeName. This bug could cause incorrect behavior when updating array values for non-value attributes.
- isFullyQualifiedClass() now excludes comma-separated values to prevent incorrectly treating "Integer.class,Long.class" as a single FQ class - Reuse isFullyQualifiedClass() instead of duplicating the check - Add getAttributeValuesAsArray() for non-string array values (no quotes) - Wrap comma-separated values in braces for array-typed value attributes
When appending a fully qualified class (like com.example.MyClass.class) to an existing class array annotation attribute: 1. Use JavaTemplate.builder() with dependsOn to provide a class stub so the created expression has proper type information 2. Use the FQ class name in the template so it can resolve against the stub 3. Add ShortenFullyQualifiedTypeReferences as doAfterVisit to shorten the FQ name to the simple name (MyClass.class) 4. Fix attributeNameOrValIsAlreadyPresent to handle FQ class names when checking for duplicates by comparing both the full string and the simplified class name 5. Fix maybeAddImport to use onlyIfReferenced=false for FQ classes (same as we did for enums)
timtebeek
reviewed
Feb 12, 2026
timtebeek
reviewed
Feb 12, 2026
timtebeek
reviewed
Feb 12, 2026
- Replace attributeIsString check with attributeIsEnum for FQ enum detection - Add comments explaining why onlyIfReferenced=false is needed
timtebeek
reviewed
Feb 12, 2026
timtebeek
approved these changes
Feb 12, 2026
Member
timtebeek
left a comment
There was a problem hiding this comment.
Neat addition, thanks! Good to get more mileage out of this recipe with some bugs squashed to boot.
Restore ShortenFullyQualifiedTypeReferences which is needed when the FQ class is in the same package as the current file.
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
New functionalities
Import and shorten fully qualified enum values
Append class values to existing single-class annotations
Bug fixes
Empty array handling with
appendArray=trueAttribute name vs value check in array updates
// Was checking attributeValue instead of attributeName when updating arraysComma-separated class values incorrectly matched as FQ class
Missing type info when appending FQ class values