diff --git a/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/beans/ComponentSymbolProvider.java b/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/beans/ComponentSymbolProvider.java index 515dffff05..f34dbee699 100644 --- a/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/beans/ComponentSymbolProvider.java +++ b/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/beans/ComponentSymbolProvider.java @@ -13,11 +13,13 @@ import java.util.Arrays; import java.util.Collection; import java.util.HashSet; +import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; import org.eclipse.jdt.core.dom.Annotation; +import org.eclipse.jdt.core.dom.Expression; import org.eclipse.jdt.core.dom.ITypeBinding; import org.eclipse.jdt.core.dom.TypeDeclaration; import org.eclipse.lsp4j.Location; @@ -80,7 +82,7 @@ protected Tuple.Two createSymbol(Annotation nod TypeDeclaration type = (TypeDeclaration) node.getParent(); - String beanName = getBeanName(type); + String beanName = getBeanName(node, type); ITypeBinding beanType = getBeanType(type); Location location = new Location(doc.getUri(), doc.toRange(node.getStartPosition(), node.getLength())); @@ -138,9 +140,16 @@ protected String beanLabel(String searchPrefix, String annotationTypeName, Colle return symbolLabel.toString(); } - private String getBeanName(TypeDeclaration type) { - String beanName = type.getName().toString(); - return BeanUtils.getBeanNameFromType(beanName); + public static String getBeanName(Annotation annotation, TypeDeclaration type) { + Optional attribute = ASTUtils.getAttribute(annotation, "value"); + if (attribute.isPresent()) { + return ASTUtils.getExpressionValueAsString(attribute.get(), (a) -> {}); + } + else { + String beanName = type.getName().toString(); + return BeanUtils.getBeanNameFromType(beanName); + } + } private ITypeBinding getBeanType(TypeDeclaration type) { diff --git a/headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/java/beans/test/SpringIndexerBeansTest.java b/headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/java/beans/test/SpringIndexerBeansTest.java index e4d0c8df0a..2beeab1303 100644 --- a/headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/java/beans/test/SpringIndexerBeansTest.java +++ b/headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/java/beans/test/SpringIndexerBeansTest.java @@ -145,6 +145,22 @@ void testScanSimpleComponentClass() throws Exception { ); } + @Test + void testScanComponentClassWithName() throws Exception { + String docUri = directory.toPath().resolve("src/main/java/org/test/SpecialNameComponent.java").toUri().toString(); + SpringIndexerHarness.assertDocumentSymbols(indexer, docUri, + SpringIndexerHarness.symbol("@Component(\"specialName\")", "@+ 'specialName' (@Component) SpecialNameComponent") + ); + } + + @Test + void testScanComponentClassWithNameAndAttributeName() throws Exception { + String docUri = directory.toPath().resolve("src/main/java/org/test/SpecialNameComponentWithAttributeName.java").toUri().toString(); + SpringIndexerHarness.assertDocumentSymbols(indexer, docUri, + SpringIndexerHarness.symbol("@Component(value = \"specialNameWithAttributeName\")", "@+ 'specialNameWithAttributeName' (@Component) SpecialNameComponentWithAttributeName") + ); + } + @Test void testScanSimpleControllerClass() throws Exception { String docUri = directory.toPath().resolve("src/main/java/org/test/SimpleController.java").toUri().toString(); diff --git a/headless-services/spring-boot-language-server/src/test/resources/test-projects/test-annotation-indexing-beans/src/main/java/org/test/SpecialNameComponentWithAttributeName.java b/headless-services/spring-boot-language-server/src/test/resources/test-projects/test-annotation-indexing-beans/src/main/java/org/test/SpecialNameComponentWithAttributeName.java new file mode 100644 index 0000000000..6f592eef44 --- /dev/null +++ b/headless-services/spring-boot-language-server/src/test/resources/test-projects/test-annotation-indexing-beans/src/main/java/org/test/SpecialNameComponentWithAttributeName.java @@ -0,0 +1,7 @@ +package org.test; + +import org.springframework.stereotype.Component; + +@Component(value = "specialNameWithAttributeName") +public class SpecialNameComponentWithAttributeName { +}