{"record":{"id":"c43dedf2f5ef206d","repo":"spring-projects/spring-ai","slug":"instantiation-failed","errorCode":null,"errorMessage":" instantiation failed","messagePattern":" instantiation failed","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/common/MetaUtils.java","lineNumber":82,"sourceCode":"\tpublic static Map<String, Object> getMeta(Class<? extends MetaProvider> metaProviderClass) {\n\n\t\tif (metaProviderClass == null) {\n\t\t\treturn null;\n\t\t}\n\n\t\tString className = metaProviderClass.getName();\n\t\tMetaProvider metaProvider;\n\t\ttry {\n\t\t\t// Prefer a public no-arg constructor; fall back to a declared no-arg if\n\t\t\t// accessible\n\t\t\tConstructor<? extends MetaProvider> constructor = getConstructor(metaProviderClass);\n\t\t\tmetaProvider = constructor.newInstance();\n\t\t}\n\t\tcatch (NoSuchMethodException e) {\n\t\t\tthrow new IllegalArgumentException(\"Required no-arg constructor not found in \" + className, e);\n\t\t}\n\t\tcatch (InvocationTargetException | InstantiationException | IllegalAccessException e) {\n\t\t\tthrow new IllegalArgumentException(className + \" instantiation failed\", e);\n\t\t}\n\n\t\tMap<String, Object> meta = metaProvider.getMeta();\n\t\treturn meta == null ? null : Collections.unmodifiableMap(meta);\n\t}\n\n\t/**\n\t * Locate a no-argument constructor on the given class: prefer public, otherwise fall\n\t * back to a declared no-arg constructor.\n\t * @param metaProviderClass the class to inspect\n\t * @return the resolved no-arg constructor\n\t * @throws NoSuchMethodException if the class does not declare any no-arg constructor\n\t */\n\tprivate static Constructor<? extends MetaProvider> getConstructor(Class<? extends MetaProvider> metaProviderClass)\n\t\t\tthrows NoSuchMethodException {\n\t\ttry {\n\t\t\treturn metaProviderClass.getDeclaredConstructor();\n\t\t}","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/common/MetaUtils.java#L64-L100","documentation":"MetaUtils.getMeta reflectively invokes the no-arg constructor of a MetaProvider via Constructor.newInstance(). This error wraps InvocationTargetException, InstantiationException, or IllegalAccessException when instantiation or the constructor body fails — e.g. the constructor threw an exception, the class is abstract, or the constructor is not accessible.","triggerScenarios":"A MetaProvider no-arg constructor that throws (wrapping InvocationTargetException); the registered class is abstract or an interface (InstantiationException); the constructor exists but is not public and not accessible (IllegalAccessException).","commonSituations":"Constructor initialization logic fails at runtime (missing env vars, failed connections); someone registers an interface or abstract base class as the provider; provider class is package-private and the module system blocks reflective access.","solutions":["Read the wrapped cause (e.getCause()) to find the real failure inside the constructor and fix it","Ensure the registered class is a concrete, instantiable class, not abstract or an interface","Make the no-arg constructor public","Remove heavy/fragile initialization from the constructor; do it lazily in getMeta()"],"exampleFix":"// before\nclass MyMetaProvider implements MetaProvider {\n    MyMetaProvider() { connectToRemote(); } // throws at construction\n}\n\n// after\nclass MyMetaProvider implements MetaProvider {\n    public Map<String, Object> getMeta() {\n        return Map.of(\"status\", lookupStatus()); // no throwing ctor work\n    }\n}","handlingStrategy":"try-catch","validationCode":"// instantiate eagerly at startup to fail fast\nObject probe = metaProviderClass.getDeclaredConstructor().newInstance();","typeGuard":null,"tryCatchPattern":"try {\n    Map<String, Object> meta = MetaUtils.getMeta(providerClass);\n} catch (IllegalArgumentException e) {\n    Throwable cause = e.getCause();\n    log.error(\"MetaProvider {} failed: {}\", providerClass.getName(),\n        cause != null ? cause.getMessage() : e.getMessage(), cause);\n}","preventionTips":["Keep MetaProvider constructors trivial (no I/O, no throwing logic)","Fail fast by constructing the provider once at startup","Ensure the class is concrete and the constructor is public"],"tags":["reflection","mcp","instantiation"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}