fixed issue with live hover to autowired showing up even without the dependencies being wired successfully

This commit is contained in:
Martin Lippert
2017-11-01 12:06:06 +01:00
parent 71cbda64dd
commit 86f9348775
4 changed files with 94 additions and 63 deletions

View File

@@ -28,7 +28,6 @@ import org.springframework.ide.vscode.boot.java.utils.ASTUtils;
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.util.BadLocationException;
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,18 +42,25 @@ public class AutowiredHoverProvider implements HoverProvider {
@Override
public Collection<Range> getLiveHoverHints(Annotation annotation, TextDocument doc, SpringBootApp[] runningApps) {
try {
for (SpringBootApp bootApp : runningApps) {
try {
LiveBeansModel liveBeans = bootApp.getBeans();
if (liveBeans != null && !liveBeans.isEmpty()) {
Range range = getLiveHoverHint(annotation, doc, liveBeans);
if (range != null) {
return ImmutableList.of(range);
LiveBean definedBean = getDefinedBean(annotation);
if (definedBean != null) {
for (SpringBootApp app : runningApps) {
try {
List<LiveBean> relevantBeans = LiveHoverUtils.findRelevantBeans(app, definedBean).collect(Collectors.toList());
if (!relevantBeans.isEmpty()) {
for (LiveBean bean : relevantBeans) {
String[] dependencies = bean.getDependencies();
if (dependencies != null && dependencies.length > 0) {
Range hoverRange = doc.toRange(annotation.getStartPosition(), annotation.getLength());
return ImmutableList.of(hoverRange);
}
}
}
}
}
catch (Exception e) {
Log.log(e);
catch (Exception e) {
Log.log(e);
}
}
}
}
@@ -65,27 +71,6 @@ public class AutowiredHoverProvider implements HoverProvider {
return null;
}
public Range getLiveHoverHint(Annotation annotation, TextDocument doc, LiveBeansModel beansModel) {
try {
TypeDeclaration declaringType = ASTUtils.findDeclaringType(annotation);
if (declaringType != null) {
String type = declaringType.resolveBinding().getQualifiedName();
if (type != null && beansModel != null) {
List<LiveBean> beansOfType = beansModel.getBeansOfType(type);
if (!beansOfType.isEmpty()) {
Range hoverRange = doc.toRange(annotation.getStartPosition(), annotation.getLength());
return hoverRange;
}
}
}
}
catch (BadLocationException e) {
Log.log(e);
}
return null;
}
@Override
public CompletableFuture<Hover> provideHover(ASTNode node, Annotation annotation, ITypeBinding type, int offset,
TextDocument doc, SpringBootApp[] runningApps) {
@@ -99,6 +84,8 @@ public class AutowiredHoverProvider implements HoverProvider {
hover.append("**Injection report for " + LiveHoverUtils.showBean(definedBean) + "**\n\n");
boolean hasInterestingApp = false;
boolean hasAutowiring = false;
for (SpringBootApp app : runningApps) {
LiveBeansModel beans = app.getBeans();
List<LiveBean> relevantBeans = LiveHoverUtils.findRelevantBeans(app, definedBean).collect(Collectors.toList());
@@ -113,11 +100,11 @@ public class AutowiredHoverProvider implements HoverProvider {
for (LiveBean bean : relevantBeans) {
hover.append("\n\n");
addAutomaticallyWired(hover, annotation, beans, bean);
hasAutowiring |= addAutomaticallyWired(hover, annotation, beans, bean);
}
}
}
if (hasInterestingApp) {
if (hasInterestingApp && hasAutowiring) {
System.out.println(hover);
return CompletableFuture
.completedFuture(new Hover(ImmutableList.of(Either.forLeft(hover.toString()))));
@@ -132,7 +119,7 @@ public class AutowiredHoverProvider implements HoverProvider {
if (declaringType != null) {
ITypeBinding beanType = declaringType.resolveBinding();
if (beanType != null) {
String id = getBeanId(annotation, beanType);
String id = getBeanId(declaringType, beanType);
if (StringUtil.hasText(id)) {
return LiveBean.builder().id(id).type(beanType.getQualifiedName()).build();
}
@@ -141,38 +128,36 @@ public class AutowiredHoverProvider implements HoverProvider {
return null;
}
private String getBeanId(Annotation annotation, ITypeBinding beanType) {
return ASTUtils.getAttribute(annotation, "value").flatMap(ASTUtils::getFirstString)
.orElseGet(() -> {
String typeName = beanType.getName();
if (StringUtil.hasText(typeName)) {
return Character.toLowerCase(typeName.charAt(0)) + typeName.substring(1);
}
return null;
});
private String getBeanId(TypeDeclaration declaringType, ITypeBinding beanType) {
// TODO: take specific bean declarations into account like @Component at declaring type
String typeName = beanType.getName();
if (StringUtil.hasText(typeName)) {
return Character.toLowerCase(typeName.charAt(0)) + typeName.substring(1);
}
return null;
}
private void addAutomaticallyWired(StringBuilder hover, Annotation annotation, LiveBeansModel beans, LiveBean bean) {
TypeDeclaration typeDecl = ASTUtils.findDeclaringType(annotation);
if (typeDecl != null) {
String[] dependencies = bean.getDependencies();
private boolean addAutomaticallyWired(StringBuilder hover, Annotation annotation, LiveBeansModel beans, LiveBean bean) {
boolean result = false;
String[] dependencies = bean.getDependencies();
if (dependencies != null && dependencies.length > 0) {
hover.append(LiveHoverUtils.showBean(bean) + " got autowired with:\n\n");
if (dependencies != null && dependencies.length > 0) {
result = true;
hover.append(LiveHoverUtils.showBean(bean) + " got autowired with:\n\n");
boolean firstDependency = true;
for (String injectedBean : dependencies) {
if (!firstDependency) {
hover.append("\n");
}
List<LiveBean> dependencyBeans = beans.getBeansOfName(injectedBean);
for (LiveBean dependencyBean : dependencyBeans) {
hover.append("- " + LiveHoverUtils.showBean(dependencyBean));
}
firstDependency = false;
boolean firstDependency = true;
for (String injectedBean : dependencies) {
if (!firstDependency) {
hover.append("\n");
}
List<LiveBean> dependencyBeans = beans.getBeansOfName(injectedBean);
for (LiveBean dependencyBean : dependencyBeans) {
hover.append("- " + LiveHoverUtils.showBean(dependencyBean));
}
firstDependency = false;
}
}
return result;
}
}

View File

@@ -25,7 +25,8 @@ import org.springframework.ide.vscode.commons.util.StringUtil;
public class ComponentInjectionsHoverProvider extends AbstractInjectedIntoHoverProvider {
@Override protected void addAutomaticallyWiredContructor(StringBuilder hover, Annotation annotation, LiveBeansModel beans, LiveBean bean) {
@Override
protected void addAutomaticallyWiredContructor(StringBuilder hover, Annotation annotation, LiveBeansModel beans, LiveBean bean) {
TypeDeclaration typeDecl = ASTUtils.findDeclaringType(annotation);
if (typeDecl != null) {
MethodDeclaration[] constructors = ASTUtils.findConstructors(typeDecl);

View File

@@ -38,10 +38,10 @@ public class LiveHoverUtils {
public static Stream<LiveBean> findRelevantBeans(SpringBootApp app, LiveBean definedBean) {
LiveBeansModel beansModel = app.getBeans();
if (beansModel!=null) {
if (beansModel != null) {
Stream<LiveBean> relevantBeans = beansModel.getBeansOfName(definedBean.getId()).stream();
String type = definedBean.getType();
if (type!=null) {
if (type != null) {
relevantBeans = relevantBeans.filter(bean -> type.equals(bean.getType()));
}
return relevantBeans;

View File

@@ -194,4 +194,49 @@ public class AutowiredHoverProviderTest {
editor.assertNoHover("@Autowired");
}
@Test
public void noHoversWhenRunningAppDoesntHaveDependenciesForTheAutowiring() throws Exception {
LiveBeansModel beans = LiveBeansModel.builder()
.add(LiveBean.builder()
.id("autowiredClass")
.type("com.example.AutowiredClass")
.build()
)
.add(LiveBean.builder()
.id("dependencyA")
.type("com.example.DependencyA")
.build()
)
.add(LiveBean.builder()
.id("dependencyB")
.type("com.example.DependencyB")
.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 org.springframework.beans.factory.annotation.Autowired;\n" +
"import org.springframework.stereotype.Component;\n" +
"\n" +
"@Component\n" +
"public class AutowiredClass {\n" +
"\n" +
" @Autowired\n" +
" public AutowiredClass(DependencyA depA, DependencyB depB) {\n" +
" }\n" +
"}\n"
);
editor.assertHighlights("@Component");
editor.assertNoHover("@Autowired");
}
}