{"record":{"id":"55514b1e32ceb89d","repo":"MyCATApache/Mycat-Server","slug":"cannot-construct-type-as-it-does-not-have-a-no","errorCode":null,"errorMessage":"Cannot construct ${type} as it does not have a no-args constructor","messagePattern":"Cannot construct (.+?) as it does not have a no-args constructor","errorType":"exception","errorClass":"ObjectAccessException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/config/util/ReflectionProvider.java","lineNumber":67,"sourceCode":"    private transient Map<Class<?>, byte[]> serializedDataCache = Collections\n            .synchronizedMap(new HashMap<Class<?>, byte[]>());\n    private transient FieldDictionary fieldDictionary = new FieldDictionary();\n\n    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","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/config/util/ReflectionProvider.java#L49-L85","documentation":"ReflectionProvider.newInstance tries each declared constructor in turn; if none is a no-args constructor it falls back to serialization-based instantiation for Serializable types, and otherwise throws ObjectAccessException. The class cannot be instantiated reflectively without a no-arg constructor (and is not Serializable).","triggerScenarios":"Calling newInstance(type) on a class that only defines parameterized constructors, is not Serializable, and has no accessible no-arg constructor — commonly during config-bean deserialization.","commonSituations":"Bean classes written with required-argument constructors (e.g. immutable beans) then fed to the XML/reflection config loader; inner/non-static classes whose constructors take an enclosing instance; adding a constructor to a previously default-constructed bean.","solutions":["Add a public no-arg constructor to the target class","Make the class implement java.io.Serializable so the serialization fallback can be used","If instantiation is intentional with arguments, call a different factory/API that supplies constructor arguments"],"exampleFix":"// before\npublic class DataSourceCfg { public DataSourceCfg(String host) {...} }\n// after\npublic class DataSourceCfg { public DataSourceCfg() {} public DataSourceCfg(String host) {...} }","handlingStrategy":"type-guard","validationCode":"static boolean canNewInstance(Class<?> t) {\n  if (t.isInterface() || Modifier.isAbstract(t.getModifiers())) return false;\n  try { t.getDeclaredConstructor(); return true; } catch (NoSuchMethodException e) { return Serializable.class.isAssignableFrom(t); }\n}","typeGuard":"boolean instantiable = c != null && !c.isInterface() && !Modifier.isAbstract(c.getModifiers());","tryCatchPattern":"try { return ReflectionProvider.newInstance(type); } catch (ObjectAccessException e) { throw new ConfigException(\"bean \" + type.getName() + \" needs a public no-arg constructor\", e); }","preventionTips":["Give every config bean a public no-arg constructor","Keep bean classes top-level and static (avoid inner classes)","Don't make configurable bean classes abstract"],"tags":["reflection","constructor","instantiation"],"backgroundTag":"missing-default-constructor","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"}