{"record":{"id":"0a49718001880927","repo":"MyCATApache/Mycat-Server","slug":"cannot-construct-type","errorCode":null,"errorMessage":"Cannot construct ${type}","messagePattern":"Cannot construct (.+?)","errorType":"exception","errorClass":"ObjectAccessException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/config/util/ReflectionProvider.java","lineNumber":71,"sourceCode":"    public Object newInstance(Class<?> type) {\n        try {\n            Constructor<?>[] c = type.getDeclaredConstructors();\n            for (int i = 0; i < c.length; i++) {\n                if (c[i].getParameterTypes().length == 0) {\n                    if (!Modifier.isPublic(c[i].getModifiers())) {\n                        c[i].setAccessible(true);\n                    }\n                    return c[i].newInstance(new Object[0]);\n                }\n            }\n            if (Serializable.class.isAssignableFrom(type)) {\n                return instantiateUsingSerialization(type);\n            } else {\n                throw new ObjectAccessException(\"Cannot construct \" + type.getName()\n                        + \" as it does not have a no-args constructor\");\n            }\n        } catch (InstantiationException e) {\n            throw new ObjectAccessException(\"Cannot construct \" + type.getName(), e);\n        } catch (IllegalAccessException e) {\n            throw new ObjectAccessException(\"Cannot construct \" + type.getName(), e);\n        } catch (InvocationTargetException e) {\n            if (e.getTargetException() instanceof RuntimeException) {\n                throw (RuntimeException) e.getTargetException();\n            } else if (e.getTargetException() instanceof Error) {\n                throw (Error) e.getTargetException();\n            } else {\n                throw new ObjectAccessException(\"Constructor for \" + type.getName() + \" threw an exception\",\n                        e.getTargetException());\n            }\n        }\n    }\n\n    public void visitSerializableFields(Object object, Visitor visitor) {\n        for (Iterator<Field> iterator = fieldDictionary.serializableFieldsFor(object.getClass()); iterator.hasNext();) {\n            Field field = iterator.next();\n            if (!fieldModifiersSupported(field)) {","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/config/util/ReflectionProvider.java#L53-L89","documentation":"ReflectionProvider.newInstance wraps InstantiationException in ObjectAccessException('Cannot construct <type>'). InstantiationException occurs when the resolved constructor is abstract-interface instantiation, or the constructor is not accessible/ class is abstract, etc. The root cause is reflection being unable to actually run the constructor.","triggerScenarios":"Calling newInstance(type) on an abstract class or interface that (surprisingly) passed the constructor scan, or where c[i].newInstance() fails with InstantiationException; constructor not visible to the caller's classloader context.","commonSituations":"Configuring an abstract base class or interface name where a concrete implementation class is expected (e.g. a <class> attribute pointing at an interface); class made abstract in a refactor while old config still references it.","solutions":["Replace the abstract class/interface reference in config with a concrete instantiable class","Make the class concrete (remove abstract modifier) if it should be instantiated","Check constructor visibility/accessibility and that the class has a workable no-arg constructor"],"exampleFix":"// before (config)\n<class>com.example.AbstractHandler</class>\n// after\n<class>com.example.DefaultHandler</class>","handlingStrategy":"type-guard","validationCode":"if (Modifier.isAbstract(type.getModifiers()) || type.isInterface()) throw new ConfigException(type.getName() + \" is abstract; configure a concrete subclass\");","typeGuard":"boolean concrete = type != null && !type.isInterface() && !Modifier.isAbstract(type.getModifiers());","tryCatchPattern":"try { return ReflectionProvider.newInstance(type); } catch (ObjectAccessException e) { throw new ConfigException(\"cannot instantiate \" + type.getName() + \": use a concrete class\", e); }","preventionTips":["Never configure interfaces or abstract classes where instances are needed","After refactors, re-verify class names referenced in config are still concrete","Maintain a factory mapping for pluggable types instead of raw class names"],"tags":["reflection","instantiation","abstract-class"],"backgroundTag":"invalid-config-value","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}