{"record":{"id":"4bce13e434b0662e","repo":"apache/pulsar","slug":"user-class-doesn-t-have-such-method","errorCode":null,"errorMessage":"User class doesn't have such method","messagePattern":"User class doesn't have such method","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"pulsar-common/src/main/java/org/apache/pulsar/common/util/Reflections.java","lineNumber":129,"sourceCode":"        Class<?> theCls;\n        try {\n            theCls = Class.forName(userClassName, true, classLoader);\n        } catch (ClassNotFoundException | NoClassDefFoundError cnfe) {\n            throw new RuntimeException(\"User class must be in class path\", cnfe);\n        }\n        Object result;\n        try {\n            Constructor<?> meth = constructorCache.get(theCls);\n            if (null == meth) {\n                meth = theCls.getDeclaredConstructor();\n                meth.setAccessible(true);\n                constructorCache.put(theCls, meth);\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(\"User class doesn't have such method\", e);\n        } catch (IllegalAccessException e) {\n            throw new RuntimeException(\"User class must have a no-arg constructor\", e);\n        } catch (InvocationTargetException e) {\n            throw new RuntimeException(\"User class constructor throws exception\", e);\n        }\n        return result;\n\n    }\n\n    public static Object createInstance(String userClassName,\n                                        ClassLoader classLoader, Object[] params, Class<?>[] paramTypes) {\n        if (params.length != paramTypes.length) {\n            throw new RuntimeException(\n                    \"Unequal number of params and paramTypes. Each param must have a corresponding param type\");\n        }\n        Class<?> theCls;\n        try {\n            theCls = Class.forName(userClassName, true, classLoader);","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-common/src/main/java/org/apache/pulsar/common/util/Reflections.java#L111-L147","documentation":"In this overload, a failure to locate the no-arg constructor (NoSuchMethodException from getDeclaredConstructor()) is wrapped with the message \"User class doesn't have such method\" — an imprecise message, but it means the class lacks a zero-argument constructor usable for reflective instantiation.","triggerScenarios":"Configuring a class that only has parameterized constructors, so `getDeclaredConstructor()` (no args) throws NoSuchMethodException.","commonSituations":"Classes written with constructor injection; adding a parameterized constructor removed the implicit default constructor; generated classes without default constructors.","solutions":["Add a public no-arg constructor to the class","Use a different instantiation path if constructor arguments are required","Note the misleading message: check for missing no-arg constructor, not a missing method"],"exampleFix":"// before\npublic MyHandler(Config c) { }\n// after\npublic MyHandler() { }\npublic MyHandler(Config c) { }","handlingStrategy":"validation","validationCode":"// ensure a no-arg constructor exists despite the vague message this error gives\nClass<?> cls = Class.forName(className, true, classLoader);\ntry { cls.getDeclaredConstructor(); }\ncatch (NoSuchMethodException e) { throw new IllegalArgumentException(\"No no-arg constructor: \" + className); }","typeGuard":"boolean hasNoArgCtor(Class<?> cls) { try { cls.getDeclaredConstructor(); return true; } catch (NoSuchMethodException e) { return false; } }","tryCatchPattern":"try {\n    Object o = Reflections.createInstance(className, classLoader);\n} catch (RuntimeException e) {\n    if (e.getMessage().equals(\"User class doesn't have such method\")) {\n        // misleading message: actually means no no-arg constructor\n        throw new IllegalArgumentException(\"Add a no-arg constructor to \" + className, e);\n    }\n    throw e;\n}","preventionTips":["Remember this message is misleading: it means a missing no-arg constructor","Add an explicit public no-arg constructor to every reflectively instantiated class","Prefer an init/lifecycle method over constructor parameters for injected dependencies"],"tags":["reflection","constructor","misleading-message"],"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"}