{"record":{"id":"5536cdf02408943b","repo":"MyCATApache/Mycat-Server","slug":"cannot-create-type-by-jdk-serialization","errorCode":null,"errorMessage":"Cannot create ${type} by JDK serialization","messagePattern":"Cannot create (.+?) by JDK serialization","errorType":"exception","errorClass":"ObjectAccessException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/config/util/ReflectionProvider.java","lineNumber":167,"sourceCode":"                DataOutputStream stream = new DataOutputStream(bytes);\n                stream.writeShort(ObjectStreamConstants.STREAM_MAGIC);\n                stream.writeShort(ObjectStreamConstants.STREAM_VERSION);\n                stream.writeByte(ObjectStreamConstants.TC_OBJECT);\n                stream.writeByte(ObjectStreamConstants.TC_CLASSDESC);\n                stream.writeUTF(type.getName());\n                stream.writeLong(ObjectStreamClass.lookup(type).getSerialVersionUID());\n                stream.writeByte(2); // classDescFlags (2 = Serializable)\n                stream.writeShort(0); // field count\n                stream.writeByte(ObjectStreamConstants.TC_ENDBLOCKDATA);\n                stream.writeByte(ObjectStreamConstants.TC_NULL);\n                data = bytes.toByteArray();\n                serializedDataCache.put(type, data);\n            }\n\n            ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(data));\n            return in.readObject();\n        } catch (IOException e) {\n            throw new ObjectAccessException(\"Cannot create \" + type.getName() + \" by JDK serialization\", e);\n        } catch (ClassNotFoundException e) {\n            throw new ObjectAccessException(\"Cannot find class \" + e.getMessage());\n        }\n    }\n\n    private boolean fieldModifiersSupported(Field field) {\n        return !(Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers()));\n    }\n\n    private void validateFieldAccess(Field field) {\n        if (Modifier.isFinal(field.getModifiers())) {\n            if (JVMInfo.is15()) {\n                field.setAccessible(true);\n            } else {\n                throw new ObjectAccessException(\"Invalid final field \" + field.getDeclaringClass().getName() + \".\"\n                        + field.getName());\n            }\n        }","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/config/util/ReflectionProvider.java#L149-L185","documentation":"instantiateUsingSerialization creates an instance of a type via JDK serialization: it writes an empty instance's serialized form and reads it back to bypass missing constructors. If an IOException occurs during that readObject, the provider throws ObjectAccessException 'Cannot create <type> by JDK serialization'. This usually means the target class is not actually serializable-compatible at that moment or the cached data is corrupt.","triggerScenarios":"ObjectInputStream.readObject throws IOException because the class's serialized form changed (serialVersionUID mismatch), the class is Externalizable with a failing no-arg constructor, or the cached serialized byte[] is truncated/corrupt.","commonSituations":"Deserializing a class whose serialVersionUID changed after a library upgrade; class not implementing Serializable but routed through this path; corrupt cached bytes in serializedDataCache.","solutions":["Confirm the target class implements Serializable and defines a stable serialVersionUID","Clear/rebuild the serializedDataCache after upgrading the class","Ensure the class's no-arg constructor (for Externalizable) does not throw","If the class cannot be JDK-serialized, provide a working constructor path instead of newInstance-serialization","Read e.getCause() for the underlying stream/serialization reason"],"exampleFix":"// before\nclass ConfigPoint { private String name; } // no serialVersionUID\n// after\nclass ConfigPoint implements Serializable {\n    private static final long serialVersionUID = 1L;\n    private String name;\n}","handlingStrategy":"try-catch","validationCode":"// before instantiation\nif (!(Serializable.class.isAssignableFrom(type))) {\n    throw new IllegalArgumentException(type + \" is not Serializable\");\n}\nlong svuid = type.getDeclaredField(\"serialVersionUID\").getLong(null); // verify stable UID","typeGuard":null,"tryCatchPattern":"try {\n    Object o = provider.newInstance(type);\n} catch (ObjectAccessException e) {\n    // clear cache and retry once\n    Object o2 = provider.newInstance(type);\n}","preventionTips":["Declare explicit serialVersionUID in all serializable classes","Invalidate serialized-data caches after class changes","Ensure class implements Serializable before routing through this path","Verify no-arg constructors do not throw"],"tags":["serialization","reflection","instantiation"],"backgroundTag":"invalid-state-transition","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"}