Skip to content

feat(java): Add CopilotToolProcessor annotation processor (task 4.3)#1777

Merged
edburns merged 29 commits into
edburns/1682-java-tool-ergonomicsfrom
copilot/edburns1682-java-tool-ergonomics
Jun 24, 2026
Merged

feat(java): Add CopilotToolProcessor annotation processor (task 4.3)#1777
edburns merged 29 commits into
edburns/1682-java-tool-ergonomicsfrom
copilot/edburns1682-java-tool-ergonomics

Conversation

Copilot AI commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Fixes #1760 .

Implements the JSR 269 annotation processor that finds @CopilotTool-annotated methods and generates $$CopilotToolMeta companion classes at compile time — zero reflection, zero -parameters flag requirement.

Changes

  • CopilotToolProcessor.java — The processor that:

    • Groups @CopilotTool methods by enclosing class, generates one $$CopilotToolMeta per class in the same package
    • Emits compile errors for private methods and required=true + defaultValue conflicts
    • Converts method names to snake_case (or uses explicit @CopilotTool(name=...))
    • Delegates to SchemaGenerator for type→JSON Schema mapping, adds @Param description/default via withMeta helper
    • Generates invocation lambdas with correct return-type handling and argument deserialization patterns
    • Routes to ToolDefinition.create / createOverride / createSkipPermission / createWithDefer based on annotation attributes
    • Maps ToolDefer.NONEnull (regular create), non-NONE → createWithDefer
  • META-INF/services/javax.annotation.processing.Processor — Registers CopilotToolProcessor

  • module-info.java — Adds to provides clause

  • CopilotToolProcessorTest.java — Programmatic javax.tools.JavaCompiler tests covering generation, error cases, return types, arg coercion, schema output, and service registration

Generated code shape

// GENERATED by CopilotToolProcessor — do not edit
package com.example;

final class MyTools$$CopilotToolMeta {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    @SuppressWarnings({"unchecked", "rawtypes"})
    static List<ToolDefinition> definitions(MyTools instance) {
        return List.of(
            ToolDefinition.create(
                "set_current_phase",
                "Sets the current phase",
                Map.of("type", "object", "properties", Map.ofEntries(
                    Map.entry("phase", (Map<String, Object>)(Map) withMeta(Map.of("type", "string"), "The phase", null))
                ), "required", List.of("phase")),
                invocation -> {
                    Map<String, Object> args = invocation.getArguments();
                    java.lang.String phase = (String) args.get("phase");
                    return CompletableFuture.completedFuture(instance.setCurrentPhase(phase));
                }
            )
        );
    }
}

edburns and others added 18 commits June 23, 2026 16:14
Your branch is up to date with 'upstream/edburns/1682-java-tool-ergonomics'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   1682-java-tool-ergonomics-prompts-remove-before-merge/20260618-prompts.md

Signed-off-by: Ed Burns <edburns@microsoft.com>
- Add NONE constant to ToolDefer enum for annotation default value
- Create com.github.copilot.tool.CopilotTool annotation
- Create com.github.copilot.tool.Param annotation
- Export com.github.copilot.tool package in module-info.java
- Add CopilotToolAnnotationTest verifying retention, targets, defaults

Closes #1758
NONE is an annotation-only sentinel for @copilotTool(defer=...) defaults.
Its @jsonvalue now returns null so @JsonInclude(NON_NULL) omits it from
the JSON-RPC payload, matching the nullable/optional semantics used by
all other SDKs (.NET CopilotToolDefer?, Node defer?, Go omitempty,
Python | None, Rust Option<DeferMode>).
* WIP Phase 4.1

* Remove prompts, pre-merge

* fix(java): correct ToolDefer.NONE Javadoc on @jsonvalue null semantics

Clarify that @jsonvalue returning null does not cause field omission
by @JsonInclude(NON_NULL) — it only changes the leak from "" to null.
The primary protection is mapping NONE to a null field reference before
constructing ToolDefinition (responsibility of the annotation processor
and ToolDefinition.fromObject()).

* fix(java): address three review comments

Co-authored-by: edburns <75821+edburns@users.noreply.github.com>

* Revert "Remove prompts, pre-merge"

This reverts commit a4fe9b2.

---------

Co-authored-by: Ed Burns <edburns@microsoft.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: edburns <75821+edburns@users.noreply.github.com>
…ity (#1766)

* Initial plan

* feat(java): add SchemaGenerator compile-time type-to-JSON-Schema utility

Creates SchemaGenerator.java that maps javax.lang.model TypeMirror
instances to JSON Schema source code literals (Map.of(...) expressions).

Implements all 24 type mappings from the specification including:
- Primitives and boxed types (int/Integer, long/Long, etc.)
- String, UUID, OffsetDateTime
- Collections (List<T>, Collection<T>, Set<T>)
- Maps (Map<String, V> with typed values)
- Arrays (String[])
- Enums (with constant enumeration)
- Records and POJOs (with properties/required)
- Optional<T>, OptionalInt, OptionalDouble
- Sealed interfaces (oneOf)
- JsonNode and Object (any)

Also adds SchemaGeneratorTest using compilation-testing approach
with javax.tools.JavaCompiler to exercise the generator at compile time.

Closes #1759

* fix: address code review - remove unused param, handle all primitive types

* fix(java): correct SimpleJavaFileObject override - getCharContent not getContent

Co-authored-by: edburns <75821+edburns@users.noreply.github.com>

* spotless

* Remove .class files generated by test

* spotless

* fix: use Map.ofEntries for properties to avoid Map.of 10-entry limit

Address review comment r3461777483: Map.of() only supports up to 10
key-value pairs. Switch properties maps in SchemaGenerator to use
Map.ofEntries(Map.entry(...), ...) so records/POJOs/methods with >10
fields won't cause generated source compilation failures.

Update SchemaGeneratorTest expectations to match the new format.

* fix: add missing Byte/Short/Character boxed type mappings

Address review comment r3461777428: Byte and Short now map to
"integer", Character maps to "string", matching their primitive
equivalents. Add tests for all three.

* fix: add missing OptionalLong mapping in generateDeclaredTypeSchema

Address review comment r3461777459: OptionalLong was handled in
isOptionalType/unwrapOptional but missing from generateDeclaredTypeSchema,
causing it to fall through to POJO introspection when used as a direct
return type. Add the mapping and tests for OptionalInt, OptionalLong,
and OptionalDouble.

* fix: correct misleading @JsonSubTypes comment on sealed interface handling

Address review comment r3461777579: the implementation uses
getPermittedSubclasses() (Java sealed types), not Jackson annotations.

* test: add sealed interface test for oneOf schema generation

Address review comment r3461777685: the processor had special handling
for TestSealed* types but no test exercised generateSealedSchema().
Add a test with a sealed interface (TestSealedShape) and two record
permits (Circle, Rect) verifying the oneOf schema output.

* test: add >10-field record test proving Map.ofEntries compiles

Address review comment r3461777706: add a test with an 11-component
record that verifies the generated Map.ofEntries(...) expression
actually compiles, proving the Map.of 10-entry limit fix works
end-to-end.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: edburns <75821+edburns@users.noreply.github.com>
Co-authored-by: Ed Burns <edburns@microsoft.com>
Copilot AI and others added 2 commits June 23, 2026 20:31
Implements JSR 269 annotation processor that finds @CopilotTool-annotated
methods and generates $$CopilotToolMeta companion classes containing tool
definitions, JSON Schema, and invocation lambdas.

Key features:
- snake_case tool name conversion from camelCase method names
- Access level enforcement (compile error for private methods)
- Return type handling (String, void, CompletableFuture<String>, etc.)
- Argument deserialization (direct cast for primitives/String, convertValue for complex)
- @Param description and defaultValue support in schema
- ToolDefer support (NONE maps to null/regular create)
- overridesBuiltInTool and skipPermission support

Also includes comprehensive test suite using javax.tools.JavaCompiler
programmatic compilation.

Closes #1760

Co-authored-by: edburns <75821+edburns@users.noreply.github.com>
- Use fully qualified type names in generated code for type safety
- Fix Files.walk() resource leak in test with try-with-resources
- Rename exception variables for clarity

Co-authored-by: edburns <75821+edburns@users.noreply.github.com>
Copilot AI changed the title [WIP] Add JSR 269 annotation processor for CopilotTool feat(java): Add CopilotToolProcessor annotation processor (task 4.3) Jun 23, 2026
Copilot AI requested a review from edburns June 23, 2026 20:37
@github-actions

This comment has been minimized.

@edburns edburns left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot

Both the JDK 25 and JDK 17 tests failed.
https://github.com/github/copilot-sdk/actions/runs/28055309594/
Please investigate, fix, run-tests locally, and then push commits.

- Remove unused Collections import
- Reformat boolean expressions: && at start of continuation lines
- Reformat ternary: ? at start of continuation line
- Reformat .replace() chain with one call per line
- Fix hasErrorContaining stream method chain formatting
- Fix resolveClasspath() to use System.getProperty("java.class.path")
  first, ensuring Jackson and all test deps are available when compiling
  generated $$CopilotToolMeta code
@github-actions

This comment has been minimized.

Copilot AI added 3 commits June 23, 2026 21:24
- Merge propertyEntries.add() onto one line per formatter requirement
- Fix sb.append() chain formatting to match Eclipse formatter output
- Revert escapeJava to original line-breaking style (formatter preference)
- Fix resolveClasspath() to combine system classpath with CodeSource
  paths from key classes (SDK, Jackson, RPC types) ensuring all
  dependencies are available for javac in the annotation processor test
The generated 6342CopilotToolMeta code uses ObjectMapper which requires
jackson-core (Versioned, JsonFactory) and jackson-annotations at
compile time. Add these transitive dependencies to the key classes
list so their CodeSource paths are included in the javac classpath.

Copilot AI commented Jun 23, 2026

Copy link
Copy Markdown
Contributor Author

@copilot

Both the JDK 25 and JDK 17 tests failed....

Investigated and fixed both CI failures across multiple iterations:

JDK 25 (Spotless formatting): Applied all Eclipse formatter-required changes to CopilotToolProcessor.java and CopilotToolProcessorTest.java — boolean expression line breaks (&& at start of continuation), ternary formatting, sb.append() chain formatting, unused import removal, and array initializer formatting. Spotless check passed as of run 28058839052.

JDK 17 (test classpath): The resolveClasspath() method only returned the SDK classes directory via CodeSource, missing Jackson dependencies. Fixed by collecting classpath entries from System.getProperty("java.class.path") combined with CodeSource paths from key classes (CopilotTool, ObjectMapper, JsonFactory, JsonProperty, ToolDefinition). JDK 17 tests passed as of run 28058839052.

Latest commit: 1ac82f6 — fixes the last Spotless formatting issue (keyClasses array initializer). CI should be green on this push.

Copilot AI requested a review from edburns June 23, 2026 21:58
@github-actions

This comment has been minimized.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generated by SDK Consistency Review Agent for issue #1777 · sonnet46 1.8M

Comment thread java/src/main/java/com/github/copilot/tool/CopilotToolProcessor.java Outdated
@edburns edburns marked this pull request as ready for review June 23, 2026 22:06
@edburns edburns requested a review from a team as a code owner June 23, 2026 22:06
Copilot AI review requested due to automatic review settings June 23, 2026 22:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a Java JSR-269 annotation processor to generate $$CopilotToolMeta companion classes for @CopilotTool methods (compile-time tool metadata + invocation wiring), and wires it into the Java module and service loader with compiler-based tests.

Changes:

  • Implement CopilotToolProcessor to discover @CopilotTool methods, validate invalid usages, and generate per-class $$CopilotToolMeta sources.
  • Register the processor via META-INF/services and module-info.java.
  • Add JavaCompiler-driven tests validating generation, errors, and key codegen behaviors.
Show a summary per file
File Description
java/src/main/java/com/github/copilot/tool/CopilotToolProcessor.java Implements the annotation processor and code generation for $$CopilotToolMeta.
java/src/main/resources/META-INF/services/javax.annotation.processing.Processor Registers the new processor for service loading.
java/src/main/java/module-info.java Adds the processor to the module provides clause.
java/src/test/java/com/github/copilot/tool/CopilotToolProcessorTest.java Programmatic compiler tests for generation and validation behaviors.

Copilot's findings

  • Files reviewed: 4/4 changed files
  • Comments generated: 4

Comment thread java/src/main/java/com/github/copilot/tool/CopilotToolProcessor.java Outdated
Comment thread java/src/main/java/com/github/copilot/tool/CopilotToolProcessor.java Outdated
…eta contract

Address PR #1777 review comment (r3463252393): the generated
$$CopilotToolMeta class was using `new ObjectMapper()`, which lacks
the SDK Jackson configuration (JavaTimeModule, NON_NULL inclusion,
lenient unknown-properties). This would break tool argument coercion
and return serialization at runtime for java.time.* and other types.

Instead of embedding a bare or configured ObjectMapper in the
generated code, change the generated `definitions()` method signature
from:
    definitions(MyTools instance)
to:
    definitions(MyTools instance, ObjectMapper mapper)

This establishes an internal contract: the caller (the future
ToolDefinition.fromObject() in issue #1761) is responsible for
supplying a properly configured mapper via reflective invocation.
The generated code uses `mapper` for all convertValue() and
writeValueAsString() calls.

Benefits:
- No DRY violation (mapper config stays canonical in JsonRpcClient)
- No new public API exposing ObjectMapper
- No package-visibility workarounds
- Clean separation: generated code declares its needs, caller supplies

Issue #1761 description has been updated to document this contract
so the implementing agent knows to pass ObjectMapper as the second
argument when reflectively invoking definitions().
@github-actions

This comment has been minimized.

edburns added 4 commits June 23, 2026 22:06
Address review comment on PR #1777: the isRecordOrPojo heuristic
incorrectly triggered for JDK container types (List, Map, etc.)
when used as a single tool parameter. For example, a tool with
parameter List<String> would attempt to deserialize the entire
arguments object as a List, failing at runtime.

Replace the heuristic with a deterministic check: only Java records
qualify for the getArgumentsAs() shortcut. Records are immutable
data carriers with compiler-guaranteed component lists, making them
safe for whole-object deserialization. POJOs and all other class
types now fall through to the per-field extraction path, which
always works correctly.

Removed isSimpleType() helper which was only used by the old
heuristic.
Address review comment on PR #1777: @Param(defaultValue=...) was
always emitted as a JSON string in the generated schema's 'default'
field, making numeric and boolean defaults the wrong type (e.g.,
"10" instead of 10, "true" instead of true).

Changes:
- withMeta helper: String defaultValue -> Object defaultValue
- buildPropertySchema: reuse generateDefaultLiteral() to emit typed
  Java literals (int, boolean, etc.) instead of always quoting
- Add test emitsTypedDefaultValuesInSchema verifying int -> 10,
  boolean -> true, String -> "hello" in generated code
Address review comment on PR #1777: getGeneratedSource() fallback
search appended 61059CopilotToolMeta to a simpleName that already
contained it, producing MyTools$$CopilotToolMeta$$CopilotToolMeta.
Simplify to just match on 'class <simpleName>'.
Address SDK Consistency Review on PR #1777: the if/else if chain
in writeToolDefinition silently dropped combined annotation flags
(e.g., overridesBuiltInTool + skipPermission + defer). All other
SDKs support combining these flags simultaneously.

Replace the factory method dispatch with a direct call to the
ToolDefinition record constructor, which accepts all seven fields
independently. Each flag is now emitted as its own argument:
Boolean.TRUE or null for overridesBuiltInTool/skipPermission,
ToolDefer.X or null for defer.

Add test generatesCombinedFlags verifying all three flags appear
in generated code when set together.
@github-actions

Copy link
Copy Markdown
Contributor

Cross-SDK Consistency Review ✅

This PR adds a Java-specific annotation processor (@CopilotTool / @Param) for ergonomic tool definition. Here's how it maps to the other SDKs:

SDK Ergonomic tool definition mechanism
Node.js defineTool() function with options
Python @define_tool decorator
Go DefineTool[T]() generic function
.NET CopilotTool.DefineTool() with delegate + options
Rust ToolHandler trait + builder methods
Java (this PR) @CopilotTool annotation processor

Feature parity ✅

The three tool option attributes introduced by @CopilotTool are consistent with all other SDKs:

Feature Node.js Python Go .NET Rust Java (this PR)
overridesBuiltInTool ✅ (on Tool struct)
skipPermission ✅ (on Tool struct)
defer: auto|never

ToolDefer.NONE sentinel ✅

Adding NONE as an annotation-default sentinel is appropriate and consistent. All other SDKs represent "no defer specified" as null/None/omitted (nullable), and NONEnull on the wire matches that behavior exactly.

No action needed

This is a Java-idiomatic feature (JSR 269 annotation processing, compile-time codegen) that doesn't require mirroring in other SDKs — each SDK already has its own idiomatic approach to ergonomic tool definition. The API surface and wire semantics are consistent across all six implementations.

Generated by SDK Consistency Review Agent for issue #1777 · sonnet46 1.6M ·

@edburns edburns merged commit 4de5995 into edburns/1682-java-tool-ergonomics Jun 24, 2026
13 checks passed
@edburns edburns deleted the copilot/edburns1682-java-tool-ergonomics branch June 24, 2026 02:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Java] @CopilotTool ergonomics 4.3: Annotation processor (CopilotToolProcessor)

3 participants