Skip to content

Commit

Permalink
GH-1400: include more precise location information for annotations an…
Browse files Browse the repository at this point in the history
…d annotation attribute values in spring index
  • Loading branch information
martinlippert committed Nov 12, 2024
1 parent 8de5ea4 commit e52ae18
Show file tree
Hide file tree
Showing 20 changed files with 370 additions and 105 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*******************************************************************************
* 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.commons.protocol.spring;

import java.util.Objects;

import org.eclipse.lsp4j.Location;

/**
* @author Martin Lippert
*/
public class AnnotationAttributeValue {

private final String name;
private final Location location;

public AnnotationAttributeValue(String name, Location location) {
this.name = name;
this.location = location;
}

public String getName() {
return name;
}

public Location getLocation() {
return location;
}

@Override
public int hashCode() {
return Objects.hash(location, name);
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AnnotationAttributeValue other = (AnnotationAttributeValue) obj;
return Objects.equals(location, other.location) && Objects.equals(name, other.name);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@

import java.util.Map;

import org.eclipse.lsp4j.Location;

/**
* @author Martin Lippert
*/
public class AnnotationMetadata {

private final String annotationType;
private final boolean isMetaAnnotation;
private final Map<String, String[]> attributes;
private final Location location;
private final Map<String, AnnotationAttributeValue[]> attributes;

public AnnotationMetadata(String annotationType, boolean isMetaAnnotation, Map<String, String[]> attributes) {
public AnnotationMetadata(String annotationType, boolean isMetaAnnotation, Location location, Map<String, AnnotationAttributeValue[]> attributes) {
this.annotationType = annotationType;
this.isMetaAnnotation = isMetaAnnotation;
this.location = location;
this.attributes = attributes;
}

Expand All @@ -34,9 +38,13 @@ public String getAnnotationType() {
public boolean isMetaAnnotation() {
return isMetaAnnotation;
}

public Location getLocation() {
return location;
}

public Map<String, String[]> getAttributes() {
public Map<String, AnnotationAttributeValue[]> getAttributes() {
return attributes;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,8 @@ protected void addSymbolsPass1(Annotation node, ITypeBinding annotationType, Col
ASTUtils.findSupertypes(beanType, supertypes);

Collection<Annotation> annotationsOnMethod = ASTUtils.getAnnotations(method);
AnnotationMetadata[] annotations = annotationsOnMethod.stream()
.map(an -> an.resolveAnnotationBinding())
.map(t -> new AnnotationMetadata(t.getAnnotationType().getQualifiedName(), false, ASTUtils.getAttributes(t)))
.toArray(AnnotationMetadata[]::new);
AnnotationMetadata[] annotations = ASTUtils.getAnnotationsMetadata(annotationsOnMethod, doc);

// AnnotationMetadata[] annotations = AnnotationHierarchies
// .findTransitiveSuperAnnotationBindings(node.resolveAnnotationBinding())
// .map(t -> new AnnotationMetadata(t.getAnnotationType().getQualifiedName(), false, getAttributes(t)))
// .toArray(AnnotationMetadata[]::new);

Bean beanDefinition = new Bean(nameAndRegion.getT1(), beanType.getQualifiedName(), location, injectionPoints, supertypes, annotations);

context.getGeneratedSymbols().add(new CachedSymbol(context.getDocURI(), context.getLastModified(), enhancedSymbol));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*******************************************************************************/
package org.springframework.ide.vscode.boot.java.beans;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -105,12 +106,10 @@ protected Tuple.Two<EnhancedSymbolInformation, Bean> createSymbol(Annotation nod
Collection<Annotation> annotationsOnType = ASTUtils.getAnnotations(type);

AnnotationMetadata[] annotations = Stream.concat(
annotationsOnType.stream()
.map(an -> an.resolveAnnotationBinding())
.map(t -> new AnnotationMetadata(t.getAnnotationType().getQualifiedName(), false, ASTUtils.getAttributes(t)))
Arrays.stream(ASTUtils.getAnnotationsMetadata(annotationsOnType, doc))
,
metaAnnotations.stream()
.map(an -> new AnnotationMetadata(an.getQualifiedName(), true, null)))
.map(an -> new AnnotationMetadata(an.getQualifiedName(), true, null, null)))
.toArray(AnnotationMetadata[]::new);

Bean beanDefinition = new Bean(beanName, beanType.getQualifiedName(), location, injectionPoints, supertypes, annotations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*******************************************************************************/
package org.springframework.ide.vscode.boot.java.beans;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -95,12 +96,10 @@ private Two<EnhancedSymbolInformation, Bean> createSymbol(Annotation node, IType
Collection<Annotation> annotationsOnType = ASTUtils.getAnnotations(type);

AnnotationMetadata[] annotations = Stream.concat(
annotationsOnType.stream()
.map(an -> an.resolveAnnotationBinding())
.map(t -> new AnnotationMetadata(t.getAnnotationType().getQualifiedName(), false, ASTUtils.getAttributes(t)))
Arrays.stream(ASTUtils.getAnnotationsMetadata(annotationsOnType, doc))
,
metaAnnotations.stream()
.map(an -> new AnnotationMetadata(an.getQualifiedName(), true, null)))
.map(an -> new AnnotationMetadata(an.getQualifiedName(), true, null, null)))
.toArray(AnnotationMetadata[]::new);

Bean beanDefinition = new Bean(beanName, beanType == null ? "" : beanType.getQualifiedName(), location, injectionPoints, supertypes, annotations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private Stream<String> findAllNamedValues(Bean[] beans) {
.flatMap(bean -> Arrays.stream(bean.getAnnotations()))
.filter(annotation -> Annotations.NAMED_ANNOTATIONS.contains(annotation.getAnnotationType()))
.filter(annotation -> annotation.getAttributes() != null && annotation.getAttributes().containsKey("value") && annotation.getAttributes().get("value").length == 1)
.map(annotation -> annotation.getAttributes().get("value")[0]);
.map(annotation -> annotation.getAttributes().get("value")[0].getName());

Stream<String> qualifiersFromInjectionPoints = Arrays.stream(beans)
// annotations from beans themselves
Expand All @@ -64,7 +64,7 @@ private Stream<String> findAllNamedValues(Bean[] beans) {
.flatMap(injectionPoint -> Arrays.stream(injectionPoint.getAnnotations()))
.filter(annotation -> Annotations.NAMED_ANNOTATIONS.contains(annotation.getAnnotationType()))
.filter(annotation -> annotation.getAttributes() != null && annotation.getAttributes().containsKey("value") && annotation.getAttributes().get("value").length == 1)
.map(annotation -> annotation.getAttributes().get("value")[0]);
.map(annotation -> annotation.getAttributes().get("value")[0].getName());

return Stream.concat(qualifiersFromBeans, qualifiersFromInjectionPoints);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ private Stream<String> findAllProfiles(Bean[] beans) {
.flatMap(bean -> Arrays.stream(bean.getAnnotations()))
.filter(annotation -> Annotations.PROFILE.equals(annotation.getAnnotationType()))
.filter(annotation -> annotation.getAttributes() != null && annotation.getAttributes().containsKey("value"))
.flatMap(annotation -> Arrays.stream(annotation.getAttributes().get("value")));
.flatMap(annotation -> Arrays.stream(annotation.getAttributes().get("value")))
.map(attributeValue -> attributeValue.getName());

Stream<String> profilesFromInjectionPoints = Arrays.stream(beans)
// annotations from beans themselves
Expand All @@ -62,7 +63,8 @@ private Stream<String> findAllProfiles(Bean[] beans) {
.flatMap(injectionPoint -> Arrays.stream(injectionPoint.getAnnotations()))
.filter(annotation -> Annotations.PROFILE.equals(annotation.getAnnotationType()))
.filter(annotation -> annotation.getAttributes() != null && annotation.getAttributes().containsKey("value"))
.flatMap(annotation -> Arrays.stream(annotation.getAttributes().get("value")));
.flatMap(annotation -> Arrays.stream(annotation.getAttributes().get("value")))
.map(attributeValue -> attributeValue.getName());

return Stream.concat(profilesFromBeans, profilesFromInjectionPoints);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private Stream<String> findAllQualifiers(Bean[] beans) {
.flatMap(bean -> Arrays.stream(bean.getAnnotations()))
.filter(annotation -> Annotations.QUALIFIER.equals(annotation.getAnnotationType()))
.filter(annotation -> annotation.getAttributes() != null && annotation.getAttributes().containsKey("value") && annotation.getAttributes().get("value").length == 1)
.map(annotation -> annotation.getAttributes().get("value")[0]);
.map(annotation -> annotation.getAttributes().get("value")[0].getName());

Stream<String> qualifiersFromInjectionPoints = Arrays.stream(beans)
// annotations from beans themselves
Expand All @@ -74,7 +74,7 @@ private Stream<String> findAllQualifiers(Bean[] beans) {
.flatMap(injectionPoint -> Arrays.stream(injectionPoint.getAnnotations()))
.filter(annotation -> Annotations.QUALIFIER.equals(annotation.getAnnotationType()))
.filter(annotation -> annotation.getAttributes() != null && annotation.getAttributes().containsKey("value") && annotation.getAttributes().get("value").length == 1)
.map(annotation -> annotation.getAttributes().get("value")[0]);
.map(annotation -> annotation.getAttributes().get("value")[0].getName());

return Stream.concat(qualifiersFromBeans, qualifiersFromInjectionPoints);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ protected void addSymbolsPass1(TypeDeclaration typeDeclaration, SpringIndexerJav
String concreteRepoType = concreteBeanTypeBindung.getQualifiedName();

Collection<Annotation> annotationsOnMethod = ASTUtils.getAnnotations(typeDeclaration);
AnnotationMetadata[] annotations = annotationsOnMethod.stream()
.map(an -> an.resolveAnnotationBinding())
.map(t -> new AnnotationMetadata(t.getAnnotationType().getQualifiedName(), false, ASTUtils.getAttributes(t)))
.toArray(AnnotationMetadata[]::new);
AnnotationMetadata[] annotations = ASTUtils.getAnnotationsMetadata(annotationsOnMethod, doc);

Bean beanDefinition = new Bean(beanName, concreteRepoType, location, injectionPoints, supertypes, annotations);

Expand Down
Loading

0 comments on commit e52ae18

Please sign in to comment.