{"record":{"id":"d4687a7a6af8bdce","repo":"spring-projects/spring-framework","slug":"specified-class-is-an-interface","errorCode":null,"errorMessage":"Specified class is an interface","messagePattern":"Specified class is an interface","errorType":"exception","errorClass":"BeanInstantiationException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/BeanUtils.java","lineNumber":102,"sourceCode":"\t\t\tdouble.class, 0D,\n\t\t\tchar.class, '\\0');\n\n\tprivate static final boolean KOTLIN_REFLECT_PRESENT = KotlinDetector.isKotlinReflectPresent();\n\n\n\t/**\n\t * Convenience method to instantiate a class using its no-arg constructor.\n\t * @param clazz class to instantiate\n\t * @return the new instance\n\t * @throws BeanInstantiationException if the bean cannot be instantiated\n\t * @see Class#newInstance()\n\t * @deprecated following the deprecation of {@link Class#newInstance()} in JDK 9\n\t */\n\t@Deprecated(since = \"5.0\")\n\tpublic static <T> T instantiate(Class<T> clazz) throws BeanInstantiationException {\n\t\tAssert.notNull(clazz, \"Class must not be null\");\n\t\tif (clazz.isInterface()) {\n\t\t\tthrow new BeanInstantiationException(clazz, \"Specified class is an interface\");\n\t\t}\n\t\ttry {\n\t\t\treturn clazz.newInstance();\n\t\t}\n\t\tcatch (InstantiationException ex) {\n\t\t\tthrow new BeanInstantiationException(clazz, \"Is it an abstract class?\", ex);\n\t\t}\n\t\tcatch (IllegalAccessException ex) {\n\t\t\tthrow new BeanInstantiationException(clazz, \"Is the constructor accessible?\", ex);\n\t\t}\n\t}\n\n\t/**\n\t * Instantiate a class using its 'primary' constructor (for Kotlin classes,\n\t * potentially having default arguments declared) or its default constructor\n\t * (for regular Java classes, expecting a standard no-arg setup).\n\t * <p>Note that this method tries to set the constructor accessible\n\t * if given a non-accessible (that is, non-public) constructor.","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/BeanUtils.java#L84-L120","documentation":"Thrown as BeanInstantiationException by the deprecated BeanUtils.instantiate(Class) at BeanUtils.java:102 when the supplied class is an interface (clazz.isInterface() == true). The method itself is deprecated because it delegates to the deprecated Class.newInstance(); prefer instantiateClass(Class). There is no underlying cause because the JVM cannot instantiate an interface at all.","triggerScenarios":"Calling BeanUtils.instantiate(SomeInterface.class) (or any code path that still uses the deprecated instantiate) where the Class argument is java.lang.Runnable, a repository/service interface, Map, etc. The isInterface() guard rejects it before any newInstance() attempt.","commonSituations":"Migrating legacy code that used Class.newInstance(); passing a Class loaded from config that resolves to an interface; reflection over plugin descriptors where the extension point is an interface; accidental wiring of an interface class instead of its implementation.","solutions":["Switch to the non-deprecated BeanUtils.instantiateClass(Class) and pass a concrete class.","Provide a concrete implementation class instead of the interface type.","If you must support interface-based lookup, resolve the implementation (e.g. via @Component scan or a ServiceLoader) before instantiation.","Validate !clazz.isInterface() before calling instantiate."],"exampleFix":"// before (deprecated)\nRunnable r = BeanUtils.instantiate(Runnable.class); // interface\n\n// after\nRunnable r = BeanUtils.instantiateClass(MyRunnableImpl.class);","handlingStrategy":"validation","validationCode":"if (!clazz.isInterface() && !Modifier.isAbstract(clazz.getModifiers())) {\n    BeanUtils.instantiate(clazz); // legacy path\n}","typeGuard":"static boolean instantiableLegacy(Class<?> c) {\n    return !c.isInterface() && !Modifier.isAbstract(c.getModifiers())\n        && !c.isPrimitive() && !c.isArray();\n}","tryCatchPattern":"try {\n    BeanUtils.instantiate(clazz);\n} catch (BeanInstantiationException ex) {\n    if (\"Specified class is an interface\".equals(ex.getMessage())) {\n        // resolve a concrete impl or skip\n    } else throw ex;\n}","preventionTips":["Migrate from deprecated BeanUtils.instantiate(Class) to instantiateClass(Class).","Never pass interface Class objects to instantiation APIs.","Validate isInterface() before reflective instantiation.","Resolve concrete implementations via component scan/ServiceLoader before instantiating."],"tags":["spring-beans","beanutils","instantiation","interface","deprecated"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}