{"record":{"id":"5944a0e707858723","repo":"oracle/graal","slug":"for-static-fields-the-receiver-argument-must-be-n","errorCode":null,"errorMessage":"For static fields, the receiver argument must be null","messagePattern":"For static fields, the receiver argument must be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"compiler/src/jdk.graal.compiler.hostvmaccess/src/jdk/graal/compiler/hostvmaccess/HostVMAccess.java","lineNumber":227,"sourceCode":"            if (cause instanceof HostProxyExceptionImpl) {\n                throw new InvocationException(cause.getCause());\n            }\n            throw new InvocationException(snippetReflection.forObject(cause), cause);\n        } catch (IllegalAccessException e) {\n            throw new RuntimeException(\"Should not reach here\", e);\n        }\n    }\n\n    @Override\n    public void writeField(ResolvedJavaField field, JavaConstant receiver, JavaConstant value) {\n        SnippetReflectionProvider snippetReflection = providers.getSnippetReflection();\n        Field reflectionField = snippetReflection.originalField(field);\n        makeAccessible(reflectionField);\n        var fieldKind = field.getJavaKind();\n\n        if (Modifier.isStatic(reflectionField.getModifiers())) {\n            if (receiver != null) {\n                throw new IllegalArgumentException(\"For static fields, the receiver argument must be null\");\n            }\n        } else if (receiver == null) {\n            throw new NullPointerException(\"For instance fields, the receiver argument must not be null\");\n        } else if (receiver.isNull()) {\n            throw new IllegalArgumentException(\"For instance fields, the receiver argument must not represent a null constant\");\n        }\n\n        Object unboxedValue;\n        if (fieldKind.isObject()) {\n            unboxedValue = snippetReflection.asObject(reflectionField.getType(), value);\n        } else {\n            assert fieldKind.isPrimitive();\n            if (fieldKind != value.getJavaKind()) {\n                throw new IllegalArgumentException(\"Expected value kind \" + fieldKind + \" but got \" + value.getJavaKind());\n            }\n            unboxedValue = value.asBoxedPrimitive();\n        }\n","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/oracle/graal/blob/a66e9ccd1d7bf2552883939aa0788dfd0e294aab/compiler/src/jdk.graal.compiler.hostvmaccess/src/jdk/graal/compiler/hostvmaccess/HostVMAccess.java#L209-L245","documentation":"Thrown by HostVMAccess.writeField when writing a static field with a non-null receiver constant. Static field access needs no object, so passing a receiver is a caller contract violation, mirroring the equivalent rule in invoke for static methods.","triggerScenarios":"Calling writeField(staticField, receiver, value) with any non-null JavaConstant as receiver, where reflectionField has the static modifier.","commonSituations":"Shared field-writing helper that always computes a receiver (forObject(holder)) regardless of staticness; refactor turning an instance field static without updating call sites.","solutions":["Pass null as receiver for static fields","Branch on Modifier.isStatic(reflectionField.getModifiers()) — or field.isStatic() on the ResolvedJavaField — before building the receiver, exactly as the guard does"],"exampleFix":"// before\nhostVM.writeField(staticField, snippetReflection.forObject(holder), value);  // throws\n\n// after\nJavaConstant receiver = field.isStatic() ? null : snippetReflection.forObject(holder);\nhostVM.writeField(staticField, receiver, value);","handlingStrategy":"validation","validationCode":"if (field.isStatic() && receiver != null) {\n    throw new IllegalArgumentException(\"receiver must be null for static field \" + field);","typeGuard":"static boolean needsReceiver(ResolvedJavaField f) { return !f.isStatic(); }","tryCatchPattern":"catch (IllegalArgumentException e) { drop the receiver for static fields and re-invoke }","preventionTips":["Branch on field.isStatic() before building the receiver constant","Centralize field writes in a helper that owns the rule","Update call sites when a field becomes static"],"tags":["graalvm","hostvmaccess","reflection","fields","api-contract"],"backgroundTag":null,"analyzedSha":"a66e9ccd1d7bf2552883939aa0788dfd0e294aab","analyzedAt":"2026-08-14T13:58:47.161Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}