|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 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.Collection; |
| 15 | +import java.util.HashSet; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Set; |
| 18 | +import java.util.stream.Collectors; |
| 19 | +import java.util.stream.Stream; |
| 20 | + |
| 21 | +import org.eclipse.jdt.core.dom.Annotation; |
| 22 | +import org.eclipse.jdt.core.dom.FieldDeclaration; |
| 23 | +import org.eclipse.jdt.core.dom.ITypeBinding; |
| 24 | +import org.eclipse.jdt.core.dom.SimpleName; |
| 25 | +import org.eclipse.jdt.core.dom.Type; |
| 26 | +import org.eclipse.jdt.core.dom.TypeDeclaration; |
| 27 | +import org.eclipse.jdt.core.dom.VariableDeclarationFragment; |
| 28 | +import org.eclipse.lsp4j.Location; |
| 29 | +import org.eclipse.lsp4j.Range; |
| 30 | +import org.eclipse.lsp4j.SymbolKind; |
| 31 | +import org.eclipse.lsp4j.WorkspaceSymbol; |
| 32 | +import org.eclipse.lsp4j.jsonrpc.messages.Either; |
| 33 | +import org.slf4j.Logger; |
| 34 | +import org.slf4j.LoggerFactory; |
| 35 | +import org.springframework.ide.vscode.boot.java.Annotations; |
| 36 | +import org.springframework.ide.vscode.boot.java.annotations.AnnotationHierarchies; |
| 37 | +import org.springframework.ide.vscode.boot.java.handlers.SymbolProvider; |
| 38 | +import org.springframework.ide.vscode.boot.java.utils.ASTUtils; |
| 39 | +import org.springframework.ide.vscode.boot.java.utils.CachedSymbol; |
| 40 | +import org.springframework.ide.vscode.boot.java.utils.SpringIndexerJavaContext; |
| 41 | +import org.springframework.ide.vscode.commons.protocol.spring.AnnotationMetadata; |
| 42 | +import org.springframework.ide.vscode.commons.protocol.spring.Bean; |
| 43 | +import org.springframework.ide.vscode.commons.protocol.spring.InjectionPoint; |
| 44 | +import org.springframework.ide.vscode.commons.util.BadLocationException; |
| 45 | +import org.springframework.ide.vscode.commons.util.text.DocumentRegion; |
| 46 | +import org.springframework.ide.vscode.commons.util.text.TextDocument; |
| 47 | + |
| 48 | +/** |
| 49 | + * @author Martin Lippert |
| 50 | + */ |
| 51 | +public class ConfigurationPropertiesSymbolProvider implements SymbolProvider { |
| 52 | + |
| 53 | + private static final Logger log = LoggerFactory.getLogger(ConfigurationPropertiesSymbolProvider.class); |
| 54 | + |
| 55 | + @Override |
| 56 | + public void addSymbols(Annotation node, ITypeBinding annotationType, Collection<ITypeBinding> metaAnnotations, SpringIndexerJavaContext context, TextDocument doc) { |
| 57 | + try { |
| 58 | + if (node != null && node.getParent() != null && node.getParent() instanceof TypeDeclaration) { |
| 59 | + createSymbol(node, annotationType, metaAnnotations, context, doc); |
| 60 | + } |
| 61 | + } |
| 62 | + catch (BadLocationException e) { |
| 63 | + log.error("", e); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + protected void createSymbol(Annotation node, ITypeBinding annotationType, Collection<ITypeBinding> metaAnnotations, SpringIndexerJavaContext context, TextDocument doc) throws BadLocationException { |
| 68 | + String annotationTypeName = annotationType.getName(); |
| 69 | + |
| 70 | + Collection<String> metaAnnotationNames = metaAnnotations.stream() |
| 71 | + .map(ITypeBinding::getName) |
| 72 | + .collect(Collectors.toList()); |
| 73 | + |
| 74 | + TypeDeclaration type = (TypeDeclaration) node.getParent(); |
| 75 | + ITypeBinding typeBinding = type.resolveBinding(); |
| 76 | + |
| 77 | + AnnotationHierarchies annotationHierarchies = AnnotationHierarchies.get(type); |
| 78 | + boolean isComponentAnnotated = annotationHierarchies.isAnnotatedWith(typeBinding, Annotations.COMPONENT); |
| 79 | + |
| 80 | + if (!isComponentAnnotated) { |
| 81 | + String beanName = BeanUtils.getBeanNameFromType(type.getName().getFullyQualifiedName()); |
| 82 | + ITypeBinding beanType = type.resolveBinding(); |
| 83 | + |
| 84 | + Location location = new Location(doc.getUri(), doc.toRange(type.getStartPosition(), type.getLength())); |
| 85 | + |
| 86 | + WorkspaceSymbol symbol = new WorkspaceSymbol( |
| 87 | + ComponentSymbolProvider.beanLabel("+", annotationTypeName, metaAnnotationNames, beanName, beanType.getName()), SymbolKind.Interface, |
| 88 | + Either.forLeft(location)); |
| 89 | + |
| 90 | + boolean isConfiguration = false; // otherwise, the ComponentSymbolProvider takes care of the bean definiton for this type |
| 91 | + |
| 92 | + InjectionPoint[] injectionPoints = ASTUtils.findInjectionPoints(type, doc); |
| 93 | + |
| 94 | + Set<String> supertypes = new HashSet<>(); |
| 95 | + ASTUtils.findSupertypes(beanType, supertypes); |
| 96 | + |
| 97 | + Collection<Annotation> annotationsOnType = ASTUtils.getAnnotations(type); |
| 98 | + |
| 99 | + AnnotationMetadata[] annotations = Stream.concat( |
| 100 | + Arrays.stream(ASTUtils.getAnnotationsMetadata(annotationsOnType, doc)) |
| 101 | + , |
| 102 | + metaAnnotations.stream() |
| 103 | + .map(an -> new AnnotationMetadata(an.getQualifiedName(), true, null, null))) |
| 104 | + .toArray(AnnotationMetadata[]::new); |
| 105 | + |
| 106 | + Bean beanDefinition = new Bean(beanName, beanType.getQualifiedName(), location, injectionPoints, supertypes, annotations, isConfiguration, symbol.getName()); |
| 107 | + |
| 108 | + indexConfigurationProperties(beanDefinition, type, context, doc); |
| 109 | + |
| 110 | + context.getGeneratedSymbols().add(new CachedSymbol(context.getDocURI(), context.getLastModified(), symbol)); |
| 111 | + context.getBeans().add(new CachedBean(context.getDocURI(), beanDefinition)); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public static void indexConfigurationProperties(Bean beanDefinition, TypeDeclaration type, SpringIndexerJavaContext context, TextDocument doc) { |
| 116 | + |
| 117 | + FieldDeclaration[] fields = type.getFields(); |
| 118 | + if (fields != null) { |
| 119 | + for (FieldDeclaration field : fields) { |
| 120 | + try { |
| 121 | + Type fieldType = field.getType(); |
| 122 | + if (fieldType != null) { |
| 123 | + |
| 124 | + @SuppressWarnings("unchecked") |
| 125 | + List<VariableDeclarationFragment> fragments = field.fragments(); |
| 126 | + |
| 127 | + for (VariableDeclarationFragment fragment : fragments) { |
| 128 | + SimpleName name = fragment.getName(); |
| 129 | + |
| 130 | + if (name != null) { |
| 131 | + |
| 132 | + DocumentRegion nodeRegion = ASTUtils.nodeRegion(doc, field); |
| 133 | + Range range = doc.toRange(nodeRegion); |
| 134 | + ConfigPropertyIndexElement configPropElement = new ConfigPropertyIndexElement(name.getFullyQualifiedName(), fieldType.resolveBinding().getQualifiedName(), range); |
| 135 | + |
| 136 | + beanDefinition.addChild(configPropElement); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } catch (BadLocationException e) { |
| 141 | + log.error("error identifying config property field", e); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments