Skip to content

Commit 75c247c

Browse files
committed
GH-1812: avoid duplicate index elements and symbols for spring data repositories
Signed-off-by: Martin Lippert <martin.lippert@broadcom.com>
1 parent 3660e93 commit 75c247c

3 files changed

Lines changed: 40 additions & 3 deletions

File tree

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,12 @@ public void addSymbols(TypeDeclaration typeDeclaration, SpringIndexerJavaContext
101101
|| annotationHierarchies.isAnnotatedWith(typeDeclaration.resolveBinding(), Annotations.NAMED_JAKARTA)
102102
|| annotationHierarchies.isAnnotatedWith(typeDeclaration.resolveBinding(), Annotations.NAMED_JAVAX);
103103

104-
// check for event listener implementations on classes that are not annotated with component, but created via bean methods (for example)
105104
if (isComponment) {
106105
indexComponent(typeDeclaration, context, context.getDoc());
107106
}
108107
else {
108+
// check for event listener implementations on classes that are not annotated with component, but created via bean methods (for example)
109+
109110
indexEventListenerInterfaceImplementation(null, typeDeclaration, context, context.getDoc());
110111
indexBeanRegistrarImplementation(null, typeDeclaration, context, context.getDoc());
111112
indexBeanMethods(null, typeDeclaration, context, context.getDoc());
@@ -130,6 +131,12 @@ private void createSymbol(TypeDeclaration type, SpringIndexerJavaContext context
130131
Location location = new Location(doc.getUri(), doc.toRange(nameNode.getStartPosition(), nameNode.getLength()));
131132

132133
boolean isConfiguration = annotationHierarchies.isAnnotatedWith(beanType, Annotations.CONFIGURATION);
134+
boolean isRepository = annotationHierarchies.isAnnotatedWith(beanType, Annotations.REPOSITORY);
135+
136+
// defer repository indexing to repository symbol provider
137+
if (isRepository) {
138+
return;
139+
}
133140

134141
InjectionPoint[] injectionPoints = ASTUtils.findInjectionPoints(type, doc);
135142
Set<String> supertypes = ASTUtils.findSupertypes(beanType);
@@ -212,7 +219,6 @@ private void createSymbol(RecordDeclaration record, Annotation node, ITypeBindin
212219
}
213220

214221
private void createSymbol(AnnotationTypeDeclaration annotationDeclaration, SpringIndexerJavaContext context, TextDocument doc) {
215-
216222
SpringBootApplicationIndexer.createIndexElement(annotationDeclaration, annotationDeclaration.resolveBinding(), context);
217223
}
218224

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,25 @@ void testSimpleRepositoryElements() throws Exception {
8484
assertEquals("org.test.CustomerRepository", repoBean[0].getType());
8585

8686
Bean[] matchingBeans = springIndex.getMatchingBeans("test-spring-data-symbols", "org.springframework.data.repository.CrudRepository");
87-
assertEquals(5, matchingBeans.length);
87+
assertEquals(6, matchingBeans.length);
8888
ArrayUtils.contains(matchingBeans, repoBean[0]);
8989
}
90+
91+
@Test
92+
void testRepositoryWithAdditionalRepositoryAnnotation() throws Exception {
93+
String docUri = directory.toPath().resolve("src/main/java/org/test/CustomerRepositoryWithAdditionalRepositoryAnnotation.java").toUri().toString();
94+
95+
DocumentElement document = springIndex.getDocument(docUri);
96+
List<Bean> children = SpringMetamodelIndex.getNodesOfType(Bean.class, List.of(document));
97+
assertEquals(1, children.size());
98+
99+
Bean repositoryElement = children.get(0);
100+
assertEquals("customerRepositoryWithAdditionalRepositoryAnnotation", repositoryElement.getName());
101+
assertEquals(1, children.size());
102+
103+
Bean[] repoBean = this.springIndex.getBeansWithName("test-spring-data-symbols", "customerRepositoryWithAdditionalRepositoryAnnotation");
104+
assertEquals(1, repoBean.length);
105+
}
90106

91107
@Test
92108
void testSimpleQueryMethodElements() throws Exception {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.test;
2+
3+
import java.util.List;
4+
5+
import org.springframework.data.repository.CrudRepository;
6+
import org.springframework.stereotype.Repository;
7+
8+
@Repository
9+
public interface CustomerRepositoryWithAdditionalRepositoryAnnotation extends CrudRepository<Customer, Long> {
10+
11+
List<Customer> findByLastName(String lastName);
12+
13+
default List<Customer> findByWhatever(String lastName) {return null;}
14+
15+
}

0 commit comments

Comments
 (0)