Skip to content

Commit

Permalink
Merge pull request #43605 from dulajdilshan/flow_model-update
Browse files Browse the repository at this point in the history
Fix SpotBugs warnings and errors
  • Loading branch information
KavinduZoysa authored Nov 19, 2024
2 parents 71200f7 + 57fd2cf commit fe8bc64
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@
package io.ballerina.compiler.syntax.tree;

import io.ballerina.compiler.internal.parser.tree.STNode;
import io.ballerina.compiler.internal.parser.tree.STNodeDiagnostic;
import io.ballerina.compiler.internal.parser.tree.STToken;
import io.ballerina.tools.diagnostics.Diagnostic;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
import io.ballerina.compiler.internal.parser.tree.STNodeList;
import io.ballerina.compiler.internal.syntax.NodeListUtils;

import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
Expand Down
6 changes: 6 additions & 0 deletions language-server/modules/langserver-core/spotbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@
<Match>
<Class name="org.ballerinalang.langserver.extensions.ballerina.document.BallerinaServiceListResponse" />
</Match>
<Match>
<Class name="org.ballerinalang.langserver.extensions.ballerina.runner.BallerinaRunnerUtil"/>
<OR>
<Bug pattern="BC_UNCONFIRMED_CAST"/>
</OR>
</Match>
<Match>
<Class name="org.ballerinalang.langserver.hover.HoverUtil" />
<OR>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -70,26 +71,45 @@
public class BallerinaTriggerService implements ExtendedLanguageServerService {
private LanguageClient languageClient;
private final Map<String, InBuiltTrigger> inBuiltTriggers;
private static final Type MAP_TYPE = MapTypeToken.TYPE;


// Represents the in-built trigger basic information
public record InBuiltTrigger(String name, String orgName, String packageName, List<String> keywords) {
public InBuiltTrigger(String name, String orgName, String packageName, List<String> keywords) {
this.name = name;
this.orgName = orgName;
this.packageName = packageName;
// Make a defensive copy of the list to ensure immutability
this.keywords = keywords == null ? List.of() : new ArrayList<>(keywords);
}

@Override
public List<String> keywords() {
// Return an unmodifiable or defensive copy, as needed
return List.copyOf(keywords);
}
}

public BallerinaTriggerService() {
InputStream propertiesStream = getClass().getClassLoader()
.getResourceAsStream("inbuilt-triggers/properties.json");
Type mapType = new TypeToken<Map<String, InBuiltTrigger>>() {}.getType();
Map<String, InBuiltTrigger> triggers = Map.of();
if (propertiesStream != null) {
try (JsonReader reader = new JsonReader(new InputStreamReader(propertiesStream, StandardCharsets.UTF_8))) {
triggers = new Gson().fromJson(reader, mapType);
triggers = new Gson().fromJson(reader, MAP_TYPE);
} catch (IOException e) {
// Ignore
}
}
this.inBuiltTriggers = triggers;
}

// Static inner class to hold the type token
private static class MapTypeToken {
private static final Type TYPE = new TypeToken<Map<String, InBuiltTrigger>>() { }.getType();
}

@Override
public void init(LanguageServer langServer, WorkspaceManager workspaceManager,
LanguageServerContext serverContext) {
Expand Down Expand Up @@ -230,7 +250,8 @@ private Optional<JsonObject> getInBuiltTriggerJson(String triggerName) {
.noneMatch(inBuiltTrigger -> inBuiltTrigger.name().equals(triggerName))) {
return Optional.empty();
}
InputStream resourceStream = getClass().getClassLoader().getResourceAsStream(String.format("inbuilt-triggers/%s.json", triggerName));
InputStream resourceStream = getClass().getClassLoader().getResourceAsStream(
String.format("inbuilt-triggers/%s.json", triggerName));
if (resourceStream == null) {
String msg = String.format("Trigger info file not found for the trigger: %s", triggerName);
this.languageClient.logMessage(new MessageParams(MessageType.Error, msg));
Expand Down

0 comments on commit fe8bc64

Please sign in to comment.