Skip to content

Commit 51e25c7

Browse files
authored
Default value for the type annotation attribute should be Object.class (#1381)
* Default value for the type annotation attribute should be Object.class * fixed test cases
1 parent a45730c commit 51e25c7

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/codeAction/proposal/ModifyAnnotationProposal.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public Change getChange() {
8383
for (String newAttr : this.attributesToAdd) {
8484
// don't add duplicate attributes to an annotation
8585
if (Arrays.stream(values).noneMatch(v -> v.getName().equals(newAttr))) {
86-
annotation.setDeclaredAttributeValue(newAttr, newDefaultExpression(annotation));
86+
annotation.setDeclaredAttributeValue(newAttr, newDefaultExpression(annotation, newAttr));
8787
}
8888
}
8989
// remove attributes
@@ -100,8 +100,26 @@ public Change getChange() {
100100
return new Change(sourceCU.getViewProvider().getDocument(), changed);
101101
}
102102

103-
private PsiAnnotationMemberValue newDefaultExpression(PsiAnnotation annotation) {
104-
return PsiElementFactory.getInstance(annotation.getProject()).
105-
createExpressionFromText("\"\"", annotation);
103+
/**
104+
* newDefaultExpression
105+
* Add new attributes of type String or Class.
106+
* For initial values, we use empty strings for String types and Object.class for Class types,
107+
* since the user's intended values are unknown at this stage,
108+
* These placeholders (e.g., name = "", type = Object.class) must be updated by the user as needed.
109+
* when an annotation in Jakarta EE declares an attribute named type, it’s always of the form of Class<?>
110+
*
111+
* @param annotation
112+
* @param attributeName
113+
* @return
114+
*/
115+
private PsiAnnotationMemberValue newDefaultExpression(PsiAnnotation annotation, String attributeName) {
116+
if ("type".equals(attributeName)) {
117+
return PsiElementFactory.getInstance(annotation.getProject())
118+
.createExpressionFromText("Object.class", annotation);
119+
} else {
120+
return PsiElementFactory.getInstance(annotation.getProject()).
121+
createExpressionFromText("\"\"", annotation);
122+
}
123+
106124
}
107125
}

src/test/java/io/openliberty/tools/intellij/lsp4jakarta/it/annotations/ResourceAnnotationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void ResourceAnnotation() throws Exception {
6565
"{\n\n private Integer studentId;\n\n\n @Resource(shareable = true)\n " +
6666
"private boolean isHappy;\n\n @Resource(name = \"test\")\n " +
6767
" private boolean isSad;\n\n\n private String emailAddress;\n\n\n}\n\n" +
68-
"@Resource(name = \"aa\",type=\"\")\nclass PostDoctoralStudent {\n\n " +
68+
"@Resource(name = \"aa\",type=Object.class)\nclass PostDoctoralStudent {\n\n " +
6969
"private Integer studentId;\n\n\n @Resource(shareable = true)\n " +
7070
"private boolean isHappy;\n\n @Resource\n private boolean isSad;\n\n\n " +
7171
"private String emailAddress;\n\n}\n\n@Resource(type = Object.class)\nclass " +

0 commit comments

Comments
 (0)