Skip to content

Commit

Permalink
fix DelegateCommandHandle to use correct JSONUtility class
Browse files Browse the repository at this point in the history
  • Loading branch information
ajm01 committed Oct 18, 2023
1 parent 6673de4 commit d10ae15
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*******************************************************************************
* Copyright (c) 2019 Red Hat Inc. 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 Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lsp4jakarta.commons.utils;

import java.util.HashMap;

import org.eclipse.lsp4j.jsonrpc.json.MessageJsonHandler;
import org.eclipse.lsp4j.jsonrpc.json.adapters.EitherTypeAdapter;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;

/**
* Utilities for working with JSON that has been converted to an Object using Gson.
*/
public class JSONUtility {

private JSONUtility() {
}

private static final Gson LSP4J_GSON = new MessageJsonHandler(new HashMap<>()).getGson();

private static final Gson EITHER_GSON = new GsonBuilder() //
.registerTypeAdapterFactory(new EitherTypeAdapter.Factory()).create();

/**
* Converts the given Object to the given class using lsp4j's GSON logic.
*
* @param <T> the class to convert the Object to
* @param object the object to convert
* @param clazz the class to convert the Object to
* @return the given Object converted to the given class using lsp4j's GSON
* logic
*/
public static <T> T toModel(Object object, Class<T> clazz) {
return toModel(getLsp4jGson(), object, clazz);
}

/**
* Converts the given Object to the given class using the given GSON instance.
*
* @param <T> the class to convert the Object to
* @param gson the gson instance to use to perform the conversion
* @param object the object to convert
* @param clazz the class to convert the Object to
* @return the given Object converted to the given class using the given GSON
* instance
*/
public static <T> T toModel(Gson gson, Object object, Class<T> clazz) {
if (object == null) {
return null;
}
if (clazz == null) {
throw new IllegalArgumentException("Class can not be null");
}
if (object instanceof JsonElement) {
return gson.fromJson((JsonElement) object, clazz);
}
if (clazz.isInstance(object)) {
return clazz.cast(object);
}
// if nothing else works, try serializing and deserializing again
return gson.fromJson(gson.toJson(object), clazz);
}

/**
* Returns a Gson instance configured similarly to the instance lsp4j uses.
*
* @return a Gson instance configured similarly to the instance lsp4j uses
*/
public static Gson getLsp4jGson() {
return LSP4J_GSON;
}

/**
* Returns a Gson instance with most of the default options, but with the
* ability to parse {@code org.eclipse.lsp4j.Either}.
*
* @return a Gson instance with most of the default options, but with the
* ability to parse {@code org.eclipse.lsp4j.Either}
*/
public static Gson getEitherGson() {
return EITHER_GSON;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.ls.core.internal.IDelegateCommandHandler;
import org.eclipse.jdt.ls.core.internal.JSONUtility;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.CodeActionContext;
Expand All @@ -47,6 +46,7 @@
import org.eclipse.lsp4jakarta.commons.JakartaJavaDiagnosticsSettings;
import org.eclipse.lsp4jakarta.commons.JavaCursorContextResult;
import org.eclipse.lsp4jakarta.commons.codeaction.CodeActionResolveData;
import org.eclipse.lsp4jakarta.commons.utils.JSONUtility;
import org.eclipse.lsp4jakarta.jdt.core.PropertiesManagerForJava;

/**
Expand Down

0 comments on commit d10ae15

Please sign in to comment.