Skip to content

Commit

Permalink
GH-1043: create correct bean index element for functional beans that …
Browse files Browse the repository at this point in the history
…implement the function interface

Fixes GH-1043
  • Loading branch information
martinlippert committed Jan 13, 2025
1 parent f58305d commit c1b1a06
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,31 @@ protected void addSymbolsPass1(TypeDeclaration typeDeclaration, SpringIndexerJav
Tuple3<String, ITypeBinding, DocumentRegion> functionBean = FunctionUtils.getFunctionBean(typeDeclaration, doc);
if (functionBean != null) {
try {
String beanName = functionBean.getT1();
ITypeBinding beanType = functionBean.getT2();
Location beanLocation = new Location(doc.getUri(), doc.toRange(functionBean.getT3()));

WorkspaceSymbol symbol = new WorkspaceSymbol(
beanLabel(true, functionBean.getT1(), functionBean.getT2().getName(), null),
beanLabel(true, beanName, beanType.getName(), null),
SymbolKind.Interface,
Either.forLeft(new Location(doc.getUri(), doc.toRange(functionBean.getT3()))));
Either.forLeft(beanLocation));

context.getGeneratedSymbols().add(new CachedSymbol(context.getDocURI(), context.getLastModified(),
new EnhancedSymbolInformation(symbol, null)));


ITypeBinding concreteBeanType = typeDeclaration.resolveBinding();
Set<String> supertypes = new HashSet<>();
ASTUtils.findSupertypes(concreteBeanType, supertypes);

Collection<Annotation> annotationsOnTypeDeclaration = ASTUtils.getAnnotations(typeDeclaration);
AnnotationMetadata[] annotations = ASTUtils.getAnnotationsMetadata(annotationsOnTypeDeclaration, doc);

InjectionPoint[] injectionPoints = ASTUtils.findInjectionPoints(typeDeclaration, doc);

Bean beanDefinition = new Bean(beanName, concreteBeanType.getQualifiedName(), beanLocation, injectionPoints, supertypes, annotations, false);
context.getBeans().add(new CachedBean(context.getDocURI(), beanDefinition));

} catch (BadLocationException e) {
log.error("", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ void testScanSimpleFunctionClass() throws Exception {
SpringIndexerHarness.assertDocumentSymbols(indexer, docUri,
SpringIndexerHarness.symbol("ScannedFunctionClass", "@> 'scannedFunctionClass' Function<String,String>")
);

Bean[] beans = springIndex.getBeansOfDocument(docUri);
assertEquals(1, beans.length);

Bean functionClassBean = Arrays.stream(beans).filter(bean -> bean.getName().equals("scannedFunctionClass")).findFirst().get();

assertEquals("org.test.ScannedFunctionClass", functionClassBean.getType());
}

@Test
Expand All @@ -95,6 +102,13 @@ void testScanSpecializedFunctionClass() throws Exception {
SpringIndexerHarness.assertDocumentSymbols(indexer, docUri,
SpringIndexerHarness.symbol("FunctionFromSpecializedClass", "@> 'functionFromSpecializedClass' Function<String,String>")
);

Bean[] beans = springIndex.getBeansOfDocument(docUri);
assertEquals(1, beans.length);

Bean functionClassBean = Arrays.stream(beans).filter(bean -> bean.getName().equals("functionFromSpecializedClass")).findFirst().get();

assertEquals("org.test.FunctionFromSpecializedClass", functionClassBean.getType());
}

@Test
Expand All @@ -103,18 +117,31 @@ void testScanSpecializedFunctionInterface() throws Exception {
SpringIndexerHarness.assertDocumentSymbols(indexer, docUri,
SpringIndexerHarness.symbol("FunctionFromSpecializedInterface", "@> 'functionFromSpecializedInterface' Function<String,String>")
);

Bean[] beans = springIndex.getBeansOfDocument(docUri);
assertEquals(1, beans.length);

Bean functionClassBean = Arrays.stream(beans).filter(bean -> bean.getName().equals("functionFromSpecializedInterface")).findFirst().get();

assertEquals("org.test.FunctionFromSpecializedInterface", functionClassBean.getType());
}

@Test
void testNoSymbolForAbstractClasses() throws Exception {
String docUri = directory.toPath().resolve("src/main/java/org/test/SpecializedFunctionClass.java").toUri().toString();
SpringIndexerHarness.assertDocumentSymbols(indexer, docUri);

Bean[] beans = springIndex.getBeansOfDocument(docUri);
assertEquals(0, beans.length);
}

@Test
void testNoSymbolForSubInterfaces() throws Exception {
String docUri = directory.toPath().resolve("src/main/java/org/test/SpecializedFunctionInterface.java").toUri().toString();
SpringIndexerHarness.assertDocumentSymbols(indexer, docUri);

Bean[] beans = springIndex.getBeansOfDocument(docUri);
assertEquals(0, beans.length);
}

}

0 comments on commit c1b1a06

Please sign in to comment.