Annotation Interface HostAccess.Optimized

Enclosing class:
HostAccess

@Documented @Retention(RUNTIME) @Target(TYPE) public static @interface HostAccess.Optimized
Requests an implementation optimized for invocation speed when converting a guest value to this interface with Value.as(Class). Use this annotation for interfaces whose methods are called frequently from Java.

Interfaces may declare multiple methods. In Native Image, annotated interfaces included in the image are available for conversion without additional proxy configuration.

@HostAccess.Optimized
@FunctionalInterface
public interface Adder {
    int add(int left, int right);
}

try (Context context = Context.newBuilder("js")
                .allowHostAccess(HostAccess.EXPLICIT)
                .build()) {
    Adder adder = context.eval("js", "(x, y) => x + y").as(Adder.class);
    assert adder.add(40, 2) == 42;
}

This annotation does not authorize host access. Conversion remains subject to the configured HostAccess policy. It also does not guarantee allocation-free conversion or invocation. Only non-sealed interfaces are currently supported. The interface and reference types in its method signatures must be public and their packages must be exported to unnamed modules.

Since:
25.7