Index Elements serialization. Structure view initial work

* WIP

Signed-off-by: aboyko <alex.boyko@broadcom.com>

* `SpringIndexElement` serialization. Initial Structure view in VSCode

Signed-off-by: aboyko <alex.boyko@broadcom.com>

* Corrections

Signed-off-by: aboyko <alex.boyko@broadcom.com>

* Serialization polishing

Signed-off-by: aboyko <alex.boyko@broadcom.com>

* Don't include java.lang.Object in supertypes serialization

Signed-off-by: aboyko <alex.boyko@broadcom.com>

---------

Signed-off-by: aboyko <alex.boyko@broadcom.com>
This commit is contained in:
Alex Boyko
2025-04-22 13:43:50 -04:00
committed by GitHub
parent 515f27d556
commit d886efd66a
17 changed files with 884 additions and 151 deletions

View File

@@ -24,6 +24,7 @@ import java.nio.channels.Channels;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.function.Consumer;
import java.util.function.Function;
import org.eclipse.lsp4j.jsonrpc.Launcher;
@@ -38,6 +39,8 @@ import org.springframework.ide.vscode.commons.languageserver.config.LanguageServ
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
import org.springframework.ide.vscode.commons.protocol.STS4LanguageClient;
import com.google.gson.GsonBuilder;
/**
* A CommandLineRunner that launches a language server. This meant to be used as a Spring bean
* in a SpringBoot app.
@@ -98,11 +101,14 @@ public class LanguageServerRunner implements CommandLineRunner {
private Function<MessageConsumer, MessageConsumer> messageConsumer;
public LanguageServerRunner(LanguageServerProperties properties, SimpleLanguageServer languageServer, Function<MessageConsumer, MessageConsumer> messageConsumer) {
private Consumer<GsonBuilder> configureGson;
public LanguageServerRunner(LanguageServerProperties properties, SimpleLanguageServer languageServer, Function<MessageConsumer, MessageConsumer> messageConsumer, Consumer<GsonBuilder> configureGson) {
super();
this.properties = properties;
this.languageServer = languageServer;
this.messageConsumer = messageConsumer;
this.configureGson = configureGson;
}
public void start() throws Exception {
@@ -207,7 +213,7 @@ public class LanguageServerRunner implements CommandLineRunner {
AsynchronousSocketChannel socketChannel = serverSocket.accept().get();
log.info("Client connected via socket");
return Launcher.createIoLauncher(localService, remoteInterface, Channels.newInputStream(socketChannel),
Channels.newOutputStream(socketChannel), executorService, wrapper);
Channels.newOutputStream(socketChannel), executorService, wrapper, configureGson);
}
private static Connection connectToNode() throws IOException {
@@ -235,12 +241,13 @@ public class LanguageServerRunner implements CommandLineRunner {
private Future<Void> runAsync(Connection connection) throws Exception {
LanguageServer server = this.languageServer;
ExecutorService executor = createServerThreads();
Launcher<STS4LanguageClient> launcher = Launcher.createLauncher(server,
Launcher<STS4LanguageClient> launcher = Launcher.createIoLauncher(server,
STS4LanguageClient.class,
connection.in,
connection.out,
executor,
messageConsumer
messageConsumer,
configureGson
);
if (server instanceof LanguageClientAware) {

View File

@@ -0,0 +1,358 @@
/*
* Copyright (C) 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ide.vscode.commons;
import java.io.IOException;
import java.lang.reflect.AccessFlag;
import java.util.LinkedHashMap;
import java.util.Map;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
/**
* Adapts values whose runtime type may differ from their declaration type. This
* is necessary when a field's type is not the same type that GSON should create
* when deserializing that field. For example, consider these types:
*
* <pre>{@code
* abstract class Shape {
* int x;
* int y;
* }
*
* class Circle extends Shape {
* int radius;
* }
*
* class Rectangle extends Shape {
* int width;
* int height;
* }
*
* class Diamond extends Shape {
* int width;
* int height;
* }
*
* class Drawing {
* Shape bottomShape;
* Shape topShape;
* }
* }</pre>
*
* <p>
* Without additional type information, the serialized JSON is ambiguous. Is the
* bottom shape in this drawing a rectangle or a diamond?
*
* <pre>{@code
* {
* "bottomShape": {
* "width": 10,
* "height": 5,
* "x": 0,
* "y": 0
* },
* "topShape": {
* "radius": 2,
* "x": 4,
* "y": 1
* }
* }
* }</pre>
*
* This class addresses this problem by adding type information to the
* serialized JSON and honoring that type information when the JSON is
* deserialized:
*
* <pre>{@code
* {
* "bottomShape": {
* "type": "Diamond",
* "width": 10,
* "height": 5,
* "x": 0,
* "y": 0
* },
* "topShape": {
* "type": "Circle",
* "radius": 2,
* "x": 4,
* "y": 1
* }
* }
* }</pre>
*
* Both the type field name ({@code "type"}) and the type labels
* ({@code "Rectangle"}) are configurable.
*
* <h2>Registering Types</h2>
*
* Create a {@code RuntimeTypeAdapterFactory} by passing the base type and type
* field name to the {@link #of} factory method. If you don't supply an explicit
* type field name, {@code "type"} will be used.
*
* <pre>{@code
* RuntimeTypeAdapterFactory<Shape> shapeAdapterFactory = RuntimeTypeAdapterFactory.of(Shape.class, "type");
* }</pre>
*
* Next register all of your subtypes. Every subtype must be explicitly
* registered. This protects your application from injection attacks. If you
* don't supply an explicit type label, the type's simple name will be used.
*
* <pre>{@code
* shapeAdapterFactory.registerSubtype(Rectangle.class, "Rectangle");
* shapeAdapterFactory.registerSubtype(Circle.class, "Circle");
* shapeAdapterFactory.registerSubtype(Diamond.class, "Diamond");
* }</pre>
*
* Finally, register the type adapter factory in your application's GSON
* builder:
*
* <pre>{@code
* Gson gson = new GsonBuilder().registerTypeAdapterFactory(shapeAdapterFactory).create();
* }</pre>
*
* Like {@code GsonBuilder}, this API supports chaining:
*
* <pre>{@code
* RuntimeTypeAdapterFactory<Shape> shapeAdapterFactory = RuntimeTypeAdapterFactory.of(Shape.class)
* .registerSubtype(Rectangle.class).registerSubtype(Circle.class).registerSubtype(Diamond.class);
* }</pre>
*
* <h2>Serialization and deserialization</h2>
*
* In order to serialize and deserialize a polymorphic object, you must specify
* the base type explicitly.
*
* <pre>{@code
* Diamond diamond = new Diamond();
* String json = gson.toJson(diamond, Shape.class);
* }</pre>
*
* And then:
*
* <pre>{@code
* Shape shape = gson.fromJson(json, Shape.class);
* }</pre>
*/
public final class RuntimeTypeAdapterFactory<T> implements TypeAdapterFactory {
private final Class<?> baseType;
private final String typeFieldName;
private final Map<String, Class<?>> labelToSubtype = new LinkedHashMap<>();
private final Map<Class<?>, String> subtypeToLabel = new LinkedHashMap<>();
private final boolean maintainType;
private boolean recognizeSubtypes;
private RuntimeTypeAdapterFactory(Class<?> baseType, String typeFieldName, boolean maintainType) {
if (typeFieldName == null || baseType == null) {
throw new NullPointerException();
}
this.baseType = baseType;
this.typeFieldName = typeFieldName;
this.maintainType = maintainType;
}
/**
* Creates a new runtime type adapter for {@code baseType} using
* {@code typeFieldName} as the type field name. Type field names are case
* sensitive.
*
* @param maintainType true if the type field should be included in deserialized
* objects
*/
public static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType, String typeFieldName, boolean maintainType) {
return new RuntimeTypeAdapterFactory<>(baseType, typeFieldName, maintainType);
}
/**
* Creates a new runtime type adapter for {@code baseType} using
* {@code typeFieldName} as the type field name. Type field names are case
* sensitive.
*/
public static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType, String typeFieldName) {
return new RuntimeTypeAdapterFactory<>(baseType, typeFieldName, false);
}
/**
* Creates a new runtime type adapter for {@code baseType} using {@code "type"}
* as the type field name.
*/
public static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType) {
return new RuntimeTypeAdapterFactory<>(baseType, "type", false);
}
/**
* Ensures that this factory will handle not just the given {@code baseType},
* but any subtype of that type.
*/
@CanIgnoreReturnValue
public RuntimeTypeAdapterFactory<T> recognizeSubtypes() {
this.recognizeSubtypes = true;
return this;
}
/**
* Registers {@code type} identified by {@code label}. Labels are case
* sensitive.
*
* @throws IllegalArgumentException if either {@code type} or {@code label} have
* already been registered on this type
* adapter.
*/
@CanIgnoreReturnValue
public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type, String label) {
if (type == null || label == null) {
throw new NullPointerException();
}
if (subtypeToLabel.containsKey(type) || labelToSubtype.containsKey(label)) {
throw new IllegalArgumentException("types and labels must be unique");
}
labelToSubtype.put(label, type);
subtypeToLabel.put(type, label);
return this;
}
/**
* Registers {@code type} identified by its {@link Class#getSimpleName simple
* name}. Labels are case sensitive.
*
* @throws IllegalArgumentException if either {@code type} or its simple name
* have already been registered on this type
* adapter.
*/
@CanIgnoreReturnValue
public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type) {
return registerSubtype(type, type.getSimpleName());
}
@Override
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
if (type == null) {
return null;
}
Class<?> rawType = type.getRawType();
boolean handle = recognizeSubtypes ? baseType.isAssignableFrom(rawType) : baseType.equals(rawType);
if (!handle) {
return null;
}
TypeAdapter<JsonElement> jsonElementAdapter = gson.getAdapter(JsonElement.class);
Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<>();
Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<>();
for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) {
TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue()));
labelToDelegate.put(entry.getKey(), delegate);
subtypeToDelegate.put(entry.getValue(), delegate);
}
final RuntimeTypeAdapterFactory<T> thisAdapter = this;
return new TypeAdapter<R>() {
private TypeAdapter<?> findSubtypeDelegate(Class<?> clazz) {
if (clazz.isInterface() || clazz.accessFlags().contains(AccessFlag.ABSTRACT)) {
throw new JsonParseException(
"cannot serialize/deserialize " + clazz.getName() + "; it is abstract");
}
if (baseType.isAssignableFrom(clazz)) {
TypeAdapter<?> delegate = gson.getDelegateAdapter(thisAdapter, TypeToken.get(clazz));
labelToDelegate.put(clazz.getName(), delegate);
subtypeToDelegate.put(clazz, delegate);
labelToSubtype.put(clazz.getName(), clazz);
subtypeToLabel.put(clazz, clazz.getName());
return delegate;
}
return null;
}
@SuppressWarnings("unchecked") // registration requires that subtype extends T
@Override
public R read(JsonReader in) throws IOException {
JsonElement jsonElement = jsonElementAdapter.read(in);
JsonElement labelJsonElement;
if (maintainType) {
labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName);
} else {
labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName);
}
if (labelJsonElement == null) {
throw new JsonParseException("cannot deserialize " + baseType
+ " because it does not define a field named " + typeFieldName);
}
String label = labelJsonElement.getAsString();
TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label);
if (delegate == null && recognizeSubtypes) {
try {
delegate = (TypeAdapter<R>) findSubtypeDelegate(Class.forName(label));
} catch (ClassNotFoundException e) {
// ignore
}
}
if (delegate == null) {
throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label
+ "; did you forget to register a subtype?");
}
return delegate.fromJsonTree(jsonElement);
}
@SuppressWarnings("unchecked") // registration requires that subtype extends T
@Override
public void write(JsonWriter out, R value) throws IOException {
Class<?> srcType = value.getClass();
TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType);
if (delegate == null && recognizeSubtypes) {
delegate = (TypeAdapter<R>) findSubtypeDelegate(srcType);
}
if (delegate == null) {
throw new JsonParseException(
"cannot serialize " + srcType.getName() + "; did you forget to register a subtype?");
}
JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject();
if (maintainType) {
jsonElementAdapter.write(out, jsonObject);
return;
}
JsonObject clone = new JsonObject();
if (jsonObject.has(typeFieldName)) {
throw new JsonParseException("cannot serialize " + srcType.getName()
+ " because it already defines a field named " + typeFieldName);
}
String label = subtypeToLabel.get(srcType);
clone.add(typeFieldName, new JsonPrimitive(label));
for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) {
clone.add(e.getKey(), e.getValue());
}
jsonElementAdapter.write(out, clone);
}
}.nullSafe();
}
}

View File

@@ -11,11 +11,13 @@
package org.springframework.ide.vscode.commons.protocol.spring;
import java.util.Set;
import java.util.stream.Collectors;
import org.eclipse.lsp4j.DocumentSymbol;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.SymbolKind;
import com.google.common.collect.ImmutableSet;
import com.google.gson.Gson;
public class Bean extends AbstractSpringIndexElement implements SymbolElement {
@@ -28,6 +30,7 @@ public class Bean extends AbstractSpringIndexElement implements SymbolElement {
private final AnnotationMetadata[] annotations;
private final boolean isConfiguration;
private final String symbolLabel;
private final boolean isInterface;
public Bean(
String name,
@@ -44,26 +47,25 @@ public class Bean extends AbstractSpringIndexElement implements SymbolElement {
this.location = location;
this.isConfiguration = isConfiguration;
this.symbolLabel = symbolLabel;
this.isInterface = supertypes == null || !supertypes.contains(Object.class.getName());
if (injectionPoints != null && injectionPoints.length == 0) {
this.injectionPoints = DefaultValues.EMPTY_INJECTION_POINTS;
this.injectionPoints = null;
}
else {
this.injectionPoints = injectionPoints;
}
if (supertypes != null && supertypes.size() == 0) {
this.supertypes = DefaultValues.EMPTY_SUPERTYPES;
}
else if (supertypes != null && supertypes.size() == 1 && supertypes.contains("java.lang.Object")) {
this.supertypes = DefaultValues.OBJECT_SUPERTYPE;
Set<String> sanitizedSuperTypes = supertypes == null ? null : supertypes.stream().filter(t -> !t.equals(Object.class.getName())).collect(Collectors.toUnmodifiableSet());
if (sanitizedSuperTypes != null && sanitizedSuperTypes.size() == 0) {
this.supertypes = null;
}
else {
this.supertypes = supertypes;
this.supertypes = sanitizedSuperTypes;
}
if (annotations != null && annotations.length == 0) {
this.annotations = DefaultValues.EMPTY_ANNOTATIONS;
this.annotations = null;
}
else {
this.annotations = annotations;
@@ -83,15 +85,15 @@ public class Bean extends AbstractSpringIndexElement implements SymbolElement {
}
public InjectionPoint[] getInjectionPoints() {
return injectionPoints;
return injectionPoints == null ? DefaultValues.EMPTY_INJECTION_POINTS : injectionPoints;
}
public boolean isTypeCompatibleWith(String type) {
return type != null && ((this.type != null && this.type.equals(type)) || (supertypes.contains(type)));
return type != null && ((this.type != null && this.type.equals(type)) || (supertypes != null && supertypes.contains(type)) || (Object.class.getName().equals(type) && !isInterface));
}
public AnnotationMetadata[] getAnnotations() {
return annotations;
return annotations == null ? DefaultValues.EMPTY_ANNOTATIONS : annotations;
}
public boolean isConfiguration() {
@@ -99,7 +101,11 @@ public class Bean extends AbstractSpringIndexElement implements SymbolElement {
}
public Set<String> getSupertypes() {
return supertypes;
if (supertypes == null) {
return isInterface ? DefaultValues.EMPTY_SUPERTYPES : DefaultValues.OBJECT_SUPERTYPE;
} else {
return isInterface ? supertypes : ImmutableSet.<String>builder().addAll(supertypes).add(Object.class.getName()).build();
}
}
public String getSymbolLabel() {

View File

@@ -29,8 +29,8 @@ public class InjectionPoint {
this.type = type;
this.location = location;
if (annotations == null || (annotations != null && annotations.length == 0)) {
this.annotations = DefaultValues.EMPTY_ANNOTATIONS;
if (annotations != null && annotations.length == 0) {
this.annotations = null;
}
else {
this.annotations = annotations;
@@ -50,7 +50,7 @@ public class InjectionPoint {
}
public AnnotationMetadata[] getAnnotations() {
return annotations;
return annotations == null ? DefaultValues.EMPTY_ANNOTATIONS : annotations;
}
}

View File

@@ -10,6 +10,8 @@
*******************************************************************************/
package org.springframework.ide.vscode.languageserver.starter;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import org.eclipse.lsp4j.jsonrpc.MessageConsumer;
@@ -21,6 +23,8 @@ import org.springframework.ide.vscode.commons.languageserver.config.LanguageServ
import org.springframework.ide.vscode.commons.languageserver.util.ParentProcessWatcher;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
import com.google.gson.GsonBuilder;
@AutoConfiguration
public class LanguageServerRunnerAutoConf {
@@ -35,12 +39,13 @@ public class LanguageServerRunnerAutoConf {
@ConditionalOnMissingClass("org.springframework.ide.vscode.languageserver.testharness.LanguageServerHarness")
@Bean
public LanguageServerRunner serverApp(
LanguageServerRunner serverApp(
LanguageServerProperties properties,
SimpleLanguageServer languageServerFactory,
Function<MessageConsumer, MessageConsumer> messageConsumer
Function<MessageConsumer, MessageConsumer> messageConsumer,
Optional<Consumer<GsonBuilder>> configureGson
) {
return new LanguageServerRunner(properties, languageServerFactory, messageConsumer);
return new LanguageServerRunner(properties, languageServerFactory, messageConsumer, configureGson.orElse(b -> {}));
}
}

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;
@@ -89,6 +90,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 +100,7 @@ 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.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 +121,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 +432,10 @@ public class BootLanguageServerBootApp {
return new ResponseModifier();
}
@Bean
Consumer<GsonBuilder> configureGson() {
return builder -> builder
.registerTypeAdapterFactory(RuntimeTypeAdapterFactory.of(SpringIndexElement.class, "_internal_node_type")
.recognizeSubtypes());
}
}

View File

@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2024 Broadcom, Inc.
* Copyright (c) 2024, 2025 Broadcom, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -12,6 +12,8 @@ package org.springframework.ide.vscode.boot.app;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
import org.springframework.ide.vscode.boot.java.commands.SpringIndexCommands;
import org.springframework.ide.vscode.boot.java.commands.WorkspaceBootExecutableProjects;
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
@@ -22,5 +24,10 @@ public class CommandsConfig {
@Bean WorkspaceBootExecutableProjects workspaceBootProjects(SimpleLanguageServer server, JavaProjectFinder projectFinder, SpringSymbolIndex symbolIndex) {
return new WorkspaceBootExecutableProjects(server, projectFinder, symbolIndex);
}
@Bean SpringIndexCommands springIndexCommands(SimpleLanguageServer server, JavaProjectFinder projectFinder, SpringMetamodelIndex symbolIndex) {
return new SpringIndexCommands(server, symbolIndex, projectFinder);
}
}

View File

@@ -38,13 +38,9 @@ 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.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.RuntimeTypeAdapterFactory;
import org.springframework.ide.vscode.commons.protocol.spring.SpringIndexElement;
import org.springframework.ide.vscode.commons.util.UriUtil;
@@ -569,10 +565,9 @@ 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())
.create();
}
@@ -635,111 +630,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

@@ -0,0 +1,15 @@
package org.springframework.ide.vscode.boot.java.commands;
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
public class SpringIndexCommands {
private static final String SPRING_STRUCTURE_CMD = "sts/spring-boot/structure";
public SpringIndexCommands(SimpleLanguageServer server, SpringMetamodelIndex metamodelIndex, JavaProjectFinder projectFinder) {
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

@@ -27,6 +27,8 @@ import * as springBootAgent from './copilot/springBootAgent';
import { applyLspEdit } from "./copilot/guideApply";
import { isLlmApiReady } from "./copilot/util";
import CopilotRequest, { logger } from "./copilot/copilotRequest";
import { ExplorerTreeProvider } from "./explorer/explorer-tree-provider";
import { StructureManager } from "./explorer/structure-tree-manager";
const PROPERTIES_LANGUAGE_ID = "spring-boot-properties";
const YAML_LANGUAGE_ID = "spring-boot-properties-yaml";
@@ -52,8 +54,8 @@ export function activate(context: ExtensionContext): Thenable<ExtensionAPI> {
],
checkjvm: (context: ExtensionContext, jvm: commons.JVM) => {
let version = jvm.getMajorVersion();
if (version < 17) {
throw Error(`Spring Tools Language Server requires Java 17 or higher to be launched. Current Java version is ${version}`);
if (version < 21) {
throw Error(`Spring Tools Language Server requires Java 21 or higher to be launched. Current Java version is ${version}`);
}
if (!jvm.isJdk()) {
@@ -149,7 +151,39 @@ export function activate(context: ExtensionContext): Thenable<ExtensionAPI> {
context.subscriptions.push(startDebugSupport());
return commons.activate(options, context).then(client => {
commands.registerCommand('vscode-spring-boot.ls.start', () => client.start().then(() => {
// Spring structure tree in the Explorer view
/*
Requires the following code to be added in the `package.json` to
1. Declare view:
"views": {
"explorer": [
{
"id": "explorer.spring",
"name": "Spring",
"when": "java:serverMode || workbenchState==empty",
"contextualTitle": "Spring",
"icon": "resources/logo.png"
}
]
},
2. Menu item (toolbar action) on the explorer view delegating to the command
"view/title": [
{
"command": "vscode-spring-boot.structure.refresh",
"when": "view == explorer.spring",
"group": "navigation@5"
}
],
*/
// const structureManager = new StructureManager();
// const explorerTreeProvider = new ExplorerTreeProvider(structureManager);
// context.subscriptions.push(window.createTreeView('explorer.spring', { treeDataProvider: explorerTreeProvider, showCollapseAll: true }));
// context.subscriptions.push(commands.registerCommand("vscode-spring-boot.structure.refresh", () => structureManager.refresh()));
context.subscriptions.push(commands.registerCommand('vscode-spring-boot.ls.start', () => client.start().then(() => {
// Boot LS is fully started
registerClasspathService(client);
registerJavaDataService(client);
@@ -162,8 +196,8 @@ export function activate(context: ExtensionContext): Thenable<ExtensionAPI> {
// Register TestJars launch support
context.subscriptions.push(startTestJarSupport());
}));
commands.registerCommand('vscode-spring-boot.ls.stop', () => client.stop());
})));
context.subscriptions.push(commands.registerCommand('vscode-spring-boot.ls.stop', () => client.stop()));
liveHoverUi.activate(client, options, context);
rewrite.activate(client, options, context);
setLogLevelUi.activate(client, options, context);
@@ -175,9 +209,13 @@ export function activate(context: ExtensionContext): Thenable<ExtensionAPI> {
registerMiscCommands(context);
commands.registerCommand('vscode-spring-boot.agent.apply', applyLspEdit);
context.subscriptions.push(commands.registerCommand('vscode-spring-boot.agent.apply', applyLspEdit));
return new ApiManager(client).api;
const api = new ApiManager(client).api
// context.subscriptions.push(api.getSpringIndex().onSpringIndexUpdated(e => structureManager.refresh()));
return api;
});
}

View File

@@ -1,4 +1,4 @@
import { Event } from "vscode";
import { Event, Uri } from "vscode";
import { LanguageClient } from "vscode-languageclient/node";
import { LiveProcess } from "./notification";
import {Location} from "vscode-languageclient";

View File

@@ -1,6 +1,6 @@
import { commands, Uri } from "vscode";
import { commands } from "vscode";
import { Emitter, LanguageClient } from "vscode-languageclient/node";
import {Bean, BeansParams, ExtensionAPI, SpringIndex} from "./api";
import {Bean, BeansParams, ExtensionAPI} from "./api";
import {
LiveProcess,
LiveProcessConnectedNotification,

View File

@@ -0,0 +1,40 @@
import { CancellationToken, commands, Event, EventEmitter, ProviderResult, TreeDataProvider, TreeItem, TreeItemCollapsibleState } from "vscode";
import { StructureManager } from "./structure-tree-manager";
import { DocumentNode, ProjectNode, SpringNode } from "./nodes";
import * as Path from "path";
export class ExplorerTreeProvider implements TreeDataProvider<SpringNode> {
private emitter: EventEmitter<undefined | SpringNode | SpringNode[]>;
public readonly onDidChangeTreeData: Event<undefined | SpringNode | SpringNode[]>;
constructor(private manager: StructureManager) {
this.emitter = new EventEmitter<undefined | SpringNode | SpringNode[]>();
this.onDidChangeTreeData = this.emitter.event;
this.manager.onDidChange(e => this.emitter.fire(e));
}
getTreeItem(element: SpringNode): TreeItem | Thenable<TreeItem> {
return element.getTreeItem();
}
getChildren(element?: SpringNode): ProviderResult<SpringNode[]> {
if (element) {
return element.children;
}
return this.getRootElements();
}
getRootElements(): ProviderResult<SpringNode[]> {
return this.manager.rootElements;
}
// getParent?(element: SpringNode): ProviderResult<SpringNode> {
// throw new Error("Method not implemented.");
// }
// resolveTreeItem?(item: TreeItem, element: SpringNode, token: CancellationToken): ProviderResult<TreeItem> {
// throw new Error("Method not implemented.");
// }
}

View File

@@ -0,0 +1,222 @@
import { ThemeIcon, TreeItem, TreeItemCollapsibleState, Uri } from "vscode";
import { Location, Range } from "vscode-languageclient";
export class SpringNode {
constructor(readonly children: SpringNode[]) {}
getTreeItem(): TreeItem {
return new TreeItem("<node>", this.computeState(TreeItemCollapsibleState.Expanded));
}
computeState(defaultState: TreeItemCollapsibleState.Collapsed | TreeItemCollapsibleState.Expanded): TreeItemCollapsibleState {
return Array.isArray(this.children) && this.children.length ? defaultState : TreeItemCollapsibleState.None;
}
}
export class ProjectNode extends SpringNode {
constructor(readonly name: string, children: SpringNode[]) {
super(children);
}
getTreeItem(): TreeItem {
const item = super.getTreeItem();
item.label = this.name;
return item;
}
}
export class DocumentNode extends SpringNode {
constructor(readonly docURI: Uri, children: SpringNode[]) {
super(children);
}
getTreeItem(): TreeItem {
const item = super.getTreeItem();
item.label = undefined; // let VSCode derive the label from the resource URI
item.resourceUri = this.docURI;
item.iconPath = ThemeIcon.File;
return item;
}
}
export class AotProcessorNode extends SpringNode {
constructor(
children: SpringNode[],
readonly type: string,
readonly docUri: Uri
) {
super(children);
}
getTreeItem(): TreeItem {
const item = super.getTreeItem();
item.label = this.type;
item.resourceUri = this.docUri;
return item;
}
}
export class BeanMethodContainerNode extends SpringNode {
constructor(
children: SpringNode[],
readonly type: string,
readonly location: Location,
) {
super(children);
}
getTreeItem(): TreeItem {
const item = super.getTreeItem();
item.label = this.type;
return item;
}
}
export class BeanRegistrarNode extends SpringNode {
constructor(
children: SpringNode[],
readonly name: string,
readonly type: string,
readonly location: Location,
) {
super(children);
}
getTreeItem(): TreeItem {
const item = super.getTreeItem();
item.label = this.name;
return item;
}
}
export class ConfigPropertyNode extends SpringNode {
constructor(
children: SpringNode[],
readonly name: string,
readonly type: string,
readonly range: Range
) {
super(children);
}
getTreeItem(): TreeItem {
const item = super.getTreeItem();
item.label = this.name;
return item;
}
}
export class EventListenerNode extends SpringNode {
constructor(
children: SpringNode[],
readonly eventType: string,
readonly location: Location,
readonly containerBeanType: string,
readonly annotations: AnnotationMetadata[]
) {
super(children);
}
getTreeItem(): TreeItem {
const item = super.getTreeItem();
item.label = this.eventType;
return item;
}
}
export class EventPublisherNode extends SpringNode {
constructor(
children: SpringNode[],
readonly eventType: string,
readonly location: Location,
readonly eventTypesFromHierarchy: string[]
) {
super(children);
}
getTreeItem(): TreeItem {
const item = super.getTreeItem();
item.label = this.eventType;
return item;
}
}
export class QueryMethodNode extends SpringNode {
constructor(
children: SpringNode[],
readonly methodName: string,
readonly queryString: string,
readonly range: Range
) {
super(children);
}
getTreeItem(): TreeItem {
const item = super.getTreeItem();
item.label = this.methodName;
return item;
}
}
export class RequestMappingNode extends SpringNode {
constructor(
children: SpringNode[],
readonly path: string,
readonly httpMethods: string[],
readonly contentTypes: string[],
readonly acceptTypes: string[],
readonly symbolLabel: string,
readonly range: Range
) {
super(children);
}
getTreeItem(): TreeItem {
const item = super.getTreeItem();
item.label = this.path;
return item;
}
}
export class WebfluxRoutesNode extends RequestMappingNode {
constructor(
children: SpringNode[],
path: string,
httpMethods: string[],
contentTypes: string[],
acceptTypes: string[],
symbolLabel: string,
range: Range,
readonly ranges: Range[]
) {
super(children, path, httpMethods, contentTypes, acceptTypes, symbolLabel, range);
}
}
export class BeanNode extends SpringNode {
constructor(
children: SpringNode[],
readonly name: string,
readonly type: string,
readonly location: Location,
readonly injectionPoints: InjectionPoint[],
readonly supertypes: string[],
readonly annotations: AnnotationMetadata[],
readonly isConfiguration: boolean,
readonly symbolLabel: string
) {
super(children);
}
getTreeItem(): TreeItem {
const item = super.getTreeItem();
item.label = this.name;
return item;
}
}
export interface InjectionPoint {
readonly name: string;
readonly type: string;
readonly location: Location;
readonly annotations: AnnotationMetadata[];
}
export interface AnnotationMetadata {
readonly annotationType: string;
readonly isMetaAnnotation: boolean;
readonly location: Location;
readonly attributes: {[key: string]: AnnotationAttributeValue[]};
}
export interface AnnotationAttributeValue {
readonly name: string;
readonly location: Location;
}

View File

@@ -0,0 +1,124 @@
import { commands, EventEmitter, Event, Uri } from "vscode";
import { AotProcessorNode, BeanMethodContainerNode, BeanNode, BeanRegistrarNode, ConfigPropertyNode, DocumentNode, EventListenerNode, EventPublisherNode, ProjectNode, QueryMethodNode, RequestMappingNode, SpringNode, WebfluxRoutesNode } from "./nodes";
const SPRING_STRUCTURE_CMD = "sts/spring-boot/structure";
export class StructureManager {
private _rootElements: Thenable<SpringNode[]>
private _onDidChange: EventEmitter<SpringNode | undefined> = new EventEmitter<SpringNode | undefined>();
get rootElements(): Thenable<SpringNode[]> {
return this._rootElements;
}
refresh(): void {
this._rootElements = commands.executeCommand(SPRING_STRUCTURE_CMD).then(json => {
const nodes = this.parseArray(json);
this._onDidChange.fire(undefined);
return nodes;
});
}
private parseNode(json: any): SpringNode | undefined {
if (typeof (json._internal_node_type) === 'string') {
switch (json._internal_node_type) {
case "org.springframework.ide.vscode.commons.protocol.spring.ProjectElement":
return new ProjectNode(json.projectName as string, this.parseArray(json.children));
case "org.springframework.ide.vscode.commons.protocol.spring.DocumentElement":
return new DocumentNode(Uri.parse(json.docURI as string), this.parseArray(json.children));
case "org.springframework.ide.vscode.commons.protocol.spring.Bean":
return new BeanNode(
this.parseArray(json.children),
json.name,
json.type,
json.location,
json.injectionPoints,
json.supertypes,
json.annotations,
json.isConfiguration,
json.symbolLabel
);
case "org.springframework.ide.vscode.commons.protocol.spring.AotProcessorElement":
return new AotProcessorNode(
this.parseArray(json.children),
json.name,
Uri.parse(json.docUri)
);
case "org.springframework.ide.vscode.commons.protocol.spring.BeanMethodContainerElement":
return new BeanMethodContainerNode(
this.parseArray(json.children),
json.type,
json.location
);
case "org.springframework.ide.vscode.commons.protocol.spring.BeanRegistrarElement":
return new BeanRegistrarNode(
this.parseArray(json.children),
json.name,
json.type,
json.location
);
case "org.springframework.ide.vscode.boot.java.beans.ConfigPropertyIndexElement":
return new ConfigPropertyNode(
this.parseArray(json.children),
json.name,
json.type,
json.range
);
case "org.springframework.ide.vscode.boot.java.events.EventListenerIndexElement":
return new EventListenerNode(
this.parseArray(json.children),
json.eventType,
json.location,
json.containerBeanType,
json.annotations
);
case "org.springframework.ide.vscode.boot.java.events.EventPublisherIndexElement":
return new EventPublisherNode(
this.parseArray(json.children),
json.eventType,
json.location,
json.eventTypesFromHierarchy,
);
case "org.springframework.ide.vscode.boot.java.data.QueryMethodIndexElement":
return new QueryMethodNode(
this.parseArray(json.children),
json.methodName,
json.queryString,
json.range
);
case "org.springframework.ide.vscode.boot.java.requestmapping.RequestMappingIndexElement":
return new RequestMappingNode(
this.parseArray(json.children),
json.path,
json.httpMethods,
json.contentTypes,
json.acceptTypes,
json.symbolLabel,
json.range
);
case "org.springframework.ide.vscode.boot.java.requestmapping.WebfluxRouteElementRangesIndexElement":
return new WebfluxRoutesNode(
this.parseArray(json.children),
json.path,
json.httpMethods,
json.contentTypes,
json.acceptTypes,
json.symbolLabel,
json.range,
json.ranges
);
}
}
}
private parseArray(json: any): SpringNode[] {
return Array.isArray(json) ? (json as []).map(j => this.parseNode(j)).filter(e => !!e) : [];
}
public get onDidChange(): Event<SpringNode | undefined> {
return this._onDidChange.event;
}
}

View File

@@ -144,6 +144,10 @@
}
],
"commandPalette": [
{
"command": "vscode-spring-boot.structure.refresh",
"when": "false"
},
{
"command": "vscode-spring-boot.props-to-yaml",
"when": "false"
@@ -184,6 +188,12 @@
]
},
"commands": [
{
"command": "vscode-spring-boot.structure.refresh",
"title": "Refresh",
"category": "Spring Boot",
"icon": "$(refresh)"
},
{
"command": "vscode-spring-boot.live-hover.connect",
"title": "Show/Refresh/Hide Live Data from Spring Boot Processes",