{"record":{"id":"f6b503e6b07f45b4","repo":"NationalSecurityAgency/ghidra","slug":"given-field-does-not-apply-to-given-object-type","errorCode":null,"errorMessage":"Given field does not apply to given object type","messagePattern":"Given field does not apply to given object type","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DBCachedObjectStoreFactory.java","lineNumber":333,"sourceCode":"\t\tprotected final Class<VT> boxedType;\n\t\tprotected final Class<OT> objectType;\n\t\tprotected final Class<FT> fieldType;\n\t\tprotected final Field field;\n\t\tprotected final int column;\n\n\t\t/**\n\t\t * Construct a codec\n\t\t * \n\t\t * @param valueType\n\t\t * @param objectType\n\t\t * @param fieldType\n\t\t * @param field\n\t\t * @param column\n\t\t */\n\t\tpublic AbstractDBFieldCodec(Class<VT> valueType, Class<OT> objectType, Class<FT> fieldType,\n\t\t\t\tField field, int column) {\n\t\t\tif (!field.getDeclaringClass().isAssignableFrom(objectType)) {\n\t\t\t\tthrow new IllegalArgumentException(\n\t\t\t\t\t\"Given field does not apply to given object type\");\n\t\t\t}\n\t\t\tif (field.getType() != valueType) {\n\t\t\t\tthrow new IllegalArgumentException(\n\t\t\t\t\t\"Given field does not have the given type: \" + valueType + \" != \" + field);\n\t\t\t}\n\t\t\tthis.valueType = valueType;\n\t\t\t@SuppressWarnings(\"unchecked\")\n\t\t\tClass<VT> boxedType = (Class<VT>) MethodType.methodType(valueType).wrap().returnType();\n\t\t\tthis.boxedType = boxedType;\n\t\t\tthis.objectType = objectType;\n\t\t\tthis.fieldType = fieldType;\n\t\t\tthis.field = field;\n\t\t\tthis.column = column;\n\t\t}\n\n\t\t@Override\n\t\tpublic void store(OT obj, DBRecord record) {","sourceCodeStart":315,"sourceCodeEnd":351,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DBCachedObjectStoreFactory.java#L315-L351","documentation":"Thrown (unchecked IllegalArgumentException) by the AbstractDBFieldCodec constructor when the reflective Field's declaring class is not assignable from the object type (OT). A codec reads/writes a field on instances of OT, so the field must actually belong to (or be compatible with) that object class; otherwise the field could not be accessed on OT instances.","triggerScenarios":"Registering a codec that pairs a Field from class A with an object type class B where A is not in B's hierarchy — e.g. passing a field declared on a sibling/parent class that OT does not extend, or wiring a field from the wrong annotated class.","commonSituations":"Refactoring an annotated object class hierarchy so a field moved between classes but the codec registration kept the old Field; copy-pasting codec setup across classes; mixing up two similarly-named annotated object types.","solutions":["Use a Field obtained from the same class (or a supertype) that OT extends — ensure field.getDeclaringClass().isAssignableFrom(objectType).","Resolve the Field via the object class's own reflection (OT.class.getDeclaredField(...)) so the declaring class always matches.","After class-hierarchy refactors, regenerate/audit codec registrations for moved fields."],"exampleFix":"// before\nnew StringDBFieldCodec<>(WrongType.class, WrongType.class.getDeclaredField(\"name\"), col);\n// throws if WrongType is not the object type\n\n// after: obtain the field from the actual object type\nField f = MyObject.class.getDeclaredField(\"name\");\nnew StringDBFieldCodec<>(MyObject.class, f, col);","handlingStrategy":"validation","validationCode":"// Ensure the Field belongs to (a supertype of) the object type\nif (!field.getDeclaringClass().isAssignableFrom(objectType)) {\n    // wrong field/object pairing: resolve the field from objectType's hierarchy\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Obtain the Field via reflection on the actual object type (or its supertype).","After class-hierarchy refactors, re-audit codec registrations for moved fields.","Don't mix fields from sibling annotated classes."],"tags":["database","codec","reflection","field-mapping","illegal-argument","unchecked-exception"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}