Skip to content

Commit 4e90e81

Browse files
committed
GH-1451: component symbol provider now takes attribute value into account when calculating the bean name
Fixes GH-1451
1 parent 58ee5ae commit 4e90e81

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
import java.util.Arrays;
1414
import java.util.Collection;
1515
import java.util.HashSet;
16+
import java.util.Optional;
1617
import java.util.Set;
1718
import java.util.stream.Collectors;
1819
import java.util.stream.Stream;
1920

2021
import org.eclipse.jdt.core.dom.Annotation;
22+
import org.eclipse.jdt.core.dom.Expression;
2123
import org.eclipse.jdt.core.dom.ITypeBinding;
2224
import org.eclipse.jdt.core.dom.TypeDeclaration;
2325
import org.eclipse.lsp4j.Location;
@@ -80,7 +82,7 @@ protected Tuple.Two<EnhancedSymbolInformation, Bean> createSymbol(Annotation nod
8082

8183
TypeDeclaration type = (TypeDeclaration) node.getParent();
8284

83-
String beanName = getBeanName(type);
85+
String beanName = getBeanName(node, type);
8486
ITypeBinding beanType = getBeanType(type);
8587

8688
Location location = new Location(doc.getUri(), doc.toRange(node.getStartPosition(), node.getLength()));
@@ -138,9 +140,16 @@ protected String beanLabel(String searchPrefix, String annotationTypeName, Colle
138140
return symbolLabel.toString();
139141
}
140142

141-
private String getBeanName(TypeDeclaration type) {
142-
String beanName = type.getName().toString();
143-
return BeanUtils.getBeanNameFromType(beanName);
143+
public static String getBeanName(Annotation annotation, TypeDeclaration type) {
144+
Optional<Expression> attribute = ASTUtils.getAttribute(annotation, "value");
145+
if (attribute.isPresent()) {
146+
return ASTUtils.getExpressionValueAsString(attribute.get(), (a) -> {});
147+
}
148+
else {
149+
String beanName = type.getName().toString();
150+
return BeanUtils.getBeanNameFromType(beanName);
151+
}
152+
144153
}
145154

146155
private ITypeBinding getBeanType(TypeDeclaration type) {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,22 @@ void testScanSimpleComponentClass() throws Exception {
145145
);
146146
}
147147

148+
@Test
149+
void testScanComponentClassWithName() throws Exception {
150+
String docUri = directory.toPath().resolve("src/main/java/org/test/SpecialNameComponent.java").toUri().toString();
151+
SpringIndexerHarness.assertDocumentSymbols(indexer, docUri,
152+
SpringIndexerHarness.symbol("@Component(\"specialName\")", "@+ 'specialName' (@Component) SpecialNameComponent")
153+
);
154+
}
155+
156+
@Test
157+
void testScanComponentClassWithNameAndAttributeName() throws Exception {
158+
String docUri = directory.toPath().resolve("src/main/java/org/test/SpecialNameComponentWithAttributeName.java").toUri().toString();
159+
SpringIndexerHarness.assertDocumentSymbols(indexer, docUri,
160+
SpringIndexerHarness.symbol("@Component(value = \"specialNameWithAttributeName\")", "@+ 'specialNameWithAttributeName' (@Component) SpecialNameComponentWithAttributeName")
161+
);
162+
}
163+
148164
@Test
149165
void testScanSimpleControllerClass() throws Exception {
150166
String docUri = directory.toPath().resolve("src/main/java/org/test/SimpleController.java").toUri().toString();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.test;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component(value = "specialNameWithAttributeName")
6+
public class SpecialNameComponentWithAttributeName {
7+
}

0 commit comments

Comments
 (0)