Search in sources :

Example 6 with LiveBean

use of org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBean in project sts4 by spring-projects.

the class AutowiredHoverProvider method provideHover.

@Override
public Hover provideHover(ASTNode node, Annotation annotation, ITypeBinding type, int offset, TextDocument doc, IJavaProject project, SpringBootApp[] runningApps) {
    if (runningApps.length > 0) {
        StringBuilder hover = new StringBuilder();
        LiveBean definedBean = getDefinedBean(annotation);
        if (definedBean != null) {
            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());
                if (!relevantBeans.isEmpty()) {
                    if (!hasInterestingApp) {
                        hasInterestingApp = true;
                    } else {
                        hover.append("\n\n");
                    }
                    hover.append(LiveHoverUtils.niceAppName(app) + ":");
                    for (LiveBean bean : relevantBeans) {
                        hover.append("\n\n");
                        hasAutowiring |= addAutomaticallyWired(hover, annotation, beans, bean, project);
                    }
                }
            }
            if (hasInterestingApp && hasAutowiring) {
                return new Hover(ImmutableList.of(Either.forLeft(hover.toString())));
            }
        }
    }
    return null;
}
Also used : SpringBootApp(org.springframework.ide.vscode.commons.boot.app.cli.SpringBootApp) Hover(org.eclipse.lsp4j.Hover) LiveBeansModel(org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBeansModel) LiveBean(org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBean)

Example 7 with LiveBean

use of org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBean in project sts4 by spring-projects.

the class AutowiredHoverProvider method addAutomaticallyWired.

private boolean addAutomaticallyWired(StringBuilder hover, Annotation annotation, LiveBeansModel beans, LiveBean bean, IJavaProject project) {
    boolean result = false;
    String[] dependencies = bean.getDependencies();
    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.showBeanWithResource(server, dependencyBean, "  ", project));
            }
            firstDependency = false;
        }
    }
    return result;
}
Also used : LiveBean(org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBean)

Example 8 with LiveBean

use of org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBean in project sts4 by spring-projects.

the class ComponentInjectionsHoverProvider method addAutomaticallyWiredContructor.

@Override
protected void addAutomaticallyWiredContructor(StringBuilder hover, Annotation annotation, LiveBeansModel beans, LiveBean bean, IJavaProject project) {
    TypeDeclaration typeDecl = ASTUtils.findDeclaringType(annotation);
    if (typeDecl != null) {
        MethodDeclaration[] constructors = ASTUtils.findConstructors(typeDecl);
        if (constructors != null && constructors.length == 1 && !hasAutowiredAnnotation(constructors[0])) {
            String[] dependencies = bean.getDependencies();
            if (dependencies != null && dependencies.length > 0) {
                hover.append("\n\n");
                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.showBeanWithResource(server, dependencyBean, "  ", project));
                    }
                    firstDependency = false;
                }
            }
        }
    }
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) LiveBean(org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBean)

Example 9 with LiveBean

use of org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBean in project sts4 by spring-projects.

the class LiveHoverUtils method findRelevantBeans.

public static Stream<LiveBean> findRelevantBeans(SpringBootApp app, LiveBean definedBean) {
    LiveBeansModel beansModel = app.getBeans();
    if (beansModel != null) {
        Stream<LiveBean> relevantBeans = beansModel.getBeansOfName(definedBean.getId()).stream();
        String type = definedBean.getType();
        if (type != null) {
            relevantBeans = relevantBeans.filter(bean -> type.equals(bean.getType(true)));
        }
        return relevantBeans;
    }
    return Stream.empty();
}
Also used : IJavaProject(org.springframework.ide.vscode.commons.java.IJavaProject) Renderables(org.springframework.ide.vscode.commons.util.Renderables) BootJavaLanguageServerComponents(org.springframework.ide.vscode.boot.java.BootJavaLanguageServerComponents) StringUtil(org.springframework.ide.vscode.commons.util.StringUtil) Stream(java.util.stream.Stream) SpringResource(org.springframework.ide.vscode.boot.java.utils.SpringResource) LiveBean(org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBean) SourceLinks(org.springframework.ide.vscode.boot.java.links.SourceLinks) LiveBeansModel(org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBeansModel) SpringBootApp(org.springframework.ide.vscode.commons.boot.app.cli.SpringBootApp) Optional(java.util.Optional) SourceLinkFactory(org.springframework.ide.vscode.boot.java.links.SourceLinkFactory) SimpleLanguageServer(org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer) LiveBeansModel(org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBeansModel) LiveBean(org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBean)

Example 10 with LiveBean

use of org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBean in project sts4 by spring-projects.

the class LiveBeansModelTest method testEmptyModel.

@Test
public void testEmptyModel() throws Exception {
    String json = IOUtils.toString(getResourceAsStream("/live-beans-models/empty-live-beans-model.json"));
    LiveBeansModel model = LiveBeansModel.parse(json);
    List<LiveBean> bean = model.getBeansOfType("org.test.DependencyA");
    assertEquals(0, bean.size());
}
Also used : LiveBeansModel(org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBeansModel) LiveBean(org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBean) Test(org.junit.Test)

Aggregations

LiveBean (org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBean)10 LiveBeansModel (org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBeansModel)7 SpringBootApp (org.springframework.ide.vscode.commons.boot.app.cli.SpringBootApp)4 Hover (org.eclipse.lsp4j.Hover)3 Test (org.junit.Test)3 Optional (java.util.Optional)1 Stream (java.util.stream.Stream)1 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)1 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)1 BootJavaLanguageServerComponents (org.springframework.ide.vscode.boot.java.BootJavaLanguageServerComponents)1 SourceLinkFactory (org.springframework.ide.vscode.boot.java.links.SourceLinkFactory)1 SourceLinks (org.springframework.ide.vscode.boot.java.links.SourceLinks)1 SpringResource (org.springframework.ide.vscode.boot.java.utils.SpringResource)1 IJavaProject (org.springframework.ide.vscode.commons.java.IJavaProject)1 SimpleLanguageServer (org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer)1 Renderables (org.springframework.ide.vscode.commons.util.Renderables)1 StringUtil (org.springframework.ide.vscode.commons.util.StringUtil)1