{"record":{"id":"64af3b5247be1028","repo":"oracle/graal","slug":"expected-an-array-constant-got","errorCode":null,"errorMessage":"Expected an array constant, got {}","messagePattern":"Expected an array constant, got (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"compiler/src/jdk.graal.compiler.hostvmaccess/src/jdk/graal/compiler/hostvmaccess/HostVMAccess.java","lineNumber":361,"sourceCode":"        if (componentType.isPrimitive()) {\n            if (componentType.getJavaKind() != element.getJavaKind()) {\n                throw new IllegalArgumentException(\"Element \" + element + \" should be a \" + componentType.getJavaKind() + \", got \" + element.getJavaKind());\n            }\n            unwrappedValue = element.asBoxedPrimitive();\n        } else {\n            if (!element.getJavaKind().isObject()) {\n                throw new IllegalArgumentException(\"Element \" + element + \" should be an object, got \" + componentType);\n            }\n            unwrappedValue = providers.getSnippetReflection().asObject(Object.class, element);\n        }\n        Array.set(array, index, unwrappedValue);\n    }\n\n    @Override\n    public void writeArrayElement(JavaConstant array, int index, JavaConstant element) {\n        ResolvedJavaType arrayType = getProviders().getMetaAccess().lookupJavaType(array);\n        if (arrayType == null || !arrayType.isArray()) {\n            throw new IllegalArgumentException(\"Expected an array constant, got \" + array);\n        }\n        Object asObject = providers.getSnippetReflection().asObject(Object.class, array);\n        if (asObject == null) {\n            throw new IllegalArgumentException(\"Could not unwrap array: \" + array);\n        }\n        doWriteArrayElement(asObject, arrayType.getComponentType(), index, element);\n    }\n\n    @Override\n    public ResolvedJavaMethod asResolvedJavaMethod(Constant constant) {\n        SnippetReflectionProvider snippetReflection = providers.getSnippetReflection();\n        Executable executable = snippetReflection.asObject(Executable.class, (JavaConstant) constant);\n        if (executable != null) {\n            return providers.getMetaAccess().lookupJavaMethod(executable);\n        }\n        return null;\n    }\n","sourceCodeStart":343,"sourceCodeEnd":379,"githubUrl":"https://github.com/oracle/graal/blob/a66e9ccd1d7bf2552883939aa0788dfd0e294aab/compiler/src/jdk.graal.compiler.hostvmaccess/src/jdk/graal/compiler/hostvmaccess/HostVMAccess.java#L343-L379","documentation":"HostVMAccess.writeArrayElement first resolves the constant's type via metaAccess.lookupJavaType(array) and verifies it is an array; this IllegalArgumentException fires when the passed JavaConstant is not an array constant at all (a scalar, null, or a non-array object). It guards Array.set against an IllegalArgumentException from the reflection layer with a clearer message.","triggerScenarios":"Calling writeArrayElement on a constant whose ResolvedJavaType is null (e.g. JavaConstant.NULL_POINTER or an illegal/foreign constant) or whose type is a class rather than an array type (storing into what the caller believed was an array but is a plain object or a boxed primitive).","commonSituations":"Data-driven pipelines where a slot can hold either a scalar or an array and the array path is chosen unconditionally; passing a constant obtained from a different JVMCI runtime/backend whose types this MetaAccessProvider cannot resolve (lookupJavaType returns null).","solutions":["Check the constant before writing: resolve its type and verify type != null && type.isArray() && type.getComponentType() != null","Fix the upstream logic so the array-write path only receives actual array constants","If the value may legitimately be a scalar, branch to a field/store API instead of writeArrayElement"],"exampleFix":"// before\nvmAccess.writeArrayElement(maybeArrayConst, idx, valueConst);\n\n// after\nResolvedJavaType t = providers.getMetaAccess().lookupJavaType(maybeArrayConst);\nif (t == null || !t.isArray()) {\n    throw new IllegalArgumentException(\"Not an array constant: \" + maybeArrayConst);\n}\nvmAccess.writeArrayElement(maybeArrayConst, idx, valueConst);","handlingStrategy":"validation","validationCode":"ResolvedJavaType t = providers.getMetaAccess().lookupJavaType(candidate);\nif (t == null || !t.isArray()) {\n    // not an array constant - do not call writeArrayElement\n}","typeGuard":"boolean isArrayConstant(JavaConstant c, MetaAccessProvider meta) {\n    ResolvedJavaType t = meta.lookupJavaType(c);\n    return t != null && t.isArray();\n}","tryCatchPattern":null,"preventionTips":["Resolve the constant's type once and branch scalar vs array before dispatching","Never pass JavaConstant.NULL_POINTER or illegal constants to array APIs","In data-driven code, tag slots with their expected shape instead of guessing"],"tags":["graalvm","vmaccess","array","validation"],"backgroundTag":null,"analyzedSha":"a66e9ccd1d7bf2552883939aa0788dfd0e294aab","analyzedAt":"2026-08-14T13:58:47.161Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}