{"record":{"id":"50231dac2f1ebde8","repo":"json-path/JsonPath","slug":"function-of-name-cannot-be-created","errorCode":null,"errorMessage":"Function of name:  cannot be created","messagePattern":"Function of name:  cannot be created","errorType":"exception","errorClass":"InvalidPathException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/internal/function/PathFunctionFactory.java","lineNumber":84,"sourceCode":"     * @see PathFunction\n     *\n     * @param name\n     *      The name of the function\n     *\n     * @return\n     *      The implementation of a function\n     *\n     * @throws InvalidPathException\n     */\n    public static PathFunction newFunction(String name) throws InvalidPathException {\n        Class functionClazz = FUNCTIONS.get(name);\n        if(functionClazz == null){\n            throw new InvalidPathException(\"Function with name: \" + name + \" does not exist.\");\n        } else {\n            try {\n                return (PathFunction)functionClazz.newInstance();\n            } catch (Exception e) {\n                throw new InvalidPathException(\"Function of name: \" + name + \" cannot be created\", e);\n            }\n        }\n    }\n}\n","sourceCodeStart":66,"sourceCodeEnd":89,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/internal/function/PathFunctionFactory.java#L66-L89","documentation":"After a function name IS found in the FUNCTIONS map, newFunction() instantiates it via Class.newInstance(). If instantiation fails (no public no-arg constructor, constructor throws, access/initialization errors), it wraps the cause in InvalidPathException('Function of name: X cannot be created').","triggerScenarios":"A registered custom PathFunction class lacks a public no-argument constructor, throws in its constructor/static initializer, or is not public — encountered whenever a path invokes that function, e.g. $.data.myCustomFunc().","commonSituations":"Custom functions defined with constructor parameters, private inner classes, functions throwing in constructors when environment/config is missing, or hot-reload/classloader issues in containers where the class can no longer be instantiated.","solutions":["Ensure the PathFunction implementation is public with a public no-argument constructor.","Check the wrapped cause (getCause()) to find the real instantiation error.","Remove any constructor logic that can throw; defer setup to the invoke() method.","If using a custom classloader/OSGi, verify the class is loadable and instantiable at runtime."],"exampleFix":"// before\nclass MyFunc implements PathFunction {\n    public MyFunc(String cfg) { ... } // no no-arg ctor\n}\n// after\npublic class MyFunc implements PathFunction {\n    public MyFunc() { }\n    public Object invoke(EvaluationContext ctx, List<Parameter> params) { ... }\n}","handlingStrategy":"validation","validationCode":"Class<?> c = PathFunctionFactory.FUNCTIONS.get(name);\nif (c == null) throw new InvalidPathException(\"not registered\");\nif (!java.lang.reflect.Modifier.isPublic(c.getModifiers())) throw new IllegalStateException(\"function class must be public\");\ntry { c.getDeclaredConstructor().newInstance(); } catch (Exception e) { throw new IllegalStateException(\"not instantiable\", e); }","typeGuard":null,"tryCatchPattern":"try {\n    JsonPath.read(json, path);\n} catch (InvalidPathException e) {\n    if (e.getCause() != null) log.warn(\"Function instantiation failed\", e.getCause());\n    throw e;\n}","preventionTips":["Always give custom PathFunction classes a public no-arg constructor","Keep constructors side-effect free; do setup in invoke()","Check getCause() when this error appears — the root cause is wrapped"],"tags":["json-path","reflection","instantiation"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"62a4c9f0f65ba3f625aa0867d64c528ba72d09ec","analyzedAt":"2026-09-11T11:36:00.448Z","contentChangedAt":"2026-09-11T11:36:00.448Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}