Skip to content

Commit

Permalink
Merge pull request #536 from aparnamichael/issue336_getData_Null_exce…
Browse files Browse the repository at this point in the history
…ption

Fixes #416 - Quick-fix fails due to getData() is null
  • Loading branch information
turkeylurkey authored Nov 14, 2024
2 parents ab60bdc + 4f99565 commit 919bb3f
Showing 1 changed file with 35 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2023 Red Hat Inc. and others.
* Copyright (c) 2019, 2024 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
Expand All @@ -15,6 +15,7 @@

import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import org.eclipse.lsp4j.CodeActionContext;
Expand All @@ -24,7 +25,7 @@
import org.eclipse.lsp4j.TextDocumentIdentifier;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonArray;

/**
* Arguments utilities.
Expand All @@ -49,6 +50,8 @@ public class ArgumentUtils {
private static final String LINE_PROPERTY = "line";
private static final String URI_PROPERTY = "uri";

private static final Logger LOGGER = Logger.getLogger(ArgumentUtils.class.getName());

public static Map<String, Object> getFirst(List<Object> arguments) {
return arguments.isEmpty() ? null : (Map<String, Object>) arguments.get(0);
}
Expand Down Expand Up @@ -108,15 +111,20 @@ public static CodeActionContext getCodeActionContext(Map<String, Object> obj, St
}
List<Map<String, Object>> diagnosticsObj = (List<Map<String, Object>>) contextObj.get(DIAGNOSTICS_PROPERTY);
List<Diagnostic> diagnostics = diagnosticsObj.stream().map(diagnosticObj -> {
LOGGER.info("Received diagnostic data" + diagnosticObj.toString());
Diagnostic diagnostic = new Diagnostic();
diagnostic.setRange(getRange(diagnosticObj, RANGE_PROPERTY));
diagnostic.setCode(getString(diagnosticObj, CODE_PROPERTY));
diagnostic.setMessage(getString(diagnosticObj, MESSAGE_PROPERTY));
diagnostic.setSource(getString(diagnosticObj, SOURCE_PROPERTY));
// In Eclipse IDE (LSP client), the data is JsonObject, and in JDT-LS (ex :
// vscode as LSP client) the data is a Map, we
// convert the Map to a JsonObject to be consistent with any LSP clients.
diagnostic.setData(getObjectAsJson(diagnosticObj, DATA_PROPERTY));
// vscode as LSP client) the data is a Map.

// In Vscode we are sending data in two different formats either as a string or
// as an array of strings. eg: data = “AssertTrue” or data =[“ApplicationScoped", "RequestScoped”]
// if it is a string -> set data as an Object.
// if ite is an array of strings - > set data as an JsonArray
diagnostic.setData(getValueFromDataParameter(diagnosticObj, DATA_PROPERTY));
return diagnostic;
}).collect(Collectors.toList());
List<String> only = null;
Expand All @@ -139,20 +147,32 @@ public static Map<String, Object> getObject(Map<String, Object> obj, String key)
}

/**
* Returns the child as a JsonObject if it exists and is an object, and null
* otherwise
* Returns the child as a JSON array if the data parameter contains an array of strings; otherwise,
* returns it as an object if present, or null if not.
*
* @param obj the object to get the child of
*
* @param data the object to get the child of
* @param key the key of the child
* @return the child as a JsonObject if it exists and is an object, and null
* otherwise
* @return Returns the child as a JSON array if the data parameter contains an array of strings; otherwise,
* returns null.
*/
public static JsonObject getObjectAsJson(Map<String, Object> obj, String key) {
Object child = obj.get(key);
if (child != null && child instanceof Map<?, ?>) {
public static Object getValueFromDataParameter(Map<String, Object> data, String key) {

Object child = data.get(key);
if (child instanceof String) {
// if the value in the 'data' is a string, we string the object.
// eg: data = “AssertTrue”
return child;
} else if (child instanceof List<?>) {
// if the value in the 'data' is an array, we will convert it to an JsonArray.
// eg: data =[“ApplicationScoped", "RequestScoped”]
Gson gson = new Gson();
return (JsonObject) gson.toJsonTree(child);
JsonArray jsonArray = gson.toJsonTree(child).getAsJsonArray();
return jsonArray;
} else {
// Returns null if it is in any other format.
return null;
}
return null;

}
}

0 comments on commit 919bb3f

Please sign in to comment.