elastic/elasticsearch · error · IllegalArgumentException

function reference [this::%s] matching [%s, %s/%d] not found

Error message

function reference [this::%s] matching [%s, %s/%d] not found%s

What it means

FunctionRef.create throws this IllegalArgumentException at compile time when a 'this::methodName' reference in a Painless script cannot find a matching local/user-defined function with the right name and argument count. The compiler builds a local function key from the method name and the total arity (numberOfCaptures + interface type parameters); if FunctionTable.getFunction returns null, no local function matches. The message appends a hint about incorrect argument count when the key contains a '$' separator.

Source

Thrown at modules/lang-painless/src/main/java/org/elasticsearch/painless/FunctionRef.java:116

            // resolved @allocates estimator for the delegate (metadata); the compiler clears it when not charging
            java.lang.reflect.Method allocationEstimator = null;

            Class<?> delegateMethodReturnType;
            List<Class<?>> delegateMethodParameters;
            int interfaceTypeParametersSize = interfaceMethod.typeParameters().size();

            if ("this".equals(typeName)) {
                Objects.requireNonNull(functionTable);

                if (numberOfCaptures < 0) {
                    throw new IllegalStateException("internal error");
                }

                String localFunctionKey = FunctionTable.buildLocalFunctionKey(methodName, numberOfCaptures + interfaceTypeParametersSize);
                LocalFunction localFunction = functionTable.getFunction(localFunctionKey);

                if (localFunction == null) {
                    throw new IllegalArgumentException(
                        Strings.format(
                            "function reference [this::%s] matching [%s, %s/%d] not found%s",
                            localFunctionKey,
                            targetClassName,
                            interfaceMethodName,
                            interfaceTypeParametersSize,
                            localFunctionKey.contains("$") ? " due to an incorrect number of arguments" : ""
                        )
                    );
                }

                delegateClassName = CLASS_NAME;
                isDelegateInterface = false;
                isDelegateAugmented = false;
                delegateInvokeType = needsScriptInstance ? H_INVOKEVIRTUAL : H_INVOKESTATIC;
                delegateMethodName = localFunction.getMangledName();
                delegateMethodType = localFunction.getMethodType();
                delegateInjections = new Object[0];

View on GitHub (pinned to db6a809a66)

Solutions

  1. Verify the referenced method name is spelled exactly as defined in the script.
  2. Check that the function's parameter count matches the functional interface's method arity (accounting for captures).
  3. If the hint says 'incorrect number of arguments', align the function definition's parameter count with the interface.

Example fix

// before
int apply(int a, int b) { return a + b; }
Function<Integer, Integer> f = this::apply;  // expects 1 arg, defined with 2
// after
int apply(int a) { return a + 1; }
Function<Integer, Integer> f = this::apply;
Defensive patterns

Strategy: validation

Validate before calling

// Verify the local function name and arity match the functional interface:
// int apply(int a) { return a; }  // 1 param
// Function<Integer,Integer> f = this::apply;  // interface SAM takes 1 arg

Prevention

When it happens

Trigger: Writing 'this::myFunc' in a Painless script where myFunc is not defined, is defined with a different number of parameters than the functional interface expects, or where the argument count is mismatched. The localFunctionKey encodes arity, so a wrong parameter count produces a different key that does not exist.

Common situations: Typos in the method name. Defining a function with N parameters but referencing it through a functional interface expecting M parameters. Refactoring a function signature without updating the method reference.

Related errors


AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12). Data as JSON: /api/errors/f0aba5e004ed0a9a. Report an issue: GitHub.