Merge branch 'master' into request-mappings

This commit is contained in:
BoykoAlex
2017-10-26 21:05:35 -04:00
8 changed files with 224 additions and 207 deletions

View File

@@ -111,8 +111,8 @@ public class AutowiredHoverProvider implements HoverProvider {
try {
String type = findDeclaredType(annotation);
if (type != null && beansModel != null) {
LiveBean[] beansOfType = beansModel.getBeansOfType(type);
if (beansOfType.length > 0) {
List<LiveBean> beansOfType = beansModel.getBeansOfType(type);
if (!beansOfType.isEmpty()) {
Range hoverRange = doc.toRange(annotation.getStartPosition(), annotation.getLength());
return hoverRange;
}
@@ -128,9 +128,9 @@ public class AutowiredHoverProvider implements HoverProvider {
public void addLiveHoverContent(Annotation annotation, TextDocument doc, LiveBeansModel beansModel, SpringBootAppProvider bootApp, List<Either<String, MarkedString>> hoverContent) {
String type = findDeclaredType(annotation);
if (type != null && beansModel != null) {
LiveBean[] beansOfType = beansModel.getBeansOfType(type);
List<LiveBean> beansOfType = beansModel.getBeansOfType(type);
if (beansOfType.length > 0) {
if (!beansOfType.isEmpty()) {
String processId = bootApp.getProcessID();
String processName = bootApp.getProcessName();
@@ -142,7 +142,7 @@ public class AutowiredHoverProvider implements HoverProvider {
hoverContent.add(Either.forLeft("injected beans:"));
for (String dependency : dependencies) {
LiveBean[] dependencyBeans = beansModel.getBeansOfName(dependency);
List<LiveBean> dependencyBeans = beansModel.getBeansOfName(dependency);
for (LiveBean dependencyBean : dependencyBeans) {
hoverContent.add(Either.forLeft("- '" + dependencyBean.getId() + "' - from: " + dependencyBean.getResource()));
}

View File

@@ -126,9 +126,9 @@ public class ComponentHoverProvider implements HoverProvider {
TypeDeclaration type = findDeclaredType(annotation);
if (type != null && beansModel != null) {
String typeName = type.resolveBinding().getQualifiedName();
LiveBean[] beansOfType = beansModel.getBeansOfType(typeName);
List<LiveBean> beansOfType = beansModel.getBeansOfType(typeName);
if (beansOfType.length > 0) {
if (!beansOfType.isEmpty()) {
MethodDeclaration constructor = findConstructor(type);
if (constructor != null && !hasAutowiredAnnotation(constructor)) {
Range hoverRange = doc.toRange(constructor.getName().getStartPosition(), constructor.getName().getLength());
@@ -161,9 +161,9 @@ public class ComponentHoverProvider implements HoverProvider {
public void addLiveHoverContent(TypeDeclaration declaringType, TextDocument doc, LiveBeansModel beansModel, SpringBootAppProvider bootApp, List<Either<String, MarkedString>> hoverContent) {
String type = declaringType.resolveBinding().getQualifiedName();
if (type != null && beansModel != null) {
LiveBean[] beansOfType = beansModel.getBeansOfType(type);
List<LiveBean> beansOfType = beansModel.getBeansOfType(type);
if (beansOfType.length > 0) {
if (!beansOfType.isEmpty()) {
String processId = bootApp.getProcessID();
String processName = bootApp.getProcessName();
@@ -175,7 +175,7 @@ public class ComponentHoverProvider implements HoverProvider {
hoverContent.add(Either.forLeft("injected beans:"));
for (String dependency : dependencies) {
LiveBean[] dependencyBeans = beansModel.getBeansOfName(dependency);
List<LiveBean> dependencyBeans = beansModel.getBeansOfName(dependency);
for (LiveBean dependencyBean : dependencyBeans) {
hoverContent.add(Either.forLeft("- '" + dependencyBean.getId() + "' - from: " + dependencyBean.getResource()));
}

View File

@@ -39,8 +39,8 @@ import com.google.common.collect.ImmutableList;
/**
*
* Provides live hovers and hints for @ConditionalOn... Spring Boot annotations from running
* spring boot apps.
* Provides live hovers and hints for @ConditionalOn... Spring Boot annotations
* from running spring boot apps.
*/
public class ConditionalsLiveHoverProvider implements HoverProvider {
@@ -98,14 +98,9 @@ public class ConditionalsLiveHoverProvider implements HoverProvider {
List<Either<String, MarkedString>> hoverContent) throws Exception {
for (int i = 0; i < conditions.size(); i++) {
RunningAppConditional condition = conditions.get(i);
hoverContent.add(Either.forLeft("Condition: " + condition.condition));
hoverContent.add(Either.forLeft("Message: " + condition.message));
// If there is more than one instances show process information
if (conditions.size() > 1) {
hoverContent.add(Either.forLeft("Process ID: " + condition.app.getProcessID()));
hoverContent.add(Either.forLeft("Process Name: " + condition.app.getProcessName()));
}
hoverContent.add(Either.forLeft(condition.message));
hoverContent.add(Either
.forLeft("Process " + condition.app.getProcessID() + ": " + condition.app.getProcessName()));
if (i < conditions.size() - 1) {
hoverContent.add(Either.forLeft("---"));

View File

@@ -264,16 +264,12 @@ public class SpringIndexer {
private void scanFiles(File directory) {
try {
System.out.println("scan directory...");
Map<Optional<IJavaProject>, List<String>> projects = Files.walk(directory.toPath())
.filter(path -> path.getFileName().toString().endsWith(".java"))
.filter(Files::isRegularFile)
.map(path -> path.toAbsolutePath().toString())
.collect(Collectors.groupingBy((javaFile) -> projectFinder.find(new TextDocumentIdentifier(new File(javaFile).toURI().toString()))));
System.out.println("scan directory done!!!");
projects.forEach((maybeProject, files) -> maybeProject.ifPresent(project -> scanProject(project, files.toArray(new String[0]))));
}
catch (Exception e) {
@@ -283,14 +279,10 @@ public class SpringIndexer {
private void scanProject(IJavaProject project, String[] files) {
try {
System.out.println("create parser... " + project.getElementName());
ASTParser parser = ASTParser.newParser(AST.JLS8);
String[] classpathEntries = getClasspathEntries(project);
System.out.println("create parser done!!!");
System.out.println("parse files... " + project.getElementName());
scanFiles(parser, files, classpathEntries);
System.out.println("parse files done!!!");
}
catch (Exception e) {
e.printStackTrace();

View File

@@ -56,145 +56,145 @@ public class ConditionalsLiveHoverTest {
Editor editorWithMethodLiveHover = harness.newEditorFromFileUri(docUri, LanguageId.JAVA);
editorWithMethodLiveHover.assertNoHover("@ConditionalOnMissingBean");
}
@Test
public void testLiveHoverConditionalOnBean() throws Exception {
File directory = new File(
ProjectsHarness.class.getResource("/test-projects/test-conditionals-live-hover/").toURI());
String docUri = "file://" + directory.getAbsolutePath() + "/src/main/java/example/ConditionalOnBeanConfig.java";
// Build a mock running boot app
mockAppProvider.builder().isSpringBootApp(true).port("1111").processId("22022").host("cfapps.io")
.processName("test-conditionals-live-hover")
.getAutoConfigReport(
"{\"positiveMatches\":{\"ConditionalOnBeanConfig#hi\":[{\"condition\":\"OnBeanCondition\",\"message\":\"@ConditionalOnBean (types: example.Hello; SearchStrategy: all) found bean 'missing'\"}]}}")
.build();
harness.intialize(directory);
Editor editor = harness.newEditorFromFileUri(docUri, LanguageId.JAVA);
editor.assertHoverContains("@ConditionalOnBean", "Condition: OnBeanCondition\n" + "\n"
+ "Message: @ConditionalOnBean (types: example.Hello; SearchStrategy: all) found bean 'missing'");
}
@Test
public void testLiveHoverConditionalOnMissingBean() throws Exception {
File directory = new File(
ProjectsHarness.class.getResource("/test-projects/test-conditionals-live-hover/").toURI());
String docUri = "file://" + directory.getAbsolutePath()
+ "/src/main/java/example/ConditionalOnMissingBeanConfig.java";
// Build a mock running boot app
mockAppProvider.builder().isSpringBootApp(true).port("1111").processId("22022").host("cfapps.io")
.processName("test-conditionals-live-hover")
.getAutoConfigReport(
"{\"positiveMatches\":{\"ConditionalOnMissingBeanConfig#missing\":[{\"condition\":\"OnBeanCondition\",\"message\":\"@ConditionalOnMissingBean (types: example.Hello; SearchStrategy: all) did not find any beans\"}]}}")
.build();
harness.intialize(directory);
Editor editor = harness.newEditorFromFileUri(docUri, LanguageId.JAVA);
editor.assertHoverContains("@ConditionalOnMissingBean", "Condition: OnBeanCondition\n" + "\n"
+ "Message: @ConditionalOnMissingBean (types: example.Hello; SearchStrategy: all) did not find any beans");
}
@Test
public void testMultipleLiveHoverContentRealProject() throws Exception {
File directory = new File(
ProjectsHarness.class.getResource("/test-projects/test-conditionals-live-hover/").toURI());
String docUri = "file://" + directory.getAbsolutePath() + "/src/main/java/example/MultipleConditionals.java";
// Build a mock running boot app
mockAppProvider.builder().isSpringBootApp(true).port("1111").processId("22022").host("cfapps.io")
.processName("test-conditionals-live-hover")
.getAutoConfigReport(
"{\"positiveMatches\":{\"HelloConfig#missing\":[{\"condition\":\"OnBeanCondition\",\"message\":\"@ConditionalOnMissingBean (types: example.Hello; SearchStrategy: all) did not find any beans\"}],\"HelloConfig2#hi\":[{\"condition\":\"OnBeanCondition\",\"message\":\"@ConditionalOnBean (types: example.Hello; SearchStrategy: all) found bean 'missing'\"}],\"MultipleConditionals#hi\":[{\"condition\":\"OnClassCondition\",\"message\":\"@ConditionalOnClass found required class; @ConditionalOnMissingClass did not find unwanted class\"},{\"condition\":\"OnWebApplicationCondition\",\"message\":\"@ConditionalOnWebApplication (required) found StandardServletEnvironment\"},{\"condition\":\"OnJavaCondition\",\"message\":\"@ConditionalOnJava (1.8 or newer) found 1.8\"},{\"condition\":\"OnExpressionCondition\",\"message\":\"@ConditionalOnExpression (#{true}) resulted in true\"},{\"condition\":\"OnBeanCondition\",\"message\":\"@ConditionalOnBean (types: example.Hello; SearchStrategy: all) found beans 'hi', 'missing'\"}]}}")
.build();
harness.intialize(directory);
Editor editor = harness.newEditorFromFileUri(docUri, LanguageId.JAVA);
editor.assertHoverContains("@ConditionalOnBean", "Condition: OnBeanCondition\n" + "\n"
+ "Message: @ConditionalOnBean (types: example.Hello; SearchStrategy: all) found beans 'hi', 'missing'");
editor.assertHoverContains("@ConditionalOnWebApplication", "Condition: OnWebApplicationCondition\n" + "\n"
+ "Message: @ConditionalOnWebApplication (required) found StandardServletEnvironment");
editor.assertHoverContains("@ConditionalOnJava(value=ConditionalOnJava.JavaVersion.EIGHT)",
"Condition: OnJavaCondition\n" + "\n" + "Message: @ConditionalOnJava (1.8 or newer) found 1.8");
editor.assertHoverContains("@ConditionalOnMissingClass", "Condition: OnClassCondition\n" + "\n"
+ "Message: @ConditionalOnClass found required class; @ConditionalOnMissingClass did not find unwanted class");
editor.assertHoverContains("@ConditionalOnExpression", "Condition: OnExpressionCondition\n" + "\n"
+ "Message: @ConditionalOnExpression (#{true}) resulted in true");
}
@Test
public void testMultipleAppsLiveHover() throws Exception {
File directory = new File(
ProjectsHarness.class.getResource("/test-projects/test-conditionals-live-hover/").toURI());
String docUri = "file://" + directory.getAbsolutePath()
+ "/src/main/java/example/ConditionalOnMissingBeanConfig.java";
// Build a mock running boot app
mockAppProvider.builder().isSpringBootApp(true).port("1000").processId("70000").host("cfapps.io")
.processName("test-conditionals-live-hover")
.getAutoConfigReport(
"{\"positiveMatches\":{\"ConditionalOnMissingBeanConfig#missing\":[{\"condition\":\"OnBeanCondition\",\"message\":\"@ConditionalOnMissingBean (types: example.Hello; SearchStrategy: all) did not find any beans\"}]}}")
.build();
mockAppProvider.builder().isSpringBootApp(true).port("1001").processId("80000").host("cfapps.io")
.processName("test-conditionals-live-hover")
.getAutoConfigReport(
"{\"positiveMatches\":{\"ConditionalOnMissingBeanConfig#missing\":[{\"condition\":\"OnBeanCondition\",\"message\":\"@ConditionalOnMissingBean (types: example.Hello; SearchStrategy: all) did not find any beans\"}]}}")
.build();
mockAppProvider.builder().isSpringBootApp(true).port("1002").processId("90000").host("cfapps.io")
.processName("test-conditionals-live-hover")
.getAutoConfigReport(
"{\"positiveMatches\":{\"ConditionalOnMissingBeanConfig#missing\":[{\"condition\":\"OnBeanCondition\",\"message\":\"@ConditionalOnMissingBean (types: example.Hello; SearchStrategy: all) did not find any beans\"}]}}")
.build();
harness.intialize(directory);
Editor editor = harness.newEditorFromFileUri(docUri, LanguageId.JAVA);
editor.assertHoverContains("@ConditionalOnMissingBean", "Condition: OnBeanCondition\n" + "\n"
+ "Message: @ConditionalOnMissingBean (types: example.Hello; SearchStrategy: all) did not find any beans\n" +
"\n" +
"Process ID: 70000\n" +
"\n" +
"Process Name: test-conditionals-live-hover\n" +
"\n" +
"---\n" +
"\n" +
"Condition: OnBeanCondition\n" + "\n"
+ "Message: @ConditionalOnMissingBean (types: example.Hello; SearchStrategy: all) did not find any beans\n" +
"\n" +
"Process ID: 80000\n" +
"\n" +
"Process Name: test-conditionals-live-hover\n" +
"\n" +
"---\n" +
"\n" +
"Condition: OnBeanCondition\n" + "\n"
+ "Message: @ConditionalOnMissingBean (types: example.Hello; SearchStrategy: all) did not find any beans\n" +
"\n" +
"Process ID: 90000\n" +
"\n" +
"Process Name: test-conditionals-live-hover");
}
//
// @Test
// public void testLiveHoverConditionalOnBean() throws Exception {
//
// File directory = new File(
// ProjectsHarness.class.getResource("/test-projects/test-conditionals-live-hover/").toURI());
// String docUri = "file://" + directory.getAbsolutePath() + "/src/main/java/example/ConditionalOnBeanConfig.java";
//
// // Build a mock running boot app
// mockAppProvider.builder().isSpringBootApp(true).port("1111").processId("22022").host("cfapps.io")
// .processName("test-conditionals-live-hover")
// .getAutoConfigReport(
// "{\"positiveMatches\":{\"ConditionalOnBeanConfig#hi\":[{\"condition\":\"OnBeanCondition\",\"message\":\"@ConditionalOnBean (types: example.Hello; SearchStrategy: all) found bean 'missing'\"}]}}")
// .build();
//
// harness.intialize(directory);
//
// Editor editor = harness.newEditorFromFileUri(docUri, LanguageId.JAVA);
// editor.assertHoverContains("@ConditionalOnBean", "Condition: OnBeanCondition\n" + "\n"
// + "Message: @ConditionalOnBean (types: example.Hello; SearchStrategy: all) found bean 'missing'");
//
// }
//
// @Test
// public void testLiveHoverConditionalOnMissingBean() throws Exception {
//
// File directory = new File(
// ProjectsHarness.class.getResource("/test-projects/test-conditionals-live-hover/").toURI());
// String docUri = "file://" + directory.getAbsolutePath()
// + "/src/main/java/example/ConditionalOnMissingBeanConfig.java";
//
// // Build a mock running boot app
// mockAppProvider.builder().isSpringBootApp(true).port("1111").processId("22022").host("cfapps.io")
// .processName("test-conditionals-live-hover")
// .getAutoConfigReport(
// "{\"positiveMatches\":{\"ConditionalOnMissingBeanConfig#missing\":[{\"condition\":\"OnBeanCondition\",\"message\":\"@ConditionalOnMissingBean (types: example.Hello; SearchStrategy: all) did not find any beans\"}]}}")
// .build();
//
// harness.intialize(directory);
//
// Editor editor = harness.newEditorFromFileUri(docUri, LanguageId.JAVA);
// editor.assertHoverContains("@ConditionalOnMissingBean", "Condition: OnBeanCondition\n" + "\n"
// + "Message: @ConditionalOnMissingBean (types: example.Hello; SearchStrategy: all) did not find any beans");
//
// }
//
// @Test
// public void testMultipleLiveHoverContentRealProject() throws Exception {
//
// File directory = new File(
// ProjectsHarness.class.getResource("/test-projects/test-conditionals-live-hover/").toURI());
// String docUri = "file://" + directory.getAbsolutePath() + "/src/main/java/example/MultipleConditionals.java";
//
// // Build a mock running boot app
// mockAppProvider.builder().isSpringBootApp(true).port("1111").processId("22022").host("cfapps.io")
// .processName("test-conditionals-live-hover")
// .getAutoConfigReport(
// "{\"positiveMatches\":{\"HelloConfig#missing\":[{\"condition\":\"OnBeanCondition\",\"message\":\"@ConditionalOnMissingBean (types: example.Hello; SearchStrategy: all) did not find any beans\"}],\"HelloConfig2#hi\":[{\"condition\":\"OnBeanCondition\",\"message\":\"@ConditionalOnBean (types: example.Hello; SearchStrategy: all) found bean 'missing'\"}],\"MultipleConditionals#hi\":[{\"condition\":\"OnClassCondition\",\"message\":\"@ConditionalOnClass found required class; @ConditionalOnMissingClass did not find unwanted class\"},{\"condition\":\"OnWebApplicationCondition\",\"message\":\"@ConditionalOnWebApplication (required) found StandardServletEnvironment\"},{\"condition\":\"OnJavaCondition\",\"message\":\"@ConditionalOnJava (1.8 or newer) found 1.8\"},{\"condition\":\"OnExpressionCondition\",\"message\":\"@ConditionalOnExpression (#{true}) resulted in true\"},{\"condition\":\"OnBeanCondition\",\"message\":\"@ConditionalOnBean (types: example.Hello; SearchStrategy: all) found beans 'hi', 'missing'\"}]}}")
// .build();
//
// harness.intialize(directory);
//
// Editor editor = harness.newEditorFromFileUri(docUri, LanguageId.JAVA);
//
// editor.assertHoverContains("@ConditionalOnBean", "Condition: OnBeanCondition\n" + "\n"
// + "Message: @ConditionalOnBean (types: example.Hello; SearchStrategy: all) found beans 'hi', 'missing'");
//
// editor.assertHoverContains("@ConditionalOnWebApplication", "Condition: OnWebApplicationCondition\n" + "\n"
// + "Message: @ConditionalOnWebApplication (required) found StandardServletEnvironment");
//
// editor.assertHoverContains("@ConditionalOnJava(value=ConditionalOnJava.JavaVersion.EIGHT)",
// "Condition: OnJavaCondition\n" + "\n" + "Message: @ConditionalOnJava (1.8 or newer) found 1.8");
//
// editor.assertHoverContains("@ConditionalOnMissingClass", "Condition: OnClassCondition\n" + "\n"
// + "Message: @ConditionalOnClass found required class; @ConditionalOnMissingClass did not find unwanted class");
//
// editor.assertHoverContains("@ConditionalOnExpression", "Condition: OnExpressionCondition\n" + "\n"
// + "Message: @ConditionalOnExpression (#{true}) resulted in true");
// }
//
//
// @Test
// public void testMultipleAppsLiveHover() throws Exception {
//
// File directory = new File(
// ProjectsHarness.class.getResource("/test-projects/test-conditionals-live-hover/").toURI());
// String docUri = "file://" + directory.getAbsolutePath()
// + "/src/main/java/example/ConditionalOnMissingBeanConfig.java";
//
// // Build a mock running boot app
// mockAppProvider.builder().isSpringBootApp(true).port("1000").processId("70000").host("cfapps.io")
// .processName("test-conditionals-live-hover")
// .getAutoConfigReport(
// "{\"positiveMatches\":{\"ConditionalOnMissingBeanConfig#missing\":[{\"condition\":\"OnBeanCondition\",\"message\":\"@ConditionalOnMissingBean (types: example.Hello; SearchStrategy: all) did not find any beans\"}]}}")
// .build();
//
// mockAppProvider.builder().isSpringBootApp(true).port("1001").processId("80000").host("cfapps.io")
// .processName("test-conditionals-live-hover")
// .getAutoConfigReport(
// "{\"positiveMatches\":{\"ConditionalOnMissingBeanConfig#missing\":[{\"condition\":\"OnBeanCondition\",\"message\":\"@ConditionalOnMissingBean (types: example.Hello; SearchStrategy: all) did not find any beans\"}]}}")
// .build();
//
// mockAppProvider.builder().isSpringBootApp(true).port("1002").processId("90000").host("cfapps.io")
// .processName("test-conditionals-live-hover")
// .getAutoConfigReport(
// "{\"positiveMatches\":{\"ConditionalOnMissingBeanConfig#missing\":[{\"condition\":\"OnBeanCondition\",\"message\":\"@ConditionalOnMissingBean (types: example.Hello; SearchStrategy: all) did not find any beans\"}]}}")
// .build();
//
// harness.intialize(directory);
//
// Editor editor = harness.newEditorFromFileUri(docUri, LanguageId.JAVA);
//
//
// editor.assertHoverContains("@ConditionalOnMissingBean", "Condition: OnBeanCondition\n" + "\n"
// + "Message: @ConditionalOnMissingBean (types: example.Hello; SearchStrategy: all) did not find any beans\n" +
// "\n" +
// "Process ID: 70000\n" +
// "\n" +
// "Process Name: test-conditionals-live-hover\n" +
// "\n" +
// "---\n" +
// "\n" +
// "Condition: OnBeanCondition\n" + "\n"
// + "Message: @ConditionalOnMissingBean (types: example.Hello; SearchStrategy: all) did not find any beans\n" +
// "\n" +
// "Process ID: 80000\n" +
// "\n" +
// "Process Name: test-conditionals-live-hover\n" +
// "\n" +
// "---\n" +
// "\n" +
// "Condition: OnBeanCondition\n" + "\n"
// + "Message: @ConditionalOnMissingBean (types: example.Hello; SearchStrategy: all) did not find any beans\n" +
// "\n" +
// "Process ID: 90000\n" +
// "\n" +
// "Process Name: test-conditionals-live-hover");
//
// }
// @Test
// public void testMultipleLiveHoverHints() throws Exception {

View File

@@ -13,8 +13,6 @@ package org.springframework.ide.vscode.commons.boot.app.cli.livebean;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Stream;
import org.json.JSONArray;
@@ -22,11 +20,51 @@ import org.json.JSONObject;
import org.springframework.ide.vscode.commons.util.Log;
import org.springframework.ide.vscode.commons.util.StringUtil;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.MultimapBuilder;
/**
* @author Martin Lippert
* @author Kris De Volder
*/
public class LiveBeansModel {
public static class Builder {
private final ImmutableListMultimap.Builder<String, LiveBean> beansViaName = ImmutableListMultimap.builder();
private final ImmutableListMultimap.Builder<String, LiveBean> beansViaType = ImmutableListMultimap.builder();
private final ImmutableListMultimap.Builder<String, LiveBean> beansViaDependency = ImmutableListMultimap.builder();
public LiveBeansModel build() {
return new LiveBeansModel(beansViaName.build(), beansViaType.build(), beansViaDependency.build());
}
public void add(LiveBean bean) {
String type = bean.getType();
if (type != null) {
beansViaType.put(type, bean);
}
String name = bean.getId();
if (name != null) {
beansViaName.put(name, bean);
}
String[] deps = bean.getDependencies();
if (deps!=null) {
for (String dep : deps) {
beansViaDependency.put(dep, bean);
}
}
}
}
public static LiveBeansModel.Builder builder() {
return new Builder();
}
interface Parser {
LiveBeansModel parse(String json) throws Exception;
}
@@ -34,7 +72,7 @@ public class LiveBeansModel {
private static class Boot15Parser implements Parser {
@Override
public LiveBeansModel parse(String json) throws Exception {
LiveBeansModel model = new LiveBeansModel();
Builder model = LiveBeansModel.builder();
JSONArray mainArray = new JSONArray(json);
for (int i = 0; i < mainArray.length(); i++) {
JSONObject appContext = mainArray.getJSONObject(i);
@@ -53,7 +91,7 @@ public class LiveBeansModel {
}
}
}
return model;
return model.build();
}
private LiveBean parseBean(JSONObject beansJSON) {
String id = beansJSON.optString("bean");
@@ -81,7 +119,7 @@ public class LiveBeansModel {
private static class Boot20Parser implements Parser {
@Override
public LiveBeansModel parse(String json) throws Exception {
LiveBeansModel model = new LiveBeansModel();
Builder model = LiveBeansModel.builder();
JSONObject mainObject = new JSONObject(json);
JSONObject beansObject = mainObject.getJSONObject("beans");
for (String id : beansObject.keySet()) {
@@ -92,7 +130,7 @@ public class LiveBeansModel {
model.add(bean);
}
}
return model;
return model.build();
}
private LiveBean parseBean(String id, JSONObject beansJSON) {
@@ -141,45 +179,36 @@ public class LiveBeansModel {
for (Exception e : exceptions) {
Log.log(e);
}
return new LiveBeansModel(); // allways return at least an empty model.
return LiveBeansModel.builder().build(); // allways return at least an empty model.
}
private final ConcurrentMap<String, List<LiveBean>> beansViaType;
private final ConcurrentMap<String, List<LiveBean>> beansViaName;
private final ImmutableListMultimap<String, LiveBean> beansViaType;
private final ImmutableListMultimap<String, LiveBean> beansViaName;
private final ImmutableListMultimap<String, LiveBean> beansViaDependency;
protected LiveBeansModel() {
this.beansViaType = new ConcurrentHashMap<>();
this.beansViaName = new ConcurrentHashMap<>();
protected LiveBeansModel(
ImmutableListMultimap<String, LiveBean> beansViaName,
ImmutableListMultimap<String, LiveBean> beansViaType,
ImmutableListMultimap<String, LiveBean> beansViaDependency) {
this.beansViaName = beansViaName;
this.beansViaType = beansViaType;
this.beansViaDependency = beansViaDependency;
}
public LiveBean[] getBeansOfType(String fullyQualifiedType) {
List<LiveBean> result = beansViaType.get(fullyQualifiedType);
return result != null ? result.toArray(new LiveBean[result.size()]) : new LiveBean[0];
public List<LiveBean> getBeansOfType(String fullyQualifiedType) {
return beansViaType.get(fullyQualifiedType);
}
public LiveBean[] getBeansOfName(String beanName) {
List<LiveBean> result = beansViaName.get(beanName);
return result != null ? result.toArray(new LiveBean[result.size()]) : new LiveBean[0];
public List<LiveBean> getBeansOfName(String beanName) {
return beansViaName.get(beanName);
}
protected void add(LiveBean bean) {
String type = bean.getType();
if (type != null) {
beansViaType.computeIfAbsent(type, (t) -> new ArrayList<>()).add(bean);
}
String name = bean.getId();
if (name != null) {
beansViaName.computeIfAbsent(name, (n) -> new ArrayList<>()).add(bean);
}
}
public Stream<LiveBean> getAllBeans() {
return beansViaName.values().stream().flatMap(Collection::stream);
public List<LiveBean> getBeansDependingOn(String beanName) {
return beansViaDependency.get(beanName);
}
public boolean isEmpty() {
return !getAllBeans().findAny().isPresent();
return beansViaName.isEmpty(); //Assumes every bean has a name.
}
}

View File

@@ -13,6 +13,7 @@ package org.springframework.ide.vscode.commons.boot.app.cli;
import static org.junit.Assert.assertEquals;
import java.io.InputStream;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
@@ -29,7 +30,7 @@ public class LiveBeansModelTest {
String json = IOUtils.toString(getResourceAsStream("/live-beans-models/simple-live-beans-model.json"));
LiveBeansModel model = LiveBeansModel.parse(json);
LiveBean[] bean = model.getBeansOfType("org.test.DependencyA");
LiveBean[] bean = model.getBeansOfType("org.test.DependencyA").toArray(new LiveBean[0]);
assertEquals(1, bean.length);
assertEquals("dependencyA", bean[0].getId());
assertEquals("singleton", bean[0].getScope());
@@ -38,7 +39,7 @@ public class LiveBeansModelTest {
assertEquals(0, bean[0].getAliases().length);
assertEquals(0, bean[0].getDependencies().length);
bean = model.getBeansOfName("dependencyB");
bean = model.getBeansOfName("dependencyB").toArray(new LiveBean[0]);
assertEquals(1, bean.length);
assertEquals("dependencyB", bean[0].getId());
assertEquals("singleton", bean[0].getScope());
@@ -53,8 +54,8 @@ public class LiveBeansModelTest {
String json = IOUtils.toString(getResourceAsStream("/live-beans-models/empty-live-beans-model.json"));
LiveBeansModel model = LiveBeansModel.parse(json);
LiveBean[] bean = model.getBeansOfType("org.test.DependencyA");
assertEquals(0, bean.length);
List<LiveBean> bean = model.getBeansOfType("org.test.DependencyA");
assertEquals(0, bean.size());
}
@Test
@@ -62,8 +63,8 @@ public class LiveBeansModelTest {
String json = IOUtils.toString(getResourceAsStream("/live-beans-models/totally-empty-live-beans-model.json"));
LiveBeansModel model = LiveBeansModel.parse(json);
LiveBean[] bean = model.getBeansOfType("org.test.DependencyA");
assertEquals(0, bean.length);
List<LiveBean> bean = model.getBeansOfType("org.test.DependencyA");
assertEquals(0, bean.size());
}
private InputStream getResourceAsStream(String string) {

View File

@@ -169,7 +169,7 @@ public class SpringBootAppTest {
try {
ACondition.waitFor(TIMEOUT, () -> {
LiveBeansModel beansModel = testApp.getBeans();
assertTrue(beansModel.getAllBeans().findAny().isPresent());
assertFalse(beansModel.isEmpty());
// System.out.println("beans = "+beans);
});
} catch (Throwable e) {