Skip to content

Commit 3d5f848

Browse files
committed
spotbugs(RCN+REC) ChatRequest source cleanup (4 cleared)
NullAway and the package's @NullMarked declaration prove the three getters (ChatMessage.getContent, ToolCall.getArgumentsJson, ToolDefinition.getDescription) are @nonnull at compile time, so the runtime "value == null ? "" : value" ternaries and the "if (getDescription() != null)" guard at three sites in buildMessagesJson / buildToolsJson are dead. Also narrows the catch in buildToolsJson from Exception to IOException. The only checked exception MAPPER.readTree(String) can throw is JsonProcessingException, which extends IOException — narrowing is honest and removes the catch-Exception code smell. Adds: import java.io.IOException. Compatibility: - Empty-string output on a null content/arguments is impossible per NullAway, so removing the fallback never changes runtime behaviour. - The 7 tests in ChatResponseTest (including buildMessagesJsonRoundTripsToolTurns and buildToolsJsonInlinesParameterSchema, which exercise both modified code paths) stay green. SpotBugs Max+Low: - RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE: 4 -> 1 (ChatMessage.requireNonEmpty:103 remains, separate cluster) - REC_CATCH_EXCEPTION: 1 -> 0 Total jllama: 19 -> 15.
1 parent c6feef7 commit 3d5f848

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

src/main/java/net/ladenthin/llama/ChatRequest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.fasterxml.jackson.databind.ObjectMapper;
88
import com.fasterxml.jackson.databind.node.ArrayNode;
99
import com.fasterxml.jackson.databind.node.ObjectNode;
10+
import java.io.IOException;
1011
import java.util.ArrayList;
1112
import java.util.Collections;
1213
import java.util.List;
@@ -167,7 +168,7 @@ public String buildMessagesJson() {
167168
for (ChatMessage m : messages) {
168169
ObjectNode obj = MAPPER.createObjectNode();
169170
obj.put("role", m.getRole());
170-
obj.put("content", m.getContent() == null ? "" : m.getContent());
171+
obj.put("content", m.getContent());
171172
final String toolCallId = m.getToolCallId();
172173
if (toolCallId != null) {
173174
obj.put("tool_call_id", toolCallId);
@@ -180,7 +181,7 @@ public String buildMessagesJson() {
180181
entry.put("type", "function");
181182
ObjectNode fn = MAPPER.createObjectNode();
182183
fn.put("name", call.getName());
183-
fn.put("arguments", call.getArgumentsJson() == null ? "" : call.getArgumentsJson());
184+
fn.put("arguments", call.getArgumentsJson());
184185
entry.set("function", fn);
185186
tc.add(entry);
186187
}
@@ -204,10 +205,10 @@ public Optional<String> buildToolsJson() {
204205
entry.put("type", "function");
205206
ObjectNode fn = MAPPER.createObjectNode();
206207
fn.put("name", t.getName());
207-
if (t.getDescription() != null) fn.put("description", t.getDescription());
208+
fn.put("description", t.getDescription());
208209
try {
209210
fn.set("parameters", MAPPER.readTree(t.getParametersSchemaJson()));
210-
} catch (Exception e) {
211+
} catch (IOException e) {
211212
fn.put("parameters", t.getParametersSchemaJson());
212213
}
213214
entry.set("function", fn);

0 commit comments

Comments
 (0)