Skip to content

Commit

Permalink
GH-1307: add support for @ConditionalOnBean annotation attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ksankaranara committed Oct 2, 2024
1 parent 609736d commit 8db8725
Show file tree
Hide file tree
Showing 14 changed files with 1,013 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.springframework.ide.vscode.boot.java.annotations.AnnotationAttributeCompletionProcessor;
import org.springframework.ide.vscode.boot.java.annotations.AnnotationHierarchies;
import org.springframework.ide.vscode.boot.java.beans.DependsOnCompletionProcessor;
import org.springframework.ide.vscode.boot.java.beans.BeanNamesCompletionProcessor;
import org.springframework.ide.vscode.boot.java.beans.BeanTypesCompletionProcessor;
import org.springframework.ide.vscode.boot.java.beans.NamedCompletionProvider;
import org.springframework.ide.vscode.boot.java.beans.ProfileCompletionProvider;
import org.springframework.ide.vscode.boot.java.beans.QualifierCompletionProvider;
Expand Down Expand Up @@ -125,6 +127,8 @@ BootJavaCompletionEngine javaCompletionEngine(

providers.put(Annotations.SCOPE, new AnnotationAttributeCompletionProcessor(javaProjectFinder, Map.of("value", new ScopeCompletionProcessor())));
providers.put(Annotations.DEPENDS_ON, new AnnotationAttributeCompletionProcessor(javaProjectFinder, Map.of("value", new DependsOnCompletionProcessor(springIndex))));
providers.put(Annotations.CONDITIONAL_ON_BEAN, new AnnotationAttributeCompletionProcessor(javaProjectFinder, Map.of("name", new BeanNamesCompletionProcessor(springIndex),"type", new BeanTypesCompletionProcessor(springIndex))));

providers.put(Annotations.QUALIFIER, new AnnotationAttributeCompletionProcessor(javaProjectFinder, Map.of("value", new QualifierCompletionProvider(springIndex))));
providers.put(Annotations.PROFILE, new AnnotationAttributeCompletionProcessor(javaProjectFinder, Map.of("value", new ProfileCompletionProvider(springIndex))));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.springframework.ide.vscode.boot.java.beans.ResourceDefinitionProvider;
import org.springframework.ide.vscode.boot.java.conditionalonresource.ConditionalOnResourceDefinitionProvider;
import org.springframework.ide.vscode.boot.java.copilot.util.ResponseModifier;
import org.springframework.ide.vscode.boot.java.beans.ConditionalOnBeanDefinitionProvider;
import org.springframework.ide.vscode.boot.java.data.jpa.queries.DataQueryParameterDefinitionProvider;
import org.springframework.ide.vscode.boot.java.data.jpa.queries.JdtDataQuerySemanticTokensProvider;
import org.springframework.ide.vscode.boot.java.handlers.BootJavaCodeActionProvider;
Expand Down Expand Up @@ -404,6 +405,7 @@ JavaDefinitionHandler javaDefinitionHandler(SimpleLanguageServer server, Compila
new ValueDefinitionProvider(),
new ConditionalOnResourceDefinitionProvider(),
new DependsOnDefinitionProvider(springIndex),
new ConditionalOnBeanDefinitionProvider(springIndex),
new ResourceDefinitionProvider(springIndex),
new QualifierDefinitionProvider(springIndex),
new NamedDefinitionProvider(springIndex),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,13 @@ else if (node instanceof StringLiteral && node.getParent() instanceof Annotation
computeProposalsForStringLiteral(project, node, "value", completions, offset, doc);
}
}
// case: @Qualifier({"prefix<*>"})
// case: @Qualifier({"prefix<*>"}) || @Qualifier(value={"prefix<*>"})
else if (node instanceof StringLiteral && node.getParent() instanceof ArrayInitializer) {
if (node.toString().startsWith("\"") && node.toString().endsWith("\"")) {
if (node.getParent().getParent() instanceof MemberValuePair) {
String attributeName = ((MemberValuePair)node.getParent().getParent()).getName().toString();
computeProposalsForInsideArrayInitializer(project, node, attributeName, completions, offset, doc);
}
computeProposalsForInsideArrayInitializer(project, node, "value", completions, offset, doc);
}
}
Expand All @@ -106,6 +110,12 @@ else if (node instanceof StringLiteral && node.getParent() instanceof MemberValu
else if (node instanceof ArrayInitializer && node.getParent() instanceof Annotation) {
computeProposalsForArrayInitializr(project, (ArrayInitializer) node, "value", completions, offset, doc);
}
// case: @Qualifier(value={<*>})
else if (node instanceof ArrayInitializer && node.getParent() instanceof MemberValuePair
&& completionProviders.containsKey(((MemberValuePair)node.getParent()).getName().toString())) {
String attributeName = ((MemberValuePair)node.getParent()).getName().toString();
computeProposalsForArrayInitializr(project, (ArrayInitializer) node, attributeName, completions, offset, doc);
}
}
catch (Exception e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*******************************************************************************
* Copyright (c) 2024 Broadcom
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Broadcom - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.boot.java.beans;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
import org.springframework.ide.vscode.boot.java.annotations.AnnotationAttributeCompletionProvider;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.protocol.spring.Bean;

/**
* @author Karthik Sankaranarayanan
*/
public class BeanNamesCompletionProcessor implements AnnotationAttributeCompletionProvider {

private final SpringMetamodelIndex springIndex;

public BeanNamesCompletionProcessor(SpringMetamodelIndex springIndex) {
this.springIndex = springIndex;
}

@Override
public Map<String, String> getCompletionCandidates(IJavaProject project) {
Bean[] beans = this.springIndex.getBeansOfProject(project.getElementName());
return Arrays.stream(beans)
.map(Bean::getName)
.distinct()
.collect(Collectors.toMap(key -> key, value -> value, (u, v) -> u, LinkedHashMap::new));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*******************************************************************************
* Copyright (c) 2024 Broadcom
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Broadcom - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.boot.java.beans;

import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
import org.springframework.ide.vscode.boot.java.annotations.AnnotationAttributeCompletionProvider;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.protocol.spring.Bean;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Karthik Sankaranarayanan
*/
public class BeanTypesCompletionProcessor implements AnnotationAttributeCompletionProvider {

private final SpringMetamodelIndex springIndex;

public BeanTypesCompletionProcessor(SpringMetamodelIndex springIndex) {
this.springIndex = springIndex;
}

@Override
public Map<String, String> getCompletionCandidates(IJavaProject project) {
Bean[] beans = this.springIndex.getBeansOfProject(project.getElementName());
return Arrays.stream(beans)
.map(Bean::getType)
.distinct()
.collect(Collectors.toMap(key -> key, value -> value, (u, v) -> u, LinkedHashMap::new));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*******************************************************************************
* Copyright (c) 2024 Broadcom
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Broadcom - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.boot.java.beans;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Annotation;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.IAnnotationBinding;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.lsp4j.LocationLink;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
import org.springframework.ide.vscode.boot.java.Annotations;
import org.springframework.ide.vscode.boot.java.IJavaDefinitionProvider;
import org.springframework.ide.vscode.boot.java.utils.ASTUtils;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.protocol.spring.Bean;

/**
* @author Karthik Sankaranarayanan
*/
public class ConditionalOnBeanDefinitionProvider implements IJavaDefinitionProvider {

private final SpringMetamodelIndex springIndex;

public ConditionalOnBeanDefinitionProvider(SpringMetamodelIndex springIndex) {
this.springIndex = springIndex;
}

@Override
public List<LocationLink> getDefinitions(CancelChecker cancelToken, IJavaProject project, TextDocumentIdentifier docId, CompilationUnit cu, ASTNode n, int offset) {
if (n instanceof StringLiteral) {
StringLiteral valueNode = (StringLiteral) n;

ASTNode parent = ASTUtils.getNearestAnnotationParent(valueNode);

if (parent != null && parent instanceof Annotation) {
Annotation a = (Annotation) parent;
IAnnotationBinding binding = a.resolveAnnotationBinding();
if (binding != null && binding.getAnnotationType() != null && Annotations.CONDITIONAL_ON_BEAN.equals(binding.getAnnotationType().getQualifiedName())) {
String beanName = valueNode.getLiteralValue();

if (beanName != null && beanName.length() > 0) {
return findBeansWithName(project, beanName);
}
}
}
}
return Collections.emptyList();
}

private List<LocationLink> findBeansWithName(IJavaProject project, String beanName) {
Bean[] beans = this.springIndex.getBeansWithName(project.getElementName(), beanName);

return Arrays.stream(beans)
.map(bean -> {
return new LocationLink(bean.getLocation().getUri(), bean.getLocation().getRange(), bean.getLocation().getRange());
})
.collect(Collectors.toList());
}

}
Loading

0 comments on commit 8db8725

Please sign in to comment.