{"record":{"id":"7e21981ef570af81","repo":"Tencent/matrix","slug":"illegal-clazz-type-clazz-getclass","errorCode":null,"errorMessage":"Illegal clazz type: ${clazz.getClass()}","messagePattern":"Illegal clazz type: (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"matrix/matrix-android/matrix-commons/src/main/java/com/tencent/matrix/javalib/util/ReflectUtil.java","lineNumber":35,"sourceCode":"package com.tencent.matrix.javalib.util;\n\n\nimport java.lang.reflect.Field;\nimport java.lang.reflect.Method;\n\n/**\n * Created by caichongyang on 17/7/4.\n */\n\npublic final class ReflectUtil {\n    public static Field getDeclaredFieldRecursive(Object clazz, String fieldName) throws NoSuchFieldException, ClassNotFoundException {\n        Class<?> realClazz = null;\n        if (clazz instanceof String) {\n            realClazz = Class.forName((String) clazz);\n        } else if (clazz instanceof Class) {\n            realClazz = (Class<?>) clazz;\n        } else {\n            throw new IllegalArgumentException(\"Illegal clazz type: \" + clazz.getClass());\n        }\n        Class<?> currClazz = realClazz;\n        while (true) {\n            try {\n                Field field = currClazz.getDeclaredField(fieldName);\n                field.setAccessible(true);\n                return field;\n            } catch (NoSuchFieldException e) {\n                if (currClazz.equals(Object.class)) {\n                    throw e;\n                }\n                currClazz = currClazz.getSuperclass();\n            }\n        }\n    }\n\n    public static Method getDeclaredMethodRecursive(Object clazz, String methodName, Class<?>... argTypes) throws NoSuchMethodException, ClassNotFoundException {\n        Class<?> realClazz = null;","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/Tencent/matrix/blob/3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7/matrix/matrix-android/matrix-commons/src/main/java/com/tencent/matrix/javalib/util/ReflectUtil.java#L17-L53","documentation":"ReflectUtil.getDeclaredFieldRecursive accepts the declaring class either as a String (class name, loaded via Class.forName) or as a Class object; anything else is rejected with IllegalArgumentException 'Illegal clazz type: X'. The method then walks the superclass chain to find the declared field.","triggerScenarios":"Calling getDeclaredFieldRecursive with an object that is neither String nor Class — e.g. passing an instance instead of its getClass(), a Method/Field object, or a null that trips the else branch.","commonSituations":"Refactoring call sites where an instance variable replaced a class literal; generic APIs forwarding Object blindly; tests passing mock instances.","solutions":["Pass a Class literal (MyClass.class) or the fully-qualified class name string.","If you have an instance, pass obj.getClass() instead of obj.","Add a null/instanceof check at the call site before invoking reflection.","If the class may not be on the classpath, ensure the String form resolves (Class.forName failure would surface separately)."],"exampleFix":"// before\nField f = ReflectUtil.getDeclaredFieldRecursive(someInstance, \"mField\");\n// after\nField f = ReflectUtil.getDeclaredFieldRecursive(someInstance.getClass(), \"mField\");","handlingStrategy":"type-guard","validationCode":"if (!(clazz instanceof String) && !(clazz instanceof Class)) throw new IllegalArgumentException(\"clazz must be Class or String\");","typeGuard":"static boolean isValidClassRef(Object clazz) {\n    return clazz instanceof String || clazz instanceof Class<?>;\n}","tryCatchPattern":"try {\n    Field f = ReflectUtil.getDeclaredFieldRecursive(clazz, fieldName);\n} catch (IllegalArgumentException e) {\n    Log.w(TAG, \"bad clazz arg: \" + e.getMessage());\n}","preventionTips":["Pass MyClass.class or \"com.example.MyClass\", never an instance.","Use obj.getClass() when starting from an instance.","Keep a single reflection wrapper API and validate args there.","Watch for refactors that swap a Class parameter for an instance."],"tags":["java","reflection","argument-validation"],"backgroundTag":"invalid-argument-value","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"}