Skip to content

Commit

Permalink
GH-1451: component symbol provider now takes attribute value into acc…
Browse files Browse the repository at this point in the history
…ount when calculating the bean name

Fixes GH-1451
  • Loading branch information
martinlippert committed Jan 17, 2025
1 parent 58ee5ae commit 4e90e81
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,7 +82,7 @@ protected Tuple.Two<EnhancedSymbolInformation, Bean> 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()));
Expand Down Expand Up @@ -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<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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.test;

import org.springframework.stereotype.Component;

@Component(value = "specialNameWithAttributeName")
public class SpecialNameComponentWithAttributeName {
}

0 comments on commit 4e90e81

Please sign in to comment.