Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:APIJSON APIJSON AbstractFunctionParser invoke

From Leeroopedia
Knowledge Sources
Domains Remote_Functions, Script_Execution, Reflection
Last Updated 2026-10-11 00:00 GMT

Overview

Concrete function executor that parses function call strings from request JSON, looks up function metadata in the FUNCTION_MAP registry, and invokes Java methods via reflection or delegates to script engines for JavaScript/Lua/Python execution, provided by the APIJSON ORM framework.

Description

The invoke method on AbstractFunctionParser is the remote function execution engine. When a request JSON contains a function-call key (e.g., "isContain(praiseUserIdList,userId)"), the parser:

  1. Calls parseFunction() to extract the method name, parameter types, and argument values from the call string
  2. Looks up the function metadata in FUNCTION_MAP (populated from the Function database table)
  3. Checks ENABLE_REMOTE_FUNCTION and ENABLE_SCRIPT_FUNCTION flags
  4. Validates version and tag constraints from the Function table
  5. If the language is Java: invokes the method via Method.invoke(parser, args) (reflection)
  6. If the language is JavaScript/Lua/Python: delegates to the appropriate ScriptExecutor via SCRIPT_EXECUTOR_MAP

The FUNCTION_MAP maps function names to metadata (language, returnType, version, tag, methods). The SCRIPT_EXECUTOR_MAP maps language names to script executor instances.

Usage

Called internally by AbstractObjectParser when processing function-call keys. Register remote functions by inserting records into the Function table and defining corresponding Java methods in a subclass of AbstractFunctionParser.

Code Reference

Source Location

  • Repository: APIJSON
  • File: src/main/java/apijson/orm/AbstractFunctionParser.java

Signature

// Instance method
public Object invoke(@NotNull String function, @NotNull M current) throws Exception

public Object invoke(@NotNull String function, @NotNull M current, boolean containRaw) throws Exception

// Static method (core implementation)
public static <T, M extends Map<String, Object>, L extends List<Object>> Object invoke(
    @NotNull AbstractFunctionParser<T, M, L> parser,
    @NotNull String function,
    @NotNull Map<String, Object> current,
    boolean containRaw
) throws Exception

// Reflection-based invocation
public static <T, M extends Map<String, Object>, L extends List<Object>> Object invoke(
    @NotNull AbstractFunctionParser<T, M, L> parser,
    @NotNull String methodName,
    @NotNull Class<?>[] parameterTypes,
    @NotNull Object[] args,
    String returnType,
    Map<String, Object> current,
    ScriptExecutor scriptExecutor
) throws Exception

Parameters:

  • function - Full function call string (e.g., "isContain(praiseUserIdList,userId)")
  • current - Current object context (the row data)
  • containRaw - Whether raw SQL is allowed in function arguments
  • parser - AbstractFunctionParser instance for reflection
  • methodName - Java method name to invoke
  • parameterTypes - Method parameter types
  • args - Method arguments
  • returnType - Expected return type from Function table
  • scriptExecutor - Script engine (null for Java)

Import

import apijson.orm.AbstractFunctionParser;

I/O Contract

Inputs

Name Type Required Description
function String Yes Function call string (e.g., "isContain(array,key)")
current M (Map<String,Object>) Yes Current row data context
containRaw boolean No Allow raw SQL in arguments (default false)
FUNCTION_MAP Map<String,Map<String,Object>> Yes Function metadata registry
SCRIPT_EXECUTOR_MAP Map<String,ScriptExecutor> No Script engine registry
ENABLE_REMOTE_FUNCTION boolean Yes Master switch (default true)
ENABLE_SCRIPT_FUNCTION boolean Yes Script execution switch (default false)

Outputs

Name Type Description
return Object Function return value (Boolean, String, Number, etc.)

Usage Examples

Defining a Remote Function

public class MyFunctionParser extends AbstractFunctionParser<Long, JSONObject, JSONArray> {
    
    // Register in Function table:
    // { "name": "isContain", "language": "java", "returnType": "Boolean" }
    
    // Java method must match the function call signature:
    public Boolean isContain(JSONArray array, Long id) {
        return array != null && array.contains(id);
    }
}

// Request JSON usage:
// { "Moment": { "id": 1, "isContain(praiseUserIdList,userId)": "isContain" } }
// After SQL query, isContain() is called with the Moment's praiseUserIdList and userId
// Response: { "Moment": { "id": 1, "isContain(praiseUserIdList,userId)": true } }

Script Function (JavaScript)

// Register in Script table:
// { "name": "multiply", "language": "javascript", "script": "function multiply(a, b) { return a * b; }" }
// Register in Function table:
// { "name": "multiply", "language": "javascript", "returnType": "Number" }

// Request JSON:
// { "Order": { "price": 100, "quantity": 3, "multiply(price,quantity)": "multiply" } }
// Script executor evaluates: multiply(100, 3) = 300

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment