-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-1307: add support for @ConditionalOnBean annotation attributes
- Loading branch information
ksankaranara
committed
Oct 2, 2024
1 parent
609736d
commit 8db8725
Showing
14 changed files
with
1,013 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...ain/java/org/springframework/ide/vscode/boot/java/beans/BeanNamesCompletionProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...ain/java/org/springframework/ide/vscode/boot/java/beans/BeanTypesCompletionProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...a/org/springframework/ide/vscode/boot/java/beans/ConditionalOnBeanDefinitionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
Oops, something went wrong.