Add index of beans by dependency to beans model
Also, replace model implementation with immutable / builder style data structures.
This commit is contained in:
@@ -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()));
|
||||
}
|
||||
|
||||
@@ -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()));
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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.
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user