{"record":{"id":"57701a4aa177767a","repo":"apache/pulsar","slug":"wraps-reflective-instantiation-failure-for-interc","errorCode":null,"errorMessage":"(wraps reflective instantiation failure for interceptor)","messagePattern":"\\(wraps reflective instantiation failure for interceptor\\)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"pulsar-common/src/main/java/org/apache/pulsar/common/intercept/BrokerEntryMetadataUtils.java","lineNumber":81,"sourceCode":"    }\n    public static <T> Set<T> loadInterceptors(\n            Set<String> interceptorNames, ClassLoader classLoader) {\n        Set<T> interceptors = new LinkedHashSet<>();\n        if (interceptorNames != null && interceptorNames.size() > 0) {\n            for (String interceptorName : interceptorNames) {\n                try {\n                    @SuppressWarnings(\"unchecked\") // class is loaded by name and expected to match type T\n                    Class<T> clz = (Class<T>) ClassLoaderUtils\n                        .loadClass(interceptorName, classLoader);\n                    try {\n                        interceptors.add(clz.getDeclaredConstructor().newInstance());\n                    } catch (InstantiationException | IllegalAccessException\n                        | InvocationTargetException | NoSuchMethodException e) {\n                        log.error()\n                            .attr(\"interceptorName\", interceptorName)\n                            .exception(e)\n                            .log(\"Create new instance failed.\");\n                        throw new RuntimeException(e);\n                    }\n                } catch (ClassNotFoundException e) {\n                    log.error()\n                        .attr(\"interceptorName\", interceptorName)\n                        .exception(e)\n                        .log(\"Load class failed.\");\n                    throw new RuntimeException(e);\n                }\n            }\n        }\n        return interceptors;\n    }\n}\n","sourceCodeStart":63,"sourceCodeEnd":95,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-common/src/main/java/org/apache/pulsar/common/intercept/BrokerEntryMetadataUtils.java#L63-L95","documentation":"BrokerEntryMetadataUtils.loadInterceptors loads and reflectively instantiates generic interceptor classes by name. Any InstantiationException, IllegalAccessException, InvocationTargetException, or NoSuchMethodException during construction is logged and rethrown as RuntimeException.","triggerScenarios":"Configured interceptor class is abstract, lacks an accessible no-arg constructor, or its constructor throws during execution, when calling loadInterceptors with an interceptor name.","commonSituations":"Custom interceptor implementations that define only parameterized constructors, constructors that throw in initialization code, classes not designed to be reflectively instantiated.","solutions":["Add a public no-arg constructor to the interceptor and make the class concrete/public","Fix any exception thrown inside the interceptor's constructor (check the wrapped InvocationTargetException cause)","Ensure the class and constructor are public so reflection can access them"],"exampleFix":"// before\npublic class MyInterceptor implements BrokerEntryMetadataInterceptor {\n    private MyInterceptor() {}\n}\n// after\npublic class MyInterceptor implements BrokerEntryMetadataInterceptor {\n    public MyInterceptor() {}\n}","handlingStrategy":"validation","validationCode":"Class<?> c = Class.forName(interceptorName);\nif (Modifier.isAbstract(c.getModifiers())) {\n    throw new IllegalArgumentException(\"Interceptor cannot be abstract: \" + interceptorName);\n}\nConstructor<?> ctor = c.getDeclaredConstructor();\nctor.setAccessible(true);\nctor.newInstance(); // dry-run: surfaces InvocationTargetException early","typeGuard":null,"tryCatchPattern":"try {\n    interceptors = BrokerEntryMetadataUtils.loadInterceptors(names, cl, Interceptor.class);\n} catch (RuntimeException e) {\n    Throwable cause = e.getCause();\n    log.error(\"Interceptor {} could not be instantiated: {}\", names, cause == null ? e : cause.getMessage());\n    throw e;\n}","preventionTips":["Ensure interceptor constructors are public, no-arg, and side-effect free","Unit-test instantiation of each configured interceptor class","Avoid heavy/throwing initialization in constructors"],"tags":["reflection","interceptor","configuration","pulsar-common"],"backgroundTag":"reflective-instantiation-failed","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"}