{"record":{"id":"9699cfad995d5dcf","repo":"oracle/graal","slug":"illegal-argument-type-receiver-of-type-could-n","errorCode":null,"errorMessage":"Illegal argument type: receiver of type {} could not be converted to a {}","messagePattern":"Illegal argument type: receiver of type (.+?) could not be converted to a (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"compiler/src/jdk.graal.compiler.hostvmaccess/src/jdk/graal/compiler/hostvmaccess/HostVMAccess.java","lineNumber":188,"sourceCode":"                unboxedArguments[i] = argument.asBoxedPrimitive();\n            }\n        }\n        try {\n            if (isConstructor) {\n                Constructor<?> constructor = (Constructor<?>) executable;\n                return snippetReflection.forObject(constructor.newInstance(unboxedArguments));\n            } else {\n                Method reflectionMethod = (Method) executable;\n                Object unboxedReceiver;\n                if (Modifier.isStatic(reflectionMethod.getModifiers())) {\n                    unboxedReceiver = null;\n                } else {\n                    if (receiver.isNull()) {\n                        unboxedReceiver = null;\n                    } else {\n                        unboxedReceiver = snippetReflection.asObject(reflectionMethod.getDeclaringClass(), receiver);\n                        if (unboxedReceiver == null) {\n                            throw new IllegalArgumentException(\n                                            \"Illegal argument type: receiver of type \" + providers.getMetaAccess().lookupJavaType(receiver).toClassName() +\n                                                            \" could not be converted to a \" + reflectionMethod.getDeclaringClass());\n                        }\n                    }\n                }\n                JavaKind returnKind = method.getSignature().getReturnKind();\n                Object result = reflectionMethod.invoke(unboxedReceiver, unboxedArguments);\n                if (returnKind == JavaKind.Void) {\n                    return null;\n                }\n                if (returnKind.isObject()) {\n                    return snippetReflection.forObject(result);\n                } else {\n                    return snippetReflection.forBoxed(returnKind, result);\n                }\n            }\n        } catch (InstantiationException e) {\n            throw new IllegalArgumentException(e);","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/oracle/graal/blob/a66e9ccd1d7bf2552883939aa0788dfd0e294aab/compiler/src/jdk.graal.compiler.hostvmaccess/src/jdk/graal/compiler/hostvmaccess/HostVMAccess.java#L170-L206","documentation":"Thrown by HostVMAccess.invoke for instance method invocation when the receiver constant cannot be unboxed to the method's declaring class: snippetReflection.asObject(declaringClass, receiver) returned null, i.e. the receiver object is not an instance of the class that declares the method.","triggerScenarios":"Calling invoke on an instance method with a receiver constant whose runtime class does not extend reflectionMethod.getDeclaringClass() — e.g. resolving a method on interface/superclass A but passing a receiver of unrelated class B, or passing a receiver already wrapped for the wrong type.","commonSituations":"Method resolution returning a default-method or bridge on an interface while the receiver is an unrelated type; classloader/module differences producing look-alike classes with the same name; refactors where the receiver expression and the resolved method drift apart.","solutions":["Check the message: it names the receiver's actual class and the expected declaring class — resolve the method against the receiver's actual type (e.g. lookupJavaType(receiverObject).resolveMethod(...))","Ensure the object you wrap is really an instance of the declaring class before forObject","With duplicate class names across classloaders/modules, verify both sides use the same loaded class"],"exampleFix":"// before\nResolvedJavaMethod m = interfaceType.resolveMethod(method);      // declared on Iface\nhostVM.invoke(m, snippetReflection.forObject(unrelatedObj), args); // throws\n\n// after\nResolvedJavaMethod m = metaAccess.lookupJavaType(receiverObj.getClass()).resolveMethod(method);\nhostVM.invoke(m, snippetReflection.forObject(receiverObj), args);","handlingStrategy":"type-guard","validationCode":"Object recv = snippetReflection.asObject(reflectionMethod.getDeclaringClass(), receiver);\nif (recv == null) throw new IllegalArgumentException(\"receiver not a \" + reflectionMethod.getDeclaringClass());","typeGuard":"static boolean receiverMatches(ResolvedJavaMethod m, JavaConstant recv, MetaAccessProvider meta) {\n    return meta.lookupJavaType(recv).isSubtypeOf(m.getDeclaringClass());\n}","tryCatchPattern":"catch (IllegalArgumentException e) { re-resolve the method against the receiver's concrete type and retry once }","preventionTips":["Resolve methods via lookupJavaType(receiver.getClass()), not a hardcoded declaring type","Beware same-named classes across classloaders/modules","Assert receiver assignability in tests of generic invoke helpers"],"tags":["graalvm","hostvmaccess","reflection","type-mismatch"],"backgroundTag":null,"analyzedSha":"a66e9ccd1d7bf2552883939aa0788dfd0e294aab","analyzedAt":"2026-08-14T13:58:47.161Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}