GH-1451: component symbol provider now takes attribute value into account when calculating the bean name

Fixes GH-1451
This commit is contained in:
Martin Lippert
2025-01-17 21:40:22 +01:00
parent 58ee5ae463
commit 4e90e8137a
3 changed files with 36 additions and 4 deletions

View File

@@ -13,11 +13,13 @@ package org.springframework.ide.vscode.boot.java.beans;
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 @@ public class ComponentSymbolProvider extends AbstractSymbolProvider {
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 @@ public class ComponentSymbolProvider extends AbstractSymbolProvider {
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<Expression> 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) {

View File

@@ -145,6 +145,22 @@ public class SpringIndexerBeansTest {
);
}
@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();

View File

@@ -0,0 +1,7 @@
package org.test;
import org.springframework.stereotype.Component;
@Component(value = "specialNameWithAttributeName")
public class SpecialNameComponentWithAttributeName {
}