{"record":{"id":"6000b4d514d14979","repo":"Tencent/matrix","slug":"unable-to-cast-object","errorCode":null,"errorMessage":"unable to cast object","messagePattern":"unable to cast object","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"matrix/matrix-android/matrix-android-lib/src/main/java/com/tencent/matrix/util/ReflectFiled.java","lineNumber":61,"sourceCode":"        return get(false);\n    }\n\n    @SuppressWarnings(\"unchecked\")\n    public synchronized Type get(boolean ignoreFieldNoExist)\n            throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException {\n        prepare();\n        if (mField == null) {\n            if (!ignoreFieldNoExist) {\n                throw new NoSuchFieldException();\n            }\n            MatrixLog.w(TAG, String.format(\"Field %s is no exists.\", mFieldName));\n            return null;\n        }\n        Type fieldVal = null;\n        try {\n            fieldVal = (Type) mField.get(null);\n        } catch (ClassCastException e) {\n            throw new IllegalArgumentException(\"unable to cast object\");\n        }\n        return fieldVal;\n    }\n\n    public synchronized Type get(boolean ignoreFieldNoExist, Object instance)\n            throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException {\n        prepare();\n        if (mField == null) {\n            if (!ignoreFieldNoExist) {\n                throw new NoSuchFieldException();\n            }\n            MatrixLog.w(TAG, String.format(\"Field %s is no exists.\", mFieldName));\n            return null;\n        }\n        Type fieldVal = null;\n        try {\n            fieldVal = (Type) mField.get(instance);\n        } catch (ClassCastException e) {","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/Tencent/matrix/blob/3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7/matrix/matrix-android/matrix-android-lib/src/main/java/com/tencent/matrix/util/ReflectFiled.java#L43-L79","documentation":"The static-field variant of ReflectFiled.get() casts mField.get(null) to the generic Type and converts a ClassCastException into IllegalArgumentException(\"unable to cast object\"). It fires when the actual field value's runtime type is not assignable to the declared generic Type.","triggerScenarios":"Calling get() where the resolved static field's value cannot be cast to the generic type parameter used when constructing ReflectFiled (e.g. declared Type=String but field holds an Integer).","commonSituations":"Type parameters inferred incorrectly (raw types, unchecked casts); field type changed between library/app versions; using ReflectFiled against a field whose type was assumed rather than verified.","solutions":["Align the generic type parameter with the field's actual declared type","Verify the field's type via mField.getType()/Field#getClass of the value before casting","Use getWithoutThrow() to receive null instead of an exception on mismatch","Catch IllegalArgumentException around the get() call and handle the fallback path"],"exampleFix":"// before\nReflectFiled<String> f = new ReflectFiled<>(Foo.class, \"count\"); // count is int\nString v = f.get();\n\n// after\nReflectFiled<Integer> f = new ReflectFiled<>(Foo.class, \"count\");\nInteger v = f.get();","handlingStrategy":"type-guard","validationCode":"Field f = Foo.class.getDeclaredField(\"count\");\nif (!Number.class.isAssignableFrom(f.getType())) {\n    // adjust the generic type parameter of ReflectFiled\n}","typeGuard":"Object raw = Foo.class.getField(name).get(null);\nif (raw instanceof String) { /* safe to use ReflectFiled<String> */ }","tryCatchPattern":"try {\n    Type v = reflectFiled.get();\n} catch (IllegalArgumentException e) {\n    if (\"unable to cast object\".equals(e.getMessage())) {\n        v = defaultValue;\n    } else { throw e; }\n}","preventionTips":["Match ReflectFiled's generic parameter to the field's declared type","Prefer getWithoutThrow() when a mismatch should not crash","Re-check field types after upgrading the reflected library"],"tags":["reflection","android","type-mismatch","generics"],"backgroundTag":"type-mismatch","analyzedSha":"3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7","analyzedAt":"2026-09-08T08:01:39.722Z","contentChangedAt":"2026-09-08T08:01:39.722Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}