Skip to content

Commit 824ee27

Browse files
Fix/langserver json primitive casts check (#709)
* Adds jsonNull check before cast with JsonPrimitive * Adds comments for getJsonStringFromConfigList.
1 parent e81a69a commit 824ee27

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

language-server/langserver/src/main/java/edu/umn/cs/melt/silver/langserver/SilverLanguageService.java

+20-3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.eclipse.lsp4j.services.WorkspaceService;
5959

6060
import com.google.gson.JsonPrimitive;
61+
import com.google.gson.JsonElement;
6162

6263
import common.ConsCell;
6364
import common.DecoratedNode;
@@ -248,6 +249,19 @@ private void setParserFactory(Supplier<SilverCopperParser<NRoot>> parserFactory)
248249
parserFn = new CopperParserNodeFactory(parserFactory);
249250
}
250251

252+
// This method is meant to deal with jsonNull, an element that is technically
253+
// not null according to java but is a null element passed through json.
254+
// It gets a string from a list containing json elements and returns "" if the element
255+
// is null.
256+
private String getJsonStringFromConfigList(List<Object> configs, int index){
257+
Object itemGet = configs.get(index);
258+
if (itemGet != null && !((JsonElement)itemGet).isJsonNull()) {
259+
return ((JsonPrimitive)itemGet).getAsString();
260+
} else {
261+
return "";
262+
}
263+
}
264+
251265
private CompletableFuture<Void> reloadParser() {
252266
ConfigurationItem compilerJarConfigItem = new ConfigurationItem();
253267
compilerJarConfigItem.setSection("silver.compilerJar");
@@ -258,8 +272,8 @@ private CompletableFuture<Void> reloadParser() {
258272
return client.configuration(configParams).thenAccept((configs) -> {
259273
String oldParserJar = compilerJar;
260274
String oldParserName = parserName;
261-
compilerJar = ((JsonPrimitive)configs.get(0)).getAsString();
262-
parserName = ((JsonPrimitive)configs.get(1)).getAsString();
275+
compilerJar = this.getJsonStringFromConfigList(configs, 0);
276+
parserName = this.getJsonStringFromConfigList(configs, 1);
263277
if (compilerJar.isEmpty() || parserName.isEmpty()) {
264278
if (!compilerJar.equals(oldParserJar) || !parserName.equals(oldParserName)) {
265279
setParserFactory(Parser_silver_compiler_composed_Default_svParse::new);
@@ -375,7 +389,10 @@ private void doBuild(Map<String, Integer> buildVersions) {
375389
ConfigurationParams configParams = new ConfigurationParams(List.of(enableMWDAConfigItem));
376390
boolean newEnableMWDA = enableMWDA;
377391
try {
378-
newEnableMWDA = ((JsonPrimitive)client.configuration(configParams).get().get(0)).getAsBoolean();
392+
Object configMWDAGet = client.configuration(configParams).get().get(0);
393+
if (configMWDAGet != null && !((JsonElement)configMWDAGet).isJsonNull()) {
394+
newEnableMWDA = ((JsonPrimitive)configMWDAGet).getAsBoolean();
395+
}
379396
} catch (InterruptedException | ExecutionException e) {
380397
// Ignore, getting the settings sometimes fails when a build is triggered during initialization
381398
}

0 commit comments

Comments
 (0)