* Generate the Java source for a single method in a wrapper API class. * Returns the Java source lines and whether an ObjectMapper is required.
(
key: string,
method: RpcMethodNode,
isSession: boolean,
sessionIdExpr: string
)
| 1676 | * Returns the Java source lines and whether an ObjectMapper is required. |
| 1677 | */ |
| 1678 | function generateApiMethod( |
| 1679 | key: string, |
| 1680 | method: RpcMethodNode, |
| 1681 | isSession: boolean, |
| 1682 | sessionIdExpr: string |
| 1683 | ): { lines: string[]; needsMapper: boolean; needsExperimentalImport: boolean } { |
| 1684 | const resultClass = wrapperResultClassName(method); |
| 1685 | const paramsClass = wrapperParamsClassName(method); |
| 1686 | const hasSessionId = methodHasSessionId(method); |
| 1687 | const hasExtraParams = paramsClass !== null; |
| 1688 | let needsMapper = false; |
| 1689 | |
| 1690 | const lines: string[] = []; |
| 1691 | |
| 1692 | // Javadoc |
| 1693 | const description = (method.params as JSONSchema7 | null)?.description |
| 1694 | ?? (method.result as JSONSchema7 | null)?.description |
| 1695 | ?? `Invokes {@code ${method.rpcMethod}}.`; |
| 1696 | lines.push(` /**`); |
| 1697 | lines.push(` * ${description}`); |
| 1698 | if (isSession && hasExtraParams && hasSessionId) { |
| 1699 | lines.push(` * <p>`); |
| 1700 | lines.push(` * Note: the {@code sessionId} field in the params record is overridden`); |
| 1701 | lines.push(` * by the session-scoped wrapper; any value provided is ignored.`); |
| 1702 | } |
| 1703 | if (method.stability === "experimental") { |
| 1704 | lines.push(` *`); |
| 1705 | lines.push(` * @apiNote This method is experimental and may change in a future version.`); |
| 1706 | } |
| 1707 | lines.push(` * @since 1.0.0`); |
| 1708 | lines.push(` */`); |
| 1709 | if (method.deprecated) { |
| 1710 | lines.push(` @Deprecated`); |
| 1711 | } |
| 1712 | if (method.stability === "experimental") { |
| 1713 | lines.push(` @CopilotExperimental`); |
| 1714 | } |
| 1715 | |
| 1716 | // Signature |
| 1717 | if (hasExtraParams) { |
| 1718 | lines.push(` public CompletableFuture<${resultClass}> ${key}(${paramsClass} params) {`); |
| 1719 | } else { |
| 1720 | lines.push(` public CompletableFuture<${resultClass}> ${key}() {`); |
| 1721 | } |
| 1722 | |
| 1723 | // Body |
| 1724 | if (isSession) { |
| 1725 | if (hasExtraParams) { |
| 1726 | // Merge sessionId into the params using Jackson ObjectNode |
| 1727 | needsMapper = true; |
| 1728 | lines.push(` com.fasterxml.jackson.databind.node.ObjectNode _p = MAPPER.valueToTree(params);`); |
| 1729 | lines.push(` _p.put("sessionId", ${sessionIdExpr});`); |
| 1730 | lines.push(` return caller.invoke("${method.rpcMethod}", _p, ${wrapperResultTypeExpression(resultClass)});`); |
| 1731 | } else if (hasSessionId) { |
| 1732 | lines.push(` return caller.invoke("${method.rpcMethod}", java.util.Map.of("sessionId", ${sessionIdExpr}), ${wrapperResultTypeExpression(resultClass)});`); |
| 1733 | } else { |
| 1734 | lines.push(` return caller.invoke("${method.rpcMethod}", java.util.Map.of(), ${wrapperResultTypeExpression(resultClass)});`); |
| 1735 | } |
no test coverage detected
searching dependent graphs…