|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 Broadcom |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Broadcom - initial API and implementation |
| 10 | + *******************************************************************************/ |
| 11 | +package org.springframework.ide.vscode.boot.java.beans; |
| 12 | + |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.List; |
| 15 | +import java.util.stream.Stream; |
| 16 | + |
| 17 | +import org.eclipse.jdt.core.dom.ASTNode; |
| 18 | +import org.eclipse.jdt.core.dom.Annotation; |
| 19 | +import org.eclipse.jdt.core.dom.ITypeBinding; |
| 20 | +import org.eclipse.jdt.core.dom.MemberValuePair; |
| 21 | +import org.eclipse.jdt.core.dom.StringLiteral; |
| 22 | +import org.eclipse.lsp4j.Location; |
| 23 | +import org.eclipse.lsp4j.WorkspaceSymbol; |
| 24 | +import org.eclipse.lsp4j.jsonrpc.CancelChecker; |
| 25 | +import org.springframework.ide.vscode.boot.app.SpringSymbolIndex; |
| 26 | +import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex; |
| 27 | +import org.springframework.ide.vscode.boot.java.handlers.ReferenceProvider; |
| 28 | +import org.springframework.ide.vscode.commons.java.IJavaProject; |
| 29 | +import org.springframework.ide.vscode.commons.protocol.spring.Bean; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author Martin Lippert |
| 33 | + */ |
| 34 | +public class NamedReferencesProvider implements ReferenceProvider { |
| 35 | + |
| 36 | + private final SpringMetamodelIndex springIndex; |
| 37 | + private final SpringSymbolIndex symbolIndex; |
| 38 | + |
| 39 | + public NamedReferencesProvider(SpringMetamodelIndex springIndex, SpringSymbolIndex symbolIndex) { |
| 40 | + this.springIndex = springIndex; |
| 41 | + this.symbolIndex = symbolIndex; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public List<? extends Location> provideReferences(CancelChecker cancelToken, IJavaProject project, ASTNode node, Annotation annotation, ITypeBinding type, int offset) { |
| 46 | + |
| 47 | + cancelToken.checkCanceled(); |
| 48 | + |
| 49 | + try { |
| 50 | + // case: @Value("prefix<*>") |
| 51 | + if (node instanceof StringLiteral && node.getParent() instanceof Annotation) { |
| 52 | + if (node.toString().startsWith("\"") && node.toString().endsWith("\"")) { |
| 53 | + return provideReferences(project, ((StringLiteral) node).getLiteralValue()); |
| 54 | + } |
| 55 | + } |
| 56 | + // case: @Value(value="prefix<*>") |
| 57 | + else if (node instanceof StringLiteral && node.getParent() instanceof MemberValuePair |
| 58 | + && "value".equals(((MemberValuePair)node.getParent()).getName().toString())) { |
| 59 | + if (node.toString().startsWith("\"") && node.toString().endsWith("\"")) { |
| 60 | + return provideReferences(project, ((StringLiteral) node).getLiteralValue()); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + catch (Exception e) { |
| 65 | + e.printStackTrace(); |
| 66 | + } |
| 67 | + |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + private List<? extends Location> provideReferences(IJavaProject project, String value) { |
| 72 | + Bean[] beans = this.springIndex.getBeansOfProject(project.getElementName()); |
| 73 | + |
| 74 | + // beans with name |
| 75 | + Stream<Location> beanLocations = Arrays.stream(beans) |
| 76 | + .filter(bean -> bean.getName().equals(value)) |
| 77 | + .map(bean -> bean.getLocation()); |
| 78 | + |
| 79 | + String exactPhrase1 = "@Named(\"" + value + "\")"; |
| 80 | + String exactPhrase2 = "@Named(value=\"" + value + "\")"; |
| 81 | + |
| 82 | + // qualifier annotations |
| 83 | + List<WorkspaceSymbol> qualifierSymbols1 = symbolIndex.getAllSymbols(exactPhrase1); |
| 84 | + List<WorkspaceSymbol> qualifierSymbols2 = symbolIndex.getAllSymbols(exactPhrase2); |
| 85 | + |
| 86 | + Stream<Location> qualifierLocations = Stream.concat(qualifierSymbols1.stream(), qualifierSymbols2.stream()) |
| 87 | + .filter(symbol -> symbol.getName().contains(exactPhrase1) || symbol.getName().contains(exactPhrase2)) |
| 88 | + .map(symbol -> symbol.getLocation()) |
| 89 | + .filter(location -> location.isLeft()) |
| 90 | + .map(location -> location.getLeft()); |
| 91 | + |
| 92 | + return Stream.concat(qualifierLocations, beanLocations).toList(); |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments