oracle/graal · error · IllegalArgumentException

Could not obtain the original class for

Error message

Could not obtain the original class for {}

What it means

Thrown by HostVMAccess.asArrayConstant when SnippetReflectionProvider.originalClass(componentType) returns null for the requested component type, i.e. the ResolvedJavaType has no original (host) Class mirror available for array creation via Array.newInstance.

Solutions

  1. Resolve the component type first (e.g. type.resolve(), or obtain it via metaAccess.lookupJavaType(realClass))
  2. If the type is a substitution, look up the original type via the snippet reflection's substitution machinery before calling asArrayConstant
  3. Pass a component type derived from an existing host Class mirror to guarantee originalClass succeeds

Example fix

// before
hostVM.asArrayConstant(unresolvedOrSubstitutedType, e0, e1);  // throws

// after
ResolvedJavaType resolved = metaAccess.lookupJavaType(String.class); // concrete host class
hostVM.asArrayConstant(resolved, e0, e1);
Defensive patterns

Strategy: validation

Validate before calling

Class<?> c = snippetReflection.originalClass(componentType);
if (c == null) throw new IllegalArgumentException("no original class for " + componentType);

Type guard

static boolean hasOriginalClass(ResolvedJavaType t, SnippetReflectionProvider s) { return s.originalClass(t) != null; }

Try / catch

catch (IllegalArgumentException e) { resolve/substitute the type via metaAccess and retry once }

Prevention

When it happens

Trigger: Calling asArrayConstant(componentType, elements) where componentType is a hotswapped/substituted type, an unresolved or array-of-array type the snippet reflection cannot map, or a type from a class loading context invisible to the host. Note this checks the component type only — a null elements array still triggers a later NullPointerException inside the loop.

Common situations: Building host-side array constants during snippet unfolding for types that only exist in the compiled/guest world; using a MetaAccess from a different universe (e.g. image heap vs host); passing an unresolved ResolvedJavaType obtained lazily without a resolution step.

Related errors


AI-assisted analysis of oracle/graal@a66e9ccd1d (2026-08-14). Data as JSON: /api/errors/f5b589b372562737. Report an issue: GitHub.

Appendix: source

Thrown at compiler/src/jdk.graal.compiler.hostvmaccess/src/jdk/graal/compiler/hostvmaccess/HostVMAccess.java:278

    @SuppressWarnings("deprecation")
    static <T extends AccessibleObject & Member> void makeAccessible(T accessibleMember) {
        try {
            if (!accessibleMember.isAccessible()) {
                accessibleMember.setAccessible(true);
            }
        } catch (InaccessibleObjectException e) {
            Class<?> declaringClass = accessibleMember.getDeclaringClass();
            ModuleSupport.addOpens(HostVMAccess.class.getModule(), declaringClass.getModule(), declaringClass.getPackageName());
            accessibleMember.setAccessible(true);
        }
    }

    @Override
    public JavaConstant asArrayConstant(ResolvedJavaType componentType, JavaConstant... elements) {
        SnippetReflectionProvider snippetReflection = providers.getSnippetReflection();
        Class<?> componentClass = snippetReflection.originalClass(componentType);
        if (componentClass == null) {
            throw new IllegalArgumentException("Could not obtain the original class for " + componentType);
        }
        Object array = Array.newInstance(componentClass, elements.length);
        for (int i = 0; i < elements.length; i++) {
            doWriteArrayElement(array, componentType, i, elements[i]);
        }
        return snippetReflection.forObject(array);
    }

    /**
     * Host mode materializes the primitive array with reflection and then wraps it as a
     * {@link JavaConstant} through {@link SnippetReflectionProvider#forObject(Object)}.
     */
    @Override
    public JavaConstant createPrimitiveArray(JavaKind kind, int length) {
        if (kind == null || !kind.isPrimitive() || kind == JavaKind.Void) {
            throw new IllegalArgumentException("Expected a non-void primitive kind, got " + kind);
        }
        Object array = Array.newInstance(kind.toJavaClass(), length);

View on GitHub (pinned to a66e9ccd1d)