Treat @Inject as synonymous to @Autowired

This commit is contained in:
Kris De Volder
2018-07-06 15:24:10 -07:00
parent f0a306ac4d
commit 1383fff41d
6 changed files with 76 additions and 17 deletions

View File

@@ -19,11 +19,12 @@ public class Annotations {
public static final String BEAN = "org.springframework.context.annotation.Bean";
public static final String PROFILE = "org.springframework.context.annotation.Profile";
public static final String CONDITIONAL = "org.springframework.context.annotation.Conditional";
public static final String COMPONENT = "org.springframework.stereotype.Component";
public static final String REPOSITORY = "org.springframework.stereotype.Repository";
public static final String AUTOWIRED = "org.springframework.beans.factory.annotation.Autowired";
public static final String INJECT = "javax.inject.Inject";
public static final String SPRING_REQUEST_MAPPING = "org.springframework.web.bind.annotation.RequestMapping";
public static final String SPRING_GET_MAPPING = "org.springframework.web.bind.annotation.GetMapping";

View File

@@ -301,6 +301,7 @@ public class BootJavaLanguageServerComponents implements LanguageServerComponent
providers.put(Annotations.PROFILE, new ActiveProfilesProvider());
providers.put(Annotations.AUTOWIRED, new AutowiredHoverProvider(this));
providers.put(Annotations.INJECT, new AutowiredHoverProvider(this));
providers.put(Annotations.COMPONENT, new ComponentInjectionsHoverProvider(this));
providers.put(Annotations.BEAN, new BeanInjectedIntoHoverProvider(this));

View File

@@ -21,6 +21,8 @@ import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.lsp4j.Hover;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ide.vscode.boot.java.Annotations;
import org.springframework.ide.vscode.boot.java.BootJavaLanguageServerComponents;
import org.springframework.ide.vscode.boot.java.annotations.AnnotationHierarchies;
@@ -32,8 +34,6 @@ import org.springframework.ide.vscode.commons.boot.app.cli.SpringBootApp;
import org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBean;
import org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBeansModel;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
import org.springframework.ide.vscode.commons.util.Log;
import org.springframework.ide.vscode.commons.util.StringUtil;
import org.springframework.ide.vscode.commons.util.text.TextDocument;
@@ -43,9 +43,11 @@ import com.google.common.collect.ImmutableList;
* @author Martin Lippert
*/
public class AutowiredHoverProvider implements HoverProvider {
final static Logger log = LoggerFactory.getLogger(AutowiredHoverProvider.class);
private BootJavaLanguageServerComponents server;
public AutowiredHoverProvider(BootJavaLanguageServerComponents server) {
this.server = server;
}
@@ -70,13 +72,13 @@ public class AutowiredHoverProvider implements HoverProvider {
}
}
catch (Exception e) {
Log.log(e);
log.error("", e);
}
}
}
}
catch (Exception e) {
Log.log(e);
log.error("", e);
}
return null;
@@ -178,8 +180,7 @@ public class AutowiredHoverProvider implements HoverProvider {
}
@Override
public Collection<Range> getLiveHoverHints(TypeDeclaration typeDeclaration, TextDocument doc,
SpringBootApp[] runningApps) {
public Collection<Range> getLiveHoverHints(TypeDeclaration typeDeclaration, TextDocument doc, SpringBootApp[] runningApps) {
return null;
}

View File

@@ -80,8 +80,9 @@ public class ComponentInjectionsHoverProvider extends AbstractInjectedIntoHoverP
for (Object modifier : modifiers) {
if (modifier instanceof MarkerAnnotation) {
ITypeBinding typeBinding = ((MarkerAnnotation) modifier).resolveTypeBinding();
if (typeBinding != null && typeBinding.getQualifiedName().equals(Annotations.AUTOWIRED)) {
return true;
if (typeBinding != null) {
String fqName = typeBinding.getQualifiedName();
return Annotations.AUTOWIRED.equals(fqName) || Annotations.INJECT.equals(fqName);
}
}
}
@@ -198,7 +199,7 @@ public class ComponentInjectionsHoverProvider extends AbstractInjectedIntoHoverP
}
return false;
}
private boolean isComponentAnnotation(ITypeBinding type) {
Set<String> transitiveSuperAnnotations = AnnotationHierarchies.getTransitiveSuperAnnotations(type);
for (String annotationType : transitiveSuperAnnotations) {
@@ -206,7 +207,7 @@ public class ComponentInjectionsHoverProvider extends AbstractInjectedIntoHoverP
return true;
}
}
return false;
}

View File

@@ -55,7 +55,6 @@ public class AutowiredHoverProviderTest {
"public class DependencyB {\n" +
"}\n"
);
};
private BootJavaLanguageServerHarness harness;
@@ -78,6 +77,57 @@ public class AutowiredHoverProviderTest {
harness.intialize(null);
}
@Test
public void javaxInjectAnnotationHover() throws Exception {
LiveBeansModel beans = LiveBeansModel.builder()
.add(LiveBean.builder()
.id("autowiredClass")
.type("com.example.AutowiredClass")
.dependencies("dependencyA")
.build()
)
.add(LiveBean.builder()
.id("dependencyA")
.type("com.example.DependencyA")
.fileResource(harness.getOutputFolder() + Paths.get("/com/example/DependencyA.class").toString())
.build()
)
.build();
mockAppProvider.builder()
.isSpringBootApp(true)
.processId("111")
.processName("the-app")
.beans(beans)
.build();
Editor editor = harness.newEditor(LanguageId.JAVA,
"package com.example;\n" +
"\n" +
"import javax.inject.Inject;\n" +
"import org.springframework.stereotype.Component;\n" +
"\n" +
"@Component\n" +
"public class AutowiredClass {\n" +
"\n" +
" @Inject\n" +
" private DependencyA depA;\n" +
"}\n"
);
editor.assertHighlights("@Component", "@Inject");
editor.assertTrimmedHover("@Inject",
"**Injection report for Bean [id: autowiredClass, type: `com.example.AutowiredClass`]**\n" +
"\n" +
"Process [PID=111, name=`the-app`]:\n" +
"\n" +
"Bean [id: autowiredClass, type: `com.example.AutowiredClass`] got autowired with:\n" +
"\n" +
"- Bean: dependencyA \n" +
" Type: `com.example.DependencyA` \n" +
" Resource: `" + Paths.get("com/example/DependencyA.class") + "`"
);
}
@Test
public void componentWithAutomaticallyWiredConstructorInjections() throws Exception {
LiveBeansModel beans = LiveBeansModel.builder()

View File

@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
@@ -15,7 +16,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
@@ -33,7 +34,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>