Skip to content

Commit 3391782

Browse files
committed
include multiple names in bean label and eliminate duplicate meta annotations from it
1 parent 77a0bd5 commit 3391782

File tree

3 files changed

+52
-16
lines changed

3 files changed

+52
-16
lines changed

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/beans/BeanUtils.java

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*******************************************************************************/
1111
package org.springframework.ide.vscode.boot.java.beans;
1212

13+
import java.util.ArrayList;
1314
import java.util.Collection;
1415
import java.util.List;
1516
import java.util.Map;
@@ -71,26 +72,27 @@ public static String createBeanLabel(List<AnnotationMetadata> annotations, Strin
7172
symbolLabel.append('\'');
7273

7374
if (annotations != null) {
74-
List<AnnotationMetadata> directAnnotations = annotations.stream()
75+
List<String> directAnnotations = annotations.stream()
7576
.filter(annotation -> ANNOTATIONS_TO_INCLUDE_IN_LABEL.contains(annotation.getAnnotationType()))
76-
.filter(annotation -> !annotation.isMetaAnnotation()).toList();
77+
.filter(annotation -> !annotation.isMetaAnnotation())
78+
.map(annotation -> "@" + annotation.getAnnotationName())
79+
.toList();
7780

78-
List<AnnotationMetadata> metaAnnotations = annotations.stream()
81+
List<String> metaAnnotationNames = annotations.stream()
7982
.filter(annotation -> ANNOTATIONS_TO_INCLUDE_IN_LABEL.contains(annotation.getAnnotationType()))
80-
.filter(annotation -> annotation.isMetaAnnotation()).toList();
83+
.filter(annotation -> annotation.isMetaAnnotation())
84+
.map(annotation -> "@" + annotation.getAnnotationName())
85+
.distinct()
86+
.toList();
8187

8288
if (directAnnotations.size() > 0) {
8389
symbolLabel.append(' ');
8490
symbolLabel.append('(');
85-
symbolLabel.append(String.join(", ", directAnnotations.stream()
86-
.map(annotation -> "@" + annotation.getAnnotationName())
87-
.toList()));
91+
symbolLabel.append(String.join(", ", directAnnotations));
8892

89-
if (metaAnnotations.size() > 0) {
93+
if (metaAnnotationNames.size() > 0) {
9094
symbolLabel.append(" <: ");
91-
symbolLabel.append(String.join(", ", metaAnnotations.stream()
92-
.map(annotation -> "@" + annotation.getAnnotationName())
93-
.toList()));
95+
symbolLabel.append(String.join(", ", metaAnnotationNames));
9496
}
9597

9698
symbolLabel.append(')');
@@ -102,7 +104,8 @@ public static String createBeanLabel(List<AnnotationMetadata> annotations, Strin
102104
return symbolLabel.toString();
103105
}
104106

105-
public static String getBeanNameFromType(AbstractTypeDeclaration type, List<AnnotationMetadata> annotationMetadata) {
107+
public static List<String> getBeanNamesFromType(AbstractTypeDeclaration type, List<AnnotationMetadata> annotationMetadata) {
108+
List<String> result = new ArrayList<>();
106109

107110
// annotation-defined names
108111
if (annotationMetadata != null) {
@@ -112,17 +115,33 @@ public static String getBeanNameFromType(AbstractTypeDeclaration type, List<Anno
112115
Map<String, AnnotationAttributeValue[]> attributes = annotation.getAttributes();
113116
if (attributes != null) {
114117
AnnotationAttributeValue[] values = attributes.get("value");
115-
if (values != null && values.length > 0) {
116-
return values[0].getName();
118+
if (values != null) {
119+
for (int i = 0; i < values.length; i++) {
120+
result.add(values[i].getName());
121+
}
117122
}
118123
}
119124
}
120125
}
121126
}
122127

123128
// otherwise fall back to type name;
124-
String beanType = type.getName().toString();
125-
return BeanUtils.getBeanNameFromType(beanType);
129+
if (result.size() == 0 && type.getName() != null) {
130+
String beanType = type.getName().toString();
131+
result.add(BeanUtils.getBeanNameFromType(beanType));
132+
}
133+
134+
return result;
135+
}
136+
137+
public static String getBeanNameFromType(AbstractTypeDeclaration type, List<AnnotationMetadata> annotationMetadata) {
138+
List<String> beanNames = getBeanNamesFromType(type, annotationMetadata);
139+
if (beanNames.size() == 1) {
140+
return beanNames.get(0);
141+
}
142+
else {
143+
return "(" + String.join(", ", beanNames) + ")";
144+
}
126145
}
127146

128147
public static String getBeanNameFromComponentAnnotation(Annotation annotation, AbstractTypeDeclaration type) {

headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/java/beans/test/SpringIndexerBeansTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,5 +233,13 @@ void testCustomAnnotationClass() throws Exception {
233233
SpringIndexerHarness.symbol("@AliasFor(annotation = Component.class)", "@AliasFor(annotation=Component.class)")
234234
);
235235
}
236+
237+
@Test
238+
void testScanControllerWithTwoNames() throws Exception {
239+
String docUri = directory.toPath().resolve("src/main/java/org/test/ControllerWithTwoNames.java").toUri().toString();
240+
SpringIndexerHarness.assertDocumentSymbols(indexer, docUri,
241+
SpringIndexerHarness.symbol("ControllerWithTwoNames", "@+ '(controllerName, configName)' (@Controller, @Configuration <: @Component) ControllerWithTwoNames")
242+
);
243+
}
236244

237245
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.test;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.stereotype.Controller;
5+
6+
@Controller("controllerName")
7+
@Configuration("configName")
8+
public class ControllerWithTwoNames {
9+
}

0 commit comments

Comments
 (0)