Skip to content

Commit

Permalink
Bean completion for all component beans (includes RestController)
Browse files Browse the repository at this point in the history
  • Loading branch information
BoykoAlex committed Feb 26, 2025
1 parent b47096e commit 07f930f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.FieldAccess;
import org.eclipse.jdt.core.dom.IAnnotationBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.ThisExpression;
Expand All @@ -32,6 +31,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
import org.springframework.ide.vscode.boot.java.Annotations;
import org.springframework.ide.vscode.boot.java.annotations.AnnotationHierarchies;
import org.springframework.ide.vscode.boot.java.handlers.CompletionProvider;
import org.springframework.ide.vscode.boot.java.rewrite.RewriteRefactorings;
import org.springframework.ide.vscode.commons.java.IJavaProject;
Expand Down Expand Up @@ -92,7 +93,8 @@ public void provideCompletions(ASTNode node, int offset, TextDocument doc,
node = fa.getName();
}

if (isSpringComponent(topLevelClass)) {

if (AnnotationHierarchies.get(node).isAnnotatedWith(topLevelClass.resolveBinding(), Annotations.COMPONENT)) {
String className = getFullyQualifiedName(topLevelClass);
Bean[] beans = this.springIndex.getBeansOfProject(project.getElementName());
ITypeBinding topLevelBeanType = topLevelClass.resolveBinding();
Expand Down Expand Up @@ -133,28 +135,6 @@ public void provideCompletions(ASTNode node, int offset, TextDocument doc,
}
}

private static boolean isSpringComponent(TypeDeclaration node) {
for (IAnnotationBinding annotation : node.resolveBinding().getAnnotations()) {
if (isSpringComponentAnnotation(annotation)) {
return true;
}
}
return false;
}

private static boolean isSpringComponentAnnotation(IAnnotationBinding annotation) {
String annotationName = annotation.getAnnotationType().getQualifiedName();
if (annotationName.equals("org.springframework.stereotype.Component")) {
return true;
}
for (IAnnotationBinding metaAnnotation : annotation.getAnnotationType().getAnnotations()) {
if (metaAnnotation.getAnnotationType().getQualifiedName().equals("org.springframework.stereotype.Component")) {
return true;
}
}
return false;
}

private static TypeDeclaration findParentClass(ASTNode node) {
ASTNode current = node;
while (current != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,43 @@ public void test() {
""");
}

@Test
public void completionsForRestController() throws Exception {
String content = """
package org.sample.test;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestBeanCompletionClass {
public void test() {
owner<*>
}
}
""";


assertCompletions(content, new String[] {"ownerRepository", "ownerService"}, 0,
"""
package org.sample.test;
import org.springframework.samples.petclinic.owner.OwnerRepository;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestBeanCompletionClass {
private final OwnerRepository ownerRepository;
TestBeanCompletionClass(OwnerRepository ownerRepository) {
this.ownerRepository = ownerRepository;
}
public void test() {
ownerRepository<*>
}
}
""");
}

private void assertCompletions(String completionLine, String[] expectedCompletions, int chosenCompletion, String expectedResult) throws Exception {
assertCompletions(completionLine, expectedCompletions.length, expectedCompletions, chosenCompletion, expectedResult);
Expand Down

0 comments on commit 07f930f

Please sign in to comment.