{"record":{"id":"1633a4b001e3ca1f","repo":"apache/pulsar","slug":"user-class-constructor-throws-exception","errorCode":null,"errorMessage":"User class constructor throws exception","messagePattern":"User class constructor throws exception","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"pulsar-common/src/main/java/org/apache/pulsar/common/util/Reflections.java","lineNumber":96,"sourceCode":"        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) {\n        Class<?> theCls;\n        try {\n            theCls = Class.forName(userClassName, true, classLoader);\n        } catch (ClassNotFoundException | NoClassDefFoundError cnfe) {","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-common/src/main/java/org/apache/pulsar/common/util/Reflections.java#L78-L114","documentation":"If the user class's no-arg constructor itself throws an exception, newInstance() raises InvocationTargetException, which createInstance wraps in this RuntimeException with the constructor's exception as the cause. This is not a misuse of the helper — the class failed during its own initialization.","triggerScenarios":"Configuring a class whose constructor throws (static init failure, missing env var, failed connection in constructor, ExceptionInInitializerError path).","commonSituations":"Plugin constructor reads missing config/env; constructor touches unavailable services; static initializer throws (NoClassDefFoundError surfacing here).","solutions":["Read the root cause via getCause() on this exception — fix the underlying constructor exception","Remove side-effectful work from the constructor; defer to an init/initialize lifecycle method","Validate required configuration/env before the framework instantiates the class"],"exampleFix":"// before\npublic MyFilter() { connection = connect(System.getenv(\"REQUIRED_URL\")); } // NPE if unset\n// after\npublic MyFilter() { }\npublic void initialize(Map<String,String> conf) { connection = connect(conf.get(\"url\")); }","handlingStrategy":"try-catch","validationCode":"// dry-run the constructor to surface its own failure early\nClass<?> cls = Class.forName(className, true, classLoader);\ncls.getDeclaredConstructor().newInstance(); // throws the real cause here first","typeGuard":null,"tryCatchPattern":"try {\n    T obj = Reflections.createInstance(className, XFace.class, classLoader);\n} catch (RuntimeException e) {\n    if (e.getMessage().equals(\"User class constructor throws exception\")) {\n        Throwable root = e;\n        while (root.getCause() != null) { root = root.getCause(); }\n        throw new IllegalStateException(\"Constructor of \" + className + \" failed: \" + root, root);\n    }\n    throw e;\n}","preventionTips":["Keep constructors free of I/O, network calls, and env-dependent logic","Unwrap the cause chain — the real problem is in the user class's constructor or static init","Validate all required configuration before the class is instantiated by the framework"],"tags":["reflection","constructor","initialization"],"backgroundTag":"constructor-threw-exception","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"}