{"record":{"id":"3dfede4f43828cc7","repo":"apache/cassandra","slug":"expected-instance-of-specificclass-getname","errorCode":null,"errorMessage":"Expected instance of ${specificClass.getName()}","messagePattern":"Expected instance of (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/utils/CastingSerializer.java","lineNumber":66,"sourceCode":"\n        private Versioned(Class<Specific> specificClass, VersionedSerializer<Specific, Version> specificSerializer)\n        {\n            this.specificClass = specificClass;\n            this.specificSerializer = specificSerializer;\n        }\n\n        @Override\n        public void serialize(Generic generic, DataOutputPlus out, Version version) throws IOException\n        {\n            specificSerializer.serialize(specificClass.cast(generic), out, version);\n        }\n\n        @Override\n        public Generic deserialize(DataInputPlus in, Version version) throws IOException\n        {\n            Generic result = specificSerializer.deserialize(in, version);\n            if (result != null && !specificClass.isInstance(result))\n                throw new IllegalStateException(\"Expected instance of \" + specificClass.getName());\n            return result;\n        }\n\n        @Override\n        public long serializedSize(Generic generic, Version version)\n        {\n            return specificSerializer.serializedSize(specificClass.cast(generic), version);\n        }\n    }\n\n    private static final class Unversioned<Generic, Specific extends Generic> implements UnversionedSerializer<Generic>\n    {\n        private final Class<Specific> specificClass;\n        private final UnversionedSerializer<Specific> specificSerializer;\n\n        private Unversioned(Class<Specific> specificClass, UnversionedSerializer<Specific> specificSerializer)\n        {\n            this.specificClass = specificClass;","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/utils/CastingSerializer.java#L48-L84","documentation":"CastingSerializer wraps a typed serializer to adapt it to a Generic interface. On deserialize it verifies the underlying specific serializer actually produced an instance of the expected class; otherwise the wire data does not match the declared type and it throws IllegalStateException.","triggerScenarios":"Deserializing bytes with a CastingSerializer<SomeType> whose wrapped serializer returns a different concrete type — corrupt or wrongly-routed serialized data, or a wrapped serializer whose generic type parameter does not match specificClass.","commonSituations":"Schema/serialization-version drift after upgrades, reading data written by a different Cassandra version or component, misuse of the generic API with mismatched type parameters.","solutions":["Verify the wrapped serializer actually serializes the class passed as specificClass","Check that the data being read was written by the matching serializer/version","If this indicates corrupt data, validate the sstable/message source rather than catching the error"],"exampleFix":"// before\nnew CastingSerializer<>(stringSerializer, Integer.class); // mismatch\n// after\nnew CastingSerializer<>(stringSerializer, String.class);","handlingStrategy":"try-catch","validationCode":"// Confirm serializer/class pairing before use\nassert expectedClass.isInstance(\n    specificSerializer.getClass().getName().contains(expectedClass.getSimpleName())\n        ? expectedClass : null) || true; // prefer unit tests round-tripping serialize->deserialize","typeGuard":"boolean isExpected(Generic g, Class<?> expected) {\n    return g != null && expected.isInstance(g);\n}","tryCatchPattern":"try {\n    Generic g = serializer.deserialize(in);\n} catch (IllegalStateException e) {\n    throw new IOException(\"Corrupt or mismatched payload: \" + e.getMessage(), e);\n}","preventionTips":["Keep specificClass and the wrapped serializer's type parameter consistent","Round-trip test serialize/deserialize in unit tests","Check serialization version compatibility after upgrades"],"tags":["serialization","deserialization","type-mismatch","java"],"backgroundTag":"type-mismatch","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}