{"record":{"id":"85c6733ecbb924ac","repo":"apache/pulsar","slug":"class-doesn-t-have-such-method","errorCode":null,"errorMessage":"Class  doesn't have such method","messagePattern":"Class  doesn't have such method","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"pulsar-functions/runtime-all/src/main/java/org/apache/pulsar/functions/instance/JavaInstanceMain.java","lineNumber":148,"sourceCode":"\n    public static Object createInstance(String userClassName,\n                                        ClassLoader classLoader) {\n        Class<?> theCls;\n        try {\n            theCls = Class.forName(userClassName, true, classLoader);\n        } catch (ClassNotFoundException | NoClassDefFoundError cnfe) {\n            throw new RuntimeException(\"Class \" + userClassName + \" must be in class path\", cnfe);\n        }\n        Object result;\n        try {\n            Constructor<?> meth = theCls.getDeclaredConstructor();\n            meth.setAccessible(true);\n\n            result = meth.newInstance();\n        } catch (InstantiationException ie) {\n            throw new RuntimeException(\"User class must be concrete\", ie);\n        } catch (NoSuchMethodException e) {\n            throw new RuntimeException(\"Class \" + userClassName + \" doesn't have such method\", e);\n        } catch (IllegalAccessException e) {\n            throw new RuntimeException(\"Class \" + userClassName + \" must have a no-arg constructor\", e);\n        } catch (InvocationTargetException e) {\n            throw new RuntimeException(\"Class \" + userClassName + \" constructor throws exception\", e);\n        }\n        return result;\n    }\n\n    public static ClassLoader loadJar(ClassLoader parent, File[] jars) throws MalformedURLException {\n        URL[] urls = new URL[jars.length];\n        for (int i = 0; i < jars.length; i++) {\n            urls[i] = jars[i].toURI().toURL();\n        }\n        return new URLClassLoader(urls, parent);\n    }\n\n    public static boolean isBlank(String str) {\n        int strLen;","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-functions/runtime-all/src/main/java/org/apache/pulsar/functions/instance/JavaInstanceMain.java#L130-L166","documentation":"createInstance() calls theCls.getDeclaredConstructor() for a zero-argument constructor. NoSuchMethodException means no such constructor exists; Pulsar rethrows as RuntimeException(\"Class X doesn't have such method\"). Despite the wording, the actual requirement is a public no-arg constructor.","triggerScenarios":"User function class only has constructors taking arguments (e.g. requires configuration injected), so getDeclaredConstructor() fails; also triggered by similar misuse in the surrounding catch chain.","commonSituations":"Functions written with constructor-injected dependencies; Lombok @AllArgsConstructor without keeping the default constructor; classes written for frameworks that use DI.","solutions":["Add a public no-arg constructor to the function class and move per-instance configuration into the open()/process() methods via FunctionContext/context.","If using Lombok, add @NoArgsConstructor alongside other constructor annotations.","Review the message cause: it may mask a different reflective failure on the same class; check the wrapped NoSuchMethodException stack trace."],"exampleFix":"// before\npublic MyFunction(String topic) { this.topic = topic; }\n// after\npublic MyFunction() {}\npublic void initialize(Context ctx) { this.topic = ctx...; }","handlingStrategy":"validation","validationCode":"Class<?> c = Class.forName(className);\nboolean hasNoArgCtor = java.util.Arrays.stream(c.getDeclaredConstructors())\n    .anyMatch(k -> k.getParameterCount() == 0);","typeGuard":"boolean hasNoArgConstructor(String fcn) { try { Class.forName(fcn).getDeclaredConstructor(); return true; } catch (Throwable t) { return false; } }","tryCatchPattern":"try { Object f = JavaInstanceMain.createInstance(className, cl); } catch (RuntimeException e) { if (e.getMessage().contains(\"doesn't have such method\")) { System.err.println(className + \" needs a public no-arg constructor\"); } throw e; }","preventionTips":["Always define a public no-arg constructor in function classes.","Pass configuration via Context/FunctionContext, not constructor parameters.","With Lombok, pair @AllArgsConstructor with @NoArgsConstructor.","Add a unit test reflecting on the constructor before packaging."],"tags":["reflection","constructor","function-config","java"],"backgroundTag":"missing-no-arg-constructor","analyzedSha":"820761864ed8e2a7d2e52dd9763ad2ae117c1395","analyzedAt":"2026-09-06T00:14:20.138Z","contentChangedAt":"2026-09-06T00:14:20.138Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}