SpringIndexElement serialization. Initial Structure view in VSCode

Signed-off-by: aboyko <alex.boyko@broadcom.com>
This commit is contained in:
aboyko
2025-04-15 16:58:45 -04:00
parent 260e9c9d49
commit 8ccaa5f5ef
14 changed files with 873 additions and 180 deletions

View File

@@ -21,6 +21,7 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.eclipse.lsp4j.CodeActionKind;
@@ -45,6 +46,7 @@ import org.springframework.ide.vscode.boot.index.cache.IndexCache;
import org.springframework.ide.vscode.boot.index.cache.IndexCacheOnDiscDeltaBased;
import org.springframework.ide.vscode.boot.index.cache.IndexCacheVoid;
import org.springframework.ide.vscode.boot.java.JavaDefinitionHandler;
import org.springframework.ide.vscode.boot.java.beans.ConfigPropertyIndexElement;
import org.springframework.ide.vscode.boot.java.beans.DependsOnDefinitionProvider;
import org.springframework.ide.vscode.boot.java.beans.NamedDefinitionProvider;
import org.springframework.ide.vscode.boot.java.beans.QualifierDefinitionProvider;
@@ -52,8 +54,11 @@ import org.springframework.ide.vscode.boot.java.beans.ResourceDefinitionProvider
import org.springframework.ide.vscode.boot.java.conditionals.ConditionalOnBeanDefinitionProvider;
import org.springframework.ide.vscode.boot.java.conditionals.ConditionalOnResourceDefinitionProvider;
import org.springframework.ide.vscode.boot.java.copilot.util.ResponseModifier;
import org.springframework.ide.vscode.boot.java.data.QueryMethodIndexElement;
import org.springframework.ide.vscode.boot.java.data.jpa.queries.DataQueryParameterDefinitionProvider;
import org.springframework.ide.vscode.boot.java.data.jpa.queries.JdtDataQuerySemanticTokensProvider;
import org.springframework.ide.vscode.boot.java.events.EventListenerIndexElement;
import org.springframework.ide.vscode.boot.java.events.EventPublisherIndexElement;
import org.springframework.ide.vscode.boot.java.handlers.BootJavaCodeActionProvider;
import org.springframework.ide.vscode.boot.java.handlers.BootJavaReconcileEngine;
import org.springframework.ide.vscode.boot.java.handlers.JavaCodeActionHandler;
@@ -73,6 +78,8 @@ import org.springframework.ide.vscode.boot.java.livehover.v2.SpringProcessLiveDa
import org.springframework.ide.vscode.boot.java.reconcilers.JavaReconciler;
import org.springframework.ide.vscode.boot.java.reconcilers.JdtAstReconciler;
import org.springframework.ide.vscode.boot.java.reconcilers.JdtReconciler;
import org.springframework.ide.vscode.boot.java.requestmapping.RequestMappingIndexElement;
import org.springframework.ide.vscode.boot.java.requestmapping.WebfluxRouteElementRangesIndexElement;
import org.springframework.ide.vscode.boot.java.spel.SpelDefinitionProvider;
import org.springframework.ide.vscode.boot.java.utils.CompilationUnitCache;
import org.springframework.ide.vscode.boot.java.value.ValueDefinitionProvider;
@@ -89,6 +96,7 @@ import org.springframework.ide.vscode.boot.properties.completions.SpringProperti
import org.springframework.ide.vscode.boot.xml.SpringXMLCompletionEngine;
import org.springframework.ide.vscode.boot.yaml.completions.ApplicationYamlAssistContext;
import org.springframework.ide.vscode.boot.yaml.completions.SpringYamlCompletionEngine;
import org.springframework.ide.vscode.commons.RuntimeTypeAdapterFactory;
import org.springframework.ide.vscode.commons.languageserver.LanguageServerRunner;
import org.springframework.ide.vscode.commons.languageserver.java.FutureProjectFinder;
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder;
@@ -98,6 +106,12 @@ import org.springframework.ide.vscode.commons.languageserver.util.LanguageComput
import org.springframework.ide.vscode.commons.languageserver.util.LspClient;
import org.springframework.ide.vscode.commons.languageserver.util.ServerCapabilityInitializer;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
import org.springframework.ide.vscode.commons.protocol.spring.AbstractSpringIndexElement;
import org.springframework.ide.vscode.commons.protocol.spring.AotProcessorElement;
import org.springframework.ide.vscode.commons.protocol.spring.BeanMethodContainerElement;
import org.springframework.ide.vscode.commons.protocol.spring.DocumentElement;
import org.springframework.ide.vscode.commons.protocol.spring.ProjectElement;
import org.springframework.ide.vscode.commons.protocol.spring.SpringIndexElement;
import org.springframework.ide.vscode.commons.util.FileObserver;
import org.springframework.ide.vscode.commons.util.LogRedirect;
import org.springframework.ide.vscode.commons.util.text.IDocument;
@@ -118,6 +132,7 @@ import org.yaml.snakeyaml.constructor.SafeConstructor;
import com.google.common.io.Files;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import reactor.core.publisher.Hooks;
@@ -428,4 +443,22 @@ public class BootLanguageServerBootApp {
return new ResponseModifier();
}
@Bean
Consumer<GsonBuilder> configureGson() {
return builder -> builder
.registerTypeAdapterFactory(RuntimeTypeAdapterFactory.of(SpringIndexElement.class, "_internal_node_type")
.recognizeSubtypes()
.registerSubtype(org.springframework.ide.vscode.commons.protocol.spring.Bean.class)
.registerSubtype(AotProcessorElement.class)
.registerSubtype(BeanMethodContainerElement.class)
.registerSubtype(ConfigPropertyIndexElement.class)
.registerSubtype(DocumentElement.class)
.registerSubtype(EventListenerIndexElement.class)
.registerSubtype(EventPublisherIndexElement.class)
.registerSubtype(ProjectElement.class)
.registerSubtype(QueryMethodIndexElement.class)
.registerSubtype(RequestMappingIndexElement.class)
.registerSubtype(WebfluxRouteElementRangesIndexElement.class)
.registerSubtype(AbstractSpringIndexElement.class));
}
}

View File

@@ -38,13 +38,21 @@ import java.util.stream.Collectors;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.eclipse.lsp4j.Location;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ide.vscode.commons.protocol.spring.AnnotationMetadata;
import org.springframework.ide.vscode.boot.java.beans.ConfigPropertyIndexElement;
import org.springframework.ide.vscode.boot.java.data.QueryMethodIndexElement;
import org.springframework.ide.vscode.boot.java.events.EventListenerIndexElement;
import org.springframework.ide.vscode.boot.java.events.EventPublisherIndexElement;
import org.springframework.ide.vscode.boot.java.requestmapping.RequestMappingIndexElement;
import org.springframework.ide.vscode.boot.java.requestmapping.WebfluxRouteElementRangesIndexElement;
import org.springframework.ide.vscode.commons.RuntimeTypeAdapterFactory;
import org.springframework.ide.vscode.commons.protocol.spring.AbstractSpringIndexElement;
import org.springframework.ide.vscode.commons.protocol.spring.AotProcessorElement;
import org.springframework.ide.vscode.commons.protocol.spring.Bean;
import org.springframework.ide.vscode.commons.protocol.spring.DefaultValues;
import org.springframework.ide.vscode.commons.protocol.spring.InjectionPoint;
import org.springframework.ide.vscode.commons.protocol.spring.BeanMethodContainerElement;
import org.springframework.ide.vscode.commons.protocol.spring.DocumentElement;
import org.springframework.ide.vscode.commons.protocol.spring.ProjectElement;
import org.springframework.ide.vscode.commons.protocol.spring.SpringIndexElement;
import org.springframework.ide.vscode.commons.util.UriUtil;
@@ -569,10 +577,23 @@ public class IndexCacheOnDiscDeltaBased implements IndexCache {
public static Gson createGson() {
return new GsonBuilder()
.registerTypeAdapter(DeltaStorage.class, new DeltaStorageAdapter())
.registerTypeAdapter(Bean.class, new BeanJsonAdapter())
.registerTypeAdapter(InjectionPoint.class, new InjectionPointJsonAdapter())
.registerTypeAdapter(IndexCacheStore.class, new IndexCacheStoreAdapter())
.registerTypeAdapter(SpringIndexElement.class, new SpringIndexElementAdapter())
.registerTypeAdapterFactory(RuntimeTypeAdapterFactory.of(SpringIndexElement.class, "_internal_node_type")
.recognizeSubtypes()
.registerSubtype(Bean.class)
.registerSubtype(AotProcessorElement.class)
.registerSubtype(BeanMethodContainerElement.class)
.registerSubtype(ConfigPropertyIndexElement.class)
.registerSubtype(DocumentElement.class)
.registerSubtype(EventListenerIndexElement.class)
.registerSubtype(EventPublisherIndexElement.class)
.registerSubtype(ProjectElement.class)
.registerSubtype(QueryMethodIndexElement.class)
.registerSubtype(RequestMappingIndexElement.class)
.registerSubtype(WebfluxRouteElementRangesIndexElement.class)
.registerSubtype(AbstractSpringIndexElement.class))
.create();
}
@@ -635,111 +656,5 @@ public class IndexCacheOnDiscDeltaBased implements IndexCache {
}
}
private static class BeanJsonAdapter implements JsonSerializer<Bean>, JsonDeserializer<Bean> {
@Override
public Bean deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
JsonObject parsedObject = json.getAsJsonObject();
String beanName = parsedObject.get("name").getAsString();
String beanType = parsedObject.get("type").getAsString();
JsonElement locationObject = parsedObject.get("location");
Location location = context.deserialize(locationObject, Location.class);
JsonElement injectionPointObject = parsedObject.get("injectionPoints");
InjectionPoint[] injectionPoints = context.deserialize(injectionPointObject, InjectionPoint[].class);
JsonElement supertypesObject = parsedObject.get("supertypes");
Set<String> supertypes = context.deserialize(supertypesObject, Set.class);
JsonElement annotationsObject = parsedObject.get("annotations");
AnnotationMetadata[] annotations = annotationsObject == null ? DefaultValues.EMPTY_ANNOTATIONS : context.deserialize(annotationsObject, AnnotationMetadata[].class);
JsonElement isConfigurationObject = parsedObject.get("isConfiguration");
boolean isConfiguration = context.deserialize(isConfigurationObject, boolean.class);
String symbolLabel = parsedObject.get("symbolLabel").getAsString();
JsonElement childrenObject = parsedObject.get("children");
Type childrenListType = TypeToken.getParameterized(List.class, SpringIndexElement.class).getType();
List<SpringIndexElement> children = context.deserialize(childrenObject, childrenListType);
Bean bean = new Bean(beanName, beanType, location, injectionPoints, supertypes, annotations, isConfiguration, symbolLabel);
for (SpringIndexElement springIndexElement : children) {
bean.addChild(springIndexElement);
}
return bean;
}
@Override
public JsonElement serialize(Bean src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject bean = new JsonObject();
bean.addProperty("name", src.getName());
bean.addProperty("type", src.getType());
bean.add("location", context.serialize(src.getLocation()));
bean.add("injectionPoints", context.serialize(src.getInjectionPoints()));
bean.add("supertypes", context.serialize(src.getSupertypes()));
bean.add("annotations", context.serialize(src.getAnnotations()));
bean.addProperty("isConfiguration", src.isConfiguration());
bean.addProperty("symbolLabel", src.getSymbolLabel());
Type childrenListType = TypeToken.getParameterized(List.class, SpringIndexElement.class).getType();
bean.add("children", context.serialize(src.getChildren(), childrenListType));
bean.addProperty("_internal_node_type", src.getClass().getName());
return bean;
}
}
private static class InjectionPointJsonAdapter implements JsonDeserializer<InjectionPoint> {
@Override
public InjectionPoint deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
JsonObject parsedObject = json.getAsJsonObject();
String injectionPointName = parsedObject.get("name").getAsString();
String injectionPointType = parsedObject.get("type").getAsString();
JsonElement locationObject = parsedObject.get("location");
Location location = context.deserialize(locationObject, Location.class);
JsonElement annotationsObject = parsedObject.get("annotations");
AnnotationMetadata[] annotations = annotationsObject == null ? DefaultValues.EMPTY_ANNOTATIONS : context.deserialize(annotationsObject, AnnotationMetadata[].class);
return new InjectionPoint(injectionPointName, injectionPointType, location, annotations);
}
}
private static class SpringIndexElementAdapter implements JsonSerializer<SpringIndexElement>, JsonDeserializer<SpringIndexElement> {
@Override
public JsonElement serialize(SpringIndexElement element, Type typeOfSrc, JsonSerializationContext context) {
JsonElement elem = context.serialize(element);
elem.getAsJsonObject().addProperty("_internal_node_type", element.getClass().getName());
return elem;
}
@Override
public SpringIndexElement deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
String typeName = jsonObject.get("_internal_node_type").getAsString();
try {
return context.deserialize(jsonObject, (Class<?>) Class.forName(typeName));
} catch (ClassNotFoundException e) {
throw new JsonParseException(e);
}
}
}
}

View File

@@ -1,27 +1,15 @@
package org.springframework.ide.vscode.boot.java.commands;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
import com.google.gson.JsonElement;
public class SpringIndexCommands {
private static final String PROJECT_BEANS_CMD = "sts/spring-boot/beans";
private static final String SPRING_STRUCTURE_CMD = "sts/spring-boot/structure";
public SpringIndexCommands(SimpleLanguageServer server, SpringMetamodelIndex metamodelIndex, JavaProjectFinder projectFinder) {
server.onCommand(PROJECT_BEANS_CMD, params -> {
String projectUri = ((JsonElement) params.getArguments().get(0)).getAsString();
IJavaProject project = projectFinder.find(new TextDocumentIdentifier(projectUri)).orElse(null);
return project == null ? CompletableFuture.completedFuture(List.of())
: server.getAsync().execute(() -> metamodelIndex.getBeansOfProject(project.getElementName()));
});
server.onCommand(SPRING_STRUCTURE_CMD, params -> server.getAsync().invoke(() -> metamodelIndex.getProjects()));
}
}

View File

@@ -53,10 +53,12 @@ public class JdtCronVisitorUtils {
} else if (e instanceof TextBlock tb) {
value = tb.getLiteralValue();
}
value = value.trim();
if (value.startsWith("#{") || value.startsWith("${")) {
// Either SPEL or Property Holder
return false;
if (value != null) {
value = value.trim();
if (value.startsWith("#{") || value.startsWith("${")) {
// Either SPEL or Property Holder
return false;
}
}
return value != null;
}

View File

@@ -29,12 +29,14 @@ import org.eclipse.lsp4j.Range;
import org.junit.jupiter.api.Test;
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
import org.springframework.ide.vscode.boot.index.cache.IndexCacheOnDiscDeltaBased;
import org.springframework.ide.vscode.commons.protocol.spring.AbstractSpringIndexElement;
import org.springframework.ide.vscode.boot.java.beans.ConfigPropertyIndexElement;
import org.springframework.ide.vscode.boot.java.data.QueryMethodIndexElement;
import org.springframework.ide.vscode.commons.protocol.spring.AnnotationAttributeValue;
import org.springframework.ide.vscode.commons.protocol.spring.AnnotationMetadata;
import org.springframework.ide.vscode.commons.protocol.spring.Bean;
import org.springframework.ide.vscode.commons.protocol.spring.DefaultValues;
import org.springframework.ide.vscode.commons.protocol.spring.InjectionPoint;
import org.springframework.ide.vscode.commons.protocol.spring.ProjectElement;
import org.springframework.ide.vscode.commons.protocol.spring.SpringIndexElement;
import com.google.gson.Gson;
@@ -269,7 +271,7 @@ public class SpringMetamodelIndexTest {
assertEquals("point2", points[1].getName());
assertEquals("point2-type", points[1].getType());
assertEquals(locationForDoc1, points[1].getLocation());
assertSame(DefaultValues.EMPTY_ANNOTATIONS, points[1].getAnnotations());
assertEquals(0, points[1].getAnnotations().length);
assertTrue(deserializedBean.isTypeCompatibleWith("supertype1"));
assertTrue(deserializedBean.isTypeCompatibleWith("supertype2"));
@@ -309,7 +311,7 @@ public class SpringMetamodelIndexTest {
assertEquals("beanType", deserializedBean.getType());
assertEquals(locationForDoc1, deserializedBean.getLocation());
assertSame(DefaultValues.EMPTY_INJECTION_POINTS, deserializedBean.getInjectionPoints());
assertEquals(0, deserializedBean.getInjectionPoints().length);
}
@Test
@@ -401,7 +403,7 @@ public class SpringMetamodelIndexTest {
void testBasicSpringIndexStructure() {
Bean bean1 = new Bean("beanName1", "beanType1", locationForDoc1, emptyInjectionPoints, Set.of("supertype1", "supertype2"), emptyAnnotations, false, "symbolLabel");
SubType1 child1 = new SubType1();
ConfigPropertyIndexElement child1 = new ConfigPropertyIndexElement("prop1", "java.lang.String", null);
bean1.addChild(child1);
List<SpringIndexElement> children2 = bean1.getChildren();
@@ -413,32 +415,32 @@ public class SpringMetamodelIndexTest {
void testSpringIndexStructurePolymorphicSerialization() {
Gson gson = IndexCacheOnDiscDeltaBased.createGson();
SubType2 subNode = new SubType2();
QueryMethodIndexElement subNode = new QueryMethodIndexElement("find1", "SELECT * FROM All", null);
SubType1 node1 = new SubType1();
ConfigPropertyIndexElement node1 = new ConfigPropertyIndexElement("prop1", "java.lang.String", null);
node1.addChild(subNode);
SubType2 node2 = new SubType2();
QueryMethodIndexElement node2 = new QueryMethodIndexElement("find", "SELECT * FROM S", null);
Root root = new Root();
ProjectElement root = new ProjectElement("my-project");
root.addChild(node1);
root.addChild(node2);
String json = gson.toJson(root);
Root deserializedRoot = gson.fromJson(json, Root.class);
ProjectElement deserializedRoot = gson.fromJson(json, ProjectElement.class);
List<SpringIndexElement> children = deserializedRoot.getChildren();
assertEquals(2, children.size());
SubType1 deserializedNode1 = (SubType1) children.stream().filter(node -> node instanceof SubType1).findAny().get();
SubType2 deserializedNode2 = (SubType2) children.stream().filter(node -> node instanceof SubType2).findAny().get();
ConfigPropertyIndexElement deserializedNode1 = (ConfigPropertyIndexElement) children.stream().filter(node -> node instanceof ConfigPropertyIndexElement).findAny().get();
QueryMethodIndexElement deserializedNode2 = (QueryMethodIndexElement) children.stream().filter(node -> node instanceof QueryMethodIndexElement).findAny().get();
assertNotNull(deserializedNode1);
assertNotNull(deserializedNode2);
List<SpringIndexElement> deserializedChild2 = deserializedNode1.getChildren();
assertEquals(1, deserializedChild2.size());
assertTrue(deserializedChild2.get(0) instanceof SubType2);
assertTrue(deserializedChild2.get(0) instanceof QueryMethodIndexElement);
}
@Test
@@ -470,11 +472,11 @@ public class SpringMetamodelIndexTest {
Gson gson = IndexCacheOnDiscDeltaBased.createGson();
SubType2 childOfChild = new SubType2();
SubType1 child1 = new SubType1();
QueryMethodIndexElement childOfChild = new QueryMethodIndexElement("find1", "SELECT * FROM All", null);
ConfigPropertyIndexElement child1 = new ConfigPropertyIndexElement("prop1", "java.lang.String", null);
child1.addChild(childOfChild);
SubType2 child2 = new SubType2();
QueryMethodIndexElement child2 = new QueryMethodIndexElement("find2", "SELECT s2 FROM S", null);
Bean bean1 = new Bean("beanName1", "beanType", locationForDoc1, emptyInjectionPoints, emptySupertypes, emptyAnnotations, true, "symbolLabel");
bean1.addChild(child1);
bean1.addChild(child2);
@@ -485,14 +487,14 @@ public class SpringMetamodelIndexTest {
List<SpringIndexElement> children = deserializedBean.getChildren();
assertEquals(2, children.size());
SpringIndexElement deserializedChild1 = children.stream().filter(element -> element instanceof SubType1).findAny().get();
SpringIndexElement deserializedChild1 = children.stream().filter(element -> element instanceof ConfigPropertyIndexElement).findAny().get();
assertNotNull(deserializedChild1);
List<SpringIndexElement> childrenOfChild = deserializedChild1.getChildren();
assertEquals(1, childrenOfChild.size());
assertTrue(childrenOfChild.get(0) instanceof SubType2);
assertTrue(childrenOfChild.get(0) instanceof QueryMethodIndexElement);
SpringIndexElement deserializedChild2 = children.stream().filter(element -> element instanceof SubType2).findAny().get();
SpringIndexElement deserializedChild2 = children.stream().filter(element -> element instanceof QueryMethodIndexElement).findAny().get();
assertNotNull(deserializedChild2);
assertEquals(0, deserializedChild2.getChildren().size());
}
@@ -502,27 +504,18 @@ public class SpringMetamodelIndexTest {
Gson gson = IndexCacheOnDiscDeltaBased.createGson();
SubType1 child1 = new SubType1();
ConfigPropertyIndexElement child1 = new ConfigPropertyIndexElement("prop1", "java.lang.String", null);;
Bean bean1 = new Bean("beanName1", "beanType", locationForDoc1, emptyInjectionPoints, emptySupertypes, emptyAnnotations, true, "symbolLabel");
bean1.addChild(child1);
String serialized = gson.toJson(bean1);
Bean deserializedBean = gson.fromJson(serialized, Bean.class);
SubType2 newChild = new SubType2();
QueryMethodIndexElement newChild = new QueryMethodIndexElement("find1", "SELECT * FROM All", null);
deserializedBean.addChild(newChild);
List<SpringIndexElement> childrenAfterNewChildAdded = deserializedBean.getChildren();
assertEquals(2, childrenAfterNewChildAdded.size());
}
static class SubType1 extends AbstractSpringIndexElement {
}
static class SubType2 extends AbstractSpringIndexElement {
}
static class Root extends AbstractSpringIndexElement {
}
}