{"record":{"id":"f3e6e29e0e4a318f","repo":"apache/pulsar","slug":"user-class-must-have-a-no-arg-constructor","errorCode":null,"errorMessage":"User class must have a no-arg constructor","messagePattern":"User class must have a no-arg constructor","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"pulsar-common/src/main/java/org/apache/pulsar/common/util/Reflections.java","lineNumber":92,"sourceCode":"        if (!xface.isAssignableFrom(theCls)) {\n            throw new RuntimeException(userClassName + \" does not implement \" + xface.getName());\n        }\n        @SuppressWarnings(\"unchecked\") // safe: theCls is verified to be assignable to xface\n        Class<T> tCls = (Class<T>) theCls.asSubclass(xface);\n        T result;\n        try {\n            @SuppressWarnings(\"unchecked\") // safe: constructor cache is keyed by theCls which extends T\n            Constructor<T> meth = (Constructor<T>) constructorCache.get(theCls);\n            if (null == meth) {\n                meth = tCls.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 must have a no-arg constructor\", e);\n        } catch (IllegalAccessException e) {\n            throw new RuntimeException(\"User class must have a public constructor\", e);\n        } catch (InvocationTargetException e) {\n            throw new RuntimeException(\"User class constructor throws exception\", e);\n        }\n        return result;\n\n    }\n\n    /**\n     * Create an instance of <code>userClassName</code> using provided <code>classLoader</code>.\n     *\n     * @param userClassName user class name\n     * @param classLoader class loader to load the class.\n     * @return the instance\n     */\n    public static Object createInstance(String userClassName,\n                                        ClassLoader classLoader) {","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-common/src/main/java/org/apache/pulsar/common/util/Reflections.java#L74-L110","documentation":"createInstance requires a no-arg constructor: it calls getDeclaredConstructor() with no arguments. If the class has no accessible zero-argument constructor, NoSuchMethodException is wrapped in this RuntimeException. All classes instantiated through this helper must expose a public no-arg constructor.","triggerScenarios":"Configuring a class whose only constructors take arguments, so `theCls.getDeclaredConstructor()` fails with NoSuchMethodException.","commonSituations":"Plugin classes written with constructor injection; adding a constructor with parameters removed the implicit no-arg constructor; Kotlin/Scala classes without a default constructor.","solutions":["Add a public no-arg constructor to the class","Move required setup out of the constructor into an initialize/lifecycle method the framework calls","If parameters are unavoidable, instantiate the object yourself instead of via this reflective helper"],"exampleFix":"// before\npublic MyFilter(String topic) { this.topic = topic; }\n// after\npublic MyFilter() { }\npublic MyFilter(String topic) { this.topic = topic; }","handlingStrategy":"validation","validationCode":"// ensure a declared no-arg constructor exists\nClass<?> cls = Class.forName(className, true, classLoader);\ntry {\n    cls.getDeclaredConstructor();\n} catch (NoSuchMethodException e) {\n    throw new IllegalArgumentException(\"Missing no-arg constructor: \" + className);\n}","typeGuard":"boolean hasNoArgCtor(Class<?> cls) { try { cls.getDeclaredConstructor(); return true; } catch (NoSuchMethodException e) { return false; } }","tryCatchPattern":"try {\n    T obj = Reflections.createInstance(className, XFace.class, classLoader);\n} catch (RuntimeException e) {\n    if (e.getMessage().equals(\"User class must have a no-arg constructor\")) {\n        throw new IllegalArgumentException(\"Add a public no-arg constructor to \" + className, e);\n    }\n    throw e;\n}","preventionTips":["Always keep an explicit public no-arg constructor in reflectively instantiated classes","Use an init() method for dependencies instead of constructor injection","Run a startup smoke test that instantiates every configured class"],"tags":["reflection","constructor","configuration"],"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-14T05:17:10.506Z"}