From a2295a3320aa23c43228a8db9166dede07919004 Mon Sep 17 00:00:00 2001 From: Michael Vorburger Date: Tue, 7 Oct 2025 21:00:39 +0200 Subject: [PATCH] fix: Make FunctionTool slightly more null safe, and use Text Blocks This is in anticipation of doing more work in this class in the context of https://github.com/google/adk-java/issues/473; it just makes it easier to working on and raising more changes, and these automagic changes propose by VSC actually do make sense, instead of having to "fight your IDE" to undo this when working on this class. --- .../com/google/adk/tools/FunctionTool.java | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/com/google/adk/tools/FunctionTool.java b/core/src/main/java/com/google/adk/tools/FunctionTool.java index 3037ef40..7c4096f0 100644 --- a/core/src/main/java/com/google/adk/tools/FunctionTool.java +++ b/core/src/main/java/com/google/adk/tools/FunctionTool.java @@ -46,16 +46,18 @@ public class FunctionTool extends BaseTool { new ObjectMapper().registerModule(new Jdk8Module()); private static final Logger logger = LoggerFactory.getLogger(FunctionTool.class); - @Nullable private final Object instance; + private final @Nullable Object instance; private final Method func; private final FunctionDeclaration funcDeclaration; public static FunctionTool create(Object instance, Method func) { if (!areParametersAnnotatedWithSchema(func) && wasCompiledWithDefaultParameterNames(func)) { logger.error( - "Functions used in tools must have their parameters annotated with @Schema or at least" - + " the code must be compiled with the -parameters flag as a fallback. Your function" - + " tool will likely not work as expected and exit at runtime."); + """ + Functions used in tools must have their parameters annotated with @Schema or at least + the code must be compiled with the -parameters flag as a fallback. Your function + tool will likely not work as expected and exit at runtime. + """); } if (!Modifier.isStatic(func.getModifiers()) && !func.getDeclaringClass().isInstance(instance)) { throw new IllegalArgumentException( @@ -70,9 +72,11 @@ public static FunctionTool create(Object instance, Method func) { public static FunctionTool create(Method func) { if (!areParametersAnnotatedWithSchema(func) && wasCompiledWithDefaultParameterNames(func)) { logger.error( - "Functions used in tools must have their parameters annotated with @Schema or at least" - + " the code must be compiled with the -parameters flag as a fallback. Your function" - + " tool will likely not work as expected and exit at runtime."); + """ + Functions used in tools must have their parameters annotated with @Schema or at least + the code must be compiled with the -parameters flag as a fallback. Your function + tool will likely not work as expected and exit at runtime. + """); } if (!Modifier.isStatic(func.getModifiers())) { throw new IllegalArgumentException("The method provided must be static."); @@ -189,11 +193,11 @@ private Maybe> call(Map args, ToolContext to && !parameters[i].getAnnotation(Annotations.Schema.class).name().isEmpty() ? parameters[i].getAnnotation(Annotations.Schema.class).name() : parameters[i].getName(); - if (paramName.equals("toolContext")) { + if ("toolContext".equals(paramName)) { arguments[i] = toolContext; continue; } - if (paramName.equals("inputStream")) { + if ("inputStream".equals(paramName)) { arguments[i] = null; continue; } @@ -261,11 +265,11 @@ public Flowable> callLive( && !parameters[i].getAnnotation(Annotations.Schema.class).name().isEmpty() ? parameters[i].getAnnotation(Annotations.Schema.class).name() : parameters[i].getName(); - if (paramName.equals("toolContext")) { + if ("toolContext".equals(paramName)) { arguments[i] = toolContext; continue; } - if (paramName.equals("inputStream")) { + if ("inputStream".equals(paramName)) { if (invocationContext.activeStreamingTools().containsKey(this.name()) && invocationContext.activeStreamingTools().get(this.name()).stream() != null) { arguments[i] = invocationContext.activeStreamingTools().get(this.name()).stream();