Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for injection Annotations #198

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class Annotations {

public static final String AUTOWIRED = "org.springframework.beans.factory.annotation.Autowired";
public static final String INJECT = "javax.inject.Inject";
public static final String RESOURCE = "javax.annotation.Resource";

public static final String QUALIFIER = "org.springframework.beans.factory.annotation.Qualifier";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.springframework.ide.vscode.boot.java.handlers.ReferenceProvider;
import org.springframework.ide.vscode.boot.java.handlers.RunningAppProvider;
import org.springframework.ide.vscode.boot.java.handlers.SymbolProvider;
import org.springframework.ide.vscode.boot.java.injection.InjectionSymbolProvider;
import org.springframework.ide.vscode.boot.java.links.SourceLinks;
import org.springframework.ide.vscode.boot.java.livehover.ActiveProfilesProvider;
import org.springframework.ide.vscode.boot.java.livehover.BeanInjectedIntoHoverProvider;
Expand Down Expand Up @@ -384,6 +385,7 @@ protected SpringSymbolIndex createAnnotationIndexer(SimpleLanguageServer server,
RestrictedDefaultSymbolProvider restrictedDefaultSymbolProvider = new RestrictedDefaultSymbolProvider();
DataRepositorySymbolProvider dataRepositorySymbolProvider = new DataRepositorySymbolProvider();
WebfluxRouterSymbolProvider webfluxRouterSymbolProvider = new WebfluxRouterSymbolProvider();
InjectionSymbolProvider injectionSymbolProvider = new InjectionSymbolProvider();

providers.put(Annotations.SPRING_REQUEST_MAPPING, requestMappingSymbolProvider);
providers.put(Annotations.SPRING_GET_MAPPING, requestMappingSymbolProvider);
Expand Down Expand Up @@ -419,6 +421,10 @@ protected SpringSymbolIndex createAnnotationIndexer(SimpleLanguageServer server,
providers.put(Annotations.REPOSITORY, dataRepositorySymbolProvider);
providers.put("", webfluxRouterSymbolProvider);

providers.put(Annotations.RESOURCE, injectionSymbolProvider);
providers.put(Annotations.INJECT, injectionSymbolProvider);
providers.put(Annotations.AUTOWIRED, injectionSymbolProvider);

return new SpringSymbolIndex(server, params, providers);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*******************************************************************************
* Copyright (c) 2019 Pivotal, Inc.
* 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
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Gayan Perera <gayanper@gmail.com> - Symbol provider for injectable annotations
*******************************************************************************/
package org.springframework.ide.vscode.boot.java.injection;

import java.util.Collection;
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.FieldDeclaration;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.SymbolInformation;
import org.eclipse.lsp4j.SymbolKind;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ide.vscode.boot.java.handlers.EnhancedSymbolInformation;
import org.springframework.ide.vscode.boot.java.handlers.SymbolProvider;
import org.springframework.ide.vscode.commons.util.BadLocationException;
import org.springframework.ide.vscode.commons.util.text.TextDocument;

import com.google.common.collect.ImmutableList;

public class InjectionSymbolProvider implements SymbolProvider {

private static final Logger LOGGER = LoggerFactory.getLogger(InjectionSymbolProvider.class);

@Override
public Collection<EnhancedSymbolInformation> getSymbols(Annotation node, ITypeBinding typeBinding,
Collection<ITypeBinding> metaAnnotations, TextDocument doc) {
if (node.getParent() != null) {
try {
switch (node.getParent().getNodeType()) {
case ASTNode.FIELD_DECLARATION: {
return createFieldSymbol(node, typeBinding, metaAnnotations, doc);
}
case ASTNode.METHOD_DECLARATION: {
break;
}
}
} catch (BadLocationException e) {
LOGGER.error(e.getMessage(), e);
}
}
return null;
}

private Collection<EnhancedSymbolInformation> createFieldSymbol(Annotation node, ITypeBinding annotationType,
Collection<ITypeBinding> metaAnnotations, TextDocument doc) throws BadLocationException {
String annotationString = node.toString();

FieldDeclaration fieldDeclaration = (FieldDeclaration) node.getParent();
@SuppressWarnings("unchecked")
List<VariableDeclarationFragment> fragments = fieldDeclaration.fragments();
com.google.common.collect.ImmutableList.Builder<EnhancedSymbolInformation> builder = ImmutableList.builder();

for (VariableDeclarationFragment fragment : fragments) {
SymbolInformation symbol = new SymbolInformation(label(fragment.getName().getIdentifier(), annotationString,
fieldDeclaration.getType().resolveBinding().getName(), getAnnotations(node, annotationType)),
SymbolKind.Interface,
new Location(doc.getUri(), doc.toRange(node.getStartPosition(), node.getLength())));
builder.add(new EnhancedSymbolInformation(symbol, null));
}
return builder.build();
}

private String label(String target, String annotationString, String fieldTypeName, String qualifierString) {
StringBuilder symbolLabel = new StringBuilder();
symbolLabel.append('@');
symbolLabel.append('=');
symbolLabel.append(' ');
symbolLabel.append('\'');
symbolLabel.append(target);
symbolLabel.append('\'').append(' ');
symbolLabel.append('(');
symbolLabel.append(annotationString);
if(!qualifierString.isEmpty()) {
symbolLabel.append(' ');
symbolLabel.append(qualifierString);
}
symbolLabel.append(')').append(' ');
symbolLabel.append(fieldTypeName);
return symbolLabel.toString();
}

private String getAnnotations(Annotation node, ITypeBinding annotationTypeBinding) {
StringBuilder result = new StringBuilder();
ASTNode parent = node.getParent();
String annotationName = annotationTypeBinding.getBinaryName();

List<Annotation> annotations = Collections.emptyList();
if (parent instanceof MethodDeclaration) {
annotations = annotationsFromMethod((MethodDeclaration) parent, annotationName);
} else if (parent instanceof FieldDeclaration) {
annotations = annotationsFromField((FieldDeclaration) parent, annotationName);
}

annotations.forEach(a -> {
result.append(' ');
result.append(a.toString());
});
return result.toString();
}

private List<Annotation> annotationsFromField(FieldDeclaration declaration, String injectAnnotation) {
List<?> modifiers = declaration.modifiers();
return extractAnnotations(injectAnnotation, modifiers);
}

private List<Annotation> annotationsFromMethod(MethodDeclaration declaration, String injectAnnotation) {
List<?> modifiers = declaration.modifiers();
return extractAnnotations(injectAnnotation, modifiers);
}

private List<Annotation> extractAnnotations(String injectAnnotation, List<?> modifiers) {
return modifiers.stream().filter(m -> m instanceof Annotation).map(m -> (Annotation) m).filter(m -> {
String type = m.resolveAnnotationBinding().getAnnotationType().getBinaryName();
return (type != null && !injectAnnotation.equals(type));
}).collect(Collectors.toList());
}

@Override
public Collection<EnhancedSymbolInformation> getSymbols(TypeDeclaration typeDeclaration, TextDocument doc) {
return null;
}

@Override
public Collection<EnhancedSymbolInformation> getSymbols(MethodDeclaration methodDeclaration, TextDocument doc) {
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Annotation;
import org.eclipse.jdt.core.dom.ArrayInitializer;
import org.eclipse.jdt.core.dom.BooleanLiteral;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.MemberValuePair;
Expand Down Expand Up @@ -176,9 +177,17 @@ public static String getLiteralValue(StringLiteral node) {
}
}

public static String getLiteralValue(BooleanLiteral node) {
synchronized (node.getAST()) {
return Boolean.toString(node.booleanValue());
}
}

public static String getExpressionValueAsString(Expression exp) {
if (exp instanceof StringLiteral) {
return getLiteralValue((StringLiteral) exp);
} else if (exp instanceof BooleanLiteral) {
return getLiteralValue((BooleanLiteral) exp);
} else if (exp instanceof QualifiedName) {
return getExpressionValueAsString(((QualifiedName) exp).getName());
} else if (exp instanceof SimpleName) {
Expand Down