diff --git a/org.eclipse.lsp4j.debug/src/main/java/org/eclipse/lsp4j/debug/DebugProtocol.xtend b/org.eclipse.lsp4j.debug/src/main/java/org/eclipse/lsp4j/debug/DebugProtocol.xtend index 0da48cad..462bebf6 100644 --- a/org.eclipse.lsp4j.debug/src/main/java/org/eclipse/lsp4j/debug/DebugProtocol.xtend +++ b/org.eclipse.lsp4j.debug/src/main/java/org/eclipse/lsp4j/debug/DebugProtocol.xtend @@ -12,8 +12,10 @@ package org.eclipse.lsp4j.debug; +import com.google.gson.annotations.JsonAdapter import com.google.gson.annotations.SerializedName import java.util.Map +import org.eclipse.lsp4j.debug.adapters.RestartArgumentsTypeAdapter import org.eclipse.lsp4j.generator.JsonRpcData import org.eclipse.lsp4j.jsonrpc.messages.Either import org.eclipse.lsp4j.jsonrpc.validation.NonNull @@ -938,6 +940,7 @@ class RestartArguments { *

* Since 1.47 */ + @JsonAdapter(RestartArgumentsTypeAdapter) Either arguments; } diff --git a/org.eclipse.lsp4j.debug/src/main/java/org/eclipse/lsp4j/debug/adapters/RestartArgumentsTypeAdapter.java b/org.eclipse.lsp4j.debug/src/main/java/org/eclipse/lsp4j/debug/adapters/RestartArgumentsTypeAdapter.java new file mode 100644 index 00000000..a32113d0 --- /dev/null +++ b/org.eclipse.lsp4j.debug/src/main/java/org/eclipse/lsp4j/debug/adapters/RestartArgumentsTypeAdapter.java @@ -0,0 +1,45 @@ +/****************************************************************************** + * Copyright (c) 2022 itemis AG and others. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, + * or the Eclipse Distribution License v. 1.0 which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause + ******************************************************************************/ +package org.eclipse.lsp4j.debug.adapters; + +import java.util.function.Predicate; + +import org.eclipse.lsp4j.debug.AttachRequestArguments; +import org.eclipse.lsp4j.debug.LaunchRequestArguments; +import org.eclipse.lsp4j.jsonrpc.json.adapters.EitherTypeAdapter; +import org.eclipse.lsp4j.jsonrpc.json.adapters.EitherTypeAdapter.PropertyChecker; +import org.eclipse.lsp4j.jsonrpc.messages.Either; + +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.TypeAdapter; +import com.google.gson.TypeAdapterFactory; +import com.google.gson.reflect.TypeToken; + +public class RestartArgumentsTypeAdapter implements TypeAdapterFactory { + + private static final TypeToken> ELEMENT_TYPE = new TypeToken>() { + }; + + @SuppressWarnings("unchecked") + @Override + public TypeAdapter create(Gson gson, TypeToken type) { + if (!ELEMENT_TYPE.equals(type)) + return null; + Predicate leftChecker = new PropertyChecker("noDebug"); + Predicate rightChecker = leftChecker.negate(); + TypeAdapter> elementTypeAdapter = new EitherTypeAdapter<>( + gson, ELEMENT_TYPE, leftChecker, rightChecker); + return (TypeAdapter) elementTypeAdapter; + } + +} diff --git a/org.eclipse.lsp4j.debug/src/test/java/org/eclipse/lsp4j/debug/test/RestartArgumentsTypeAdapterFactoryTest.java b/org.eclipse.lsp4j.debug/src/test/java/org/eclipse/lsp4j/debug/test/RestartArgumentsTypeAdapterFactoryTest.java new file mode 100644 index 00000000..32255926 --- /dev/null +++ b/org.eclipse.lsp4j.debug/src/test/java/org/eclipse/lsp4j/debug/test/RestartArgumentsTypeAdapterFactoryTest.java @@ -0,0 +1,46 @@ +/****************************************************************************** + * Copyright (c) 2022 itemis AG and others. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, + * or the Eclipse Distribution License v. 1.0 which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause + ******************************************************************************/ +package org.eclipse.lsp4j.debug.test; + +import static org.junit.Assert.assertTrue; + +import org.eclipse.lsp4j.debug.AttachRequestArguments; +import org.eclipse.lsp4j.debug.LaunchRequestArguments; +import org.eclipse.lsp4j.debug.adapters.RestartArgumentsTypeAdapter; +import org.eclipse.lsp4j.jsonrpc.messages.Either; +import org.junit.Test; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; + +public class RestartArgumentsTypeAdapterFactoryTest { + + private static final TypeToken> ELEMENT_TYPE = new TypeToken>() { + }; + + @Test + public void test() { + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapterFactory(new RestartArgumentsTypeAdapter()); + Gson gson = builder.create(); + Either object = gson.fromJson("{\"noDebug\":true}", ELEMENT_TYPE.getType()); + assertTrue(object.toString(), object.isLeft()); + object = gson.fromJson("{\"noDebug\":true, \"__restart\":{}}", ELEMENT_TYPE.getType()); + assertTrue(object.toString(), object.isLeft()); + object = gson.fromJson("{}", ELEMENT_TYPE.getType()); + assertTrue(object.toString(), object.isRight()); + object = gson.fromJson("{\"__restart\":{\"dunno\": true}}", ELEMENT_TYPE.getType()); + assertTrue(object.toString(), object.isRight()); + } + +}