Weird behaviour of Java MethodHandle.invokeExact

Here is a minimal working example (requires Java 22 or later):

(just using libc free for elaborating the behaviour)

import java.lang.foreign.*;
import java.lang.invoke.MethodHandle;

public class Main {
    public static final Linker nativeLinker = Linker.nativeLinker();
    public static final SymbolLookup stdlibLookup = nativeLinker.defaultLookup();
    public static final SymbolLookup loaderLookup = SymbolLookup.loaderLookup();

    private static final FunctionDescriptor DESCRIPTOR$free = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS);
    private static final MethodHandle HANDLE$free =
            loaderLookup.find("free")
                    .or(() -> stdlibLookup.find("free"))
                    .map(symbolSegment -> nativeLinker.downcallHandle(symbolSegment, DESCRIPTOR$free))
                    .orElseThrow(() -> new RuntimeException("libc function free not found but y?"));

    public static void free(MemorySegment address) {
        try {
            // I want to allow user code to use Java null and MemorySegment.NULL (C NULL) interchangeably
            HANDLE$free.invokeExact(address != null ? address : MemorySegment.NULL);
        } catch (Throwable throwable) {
            throwable.printStackTrace(System.err);
        }
    }

    public static void main(String[] args) {
        free(null); // free(MemorySegment.NULL) will produce same result
    }
}

Run program and there will be an exception:

java.lang.invoke.WrongMethodTypeException: handle's method type (MemorySegment)void but found (Object)void
    at java.base/java.lang.invoke.Invokers.newWrongMethodTypeException(Invokers.java:521)
    at java.base/java.lang.invoke.Invokers.checkExactType(Invokers.java:530)
    at Main.free(Main.java:19)
    at Main.main(Main.java:26)

Java complains that the argument used to call invokeExact is an Object, not MemorySegment. However, the expression

address != null ? address : MemorySegment.NULL

should have type MemorySegment of course. And if we make a small modification to the code:

    public static void free(MemorySegment address) {
        try {
            MemorySegment temp = address != null ? address : MemorySegment.NULL;
            HANDLE$free.invokeExact(temp);
        } catch (Throwable throwable) {
            throwable.printStackTrace(System.err);
        }
    }

it works correctly.

Also

  • HANDLE$free.invoke(address != null ? address : MemorySegment.NULL) works
  • HANDLE$free.invokeExact((MemorySegment)(address != null ? address : MemorySegment.NULL)) works, but
  • HANDLE$free.invokeExact((address != null ? address : MemorySegment.NULL)) does not work

Is this some kind of deliberate design, implementation limitation or JVM bugs?

Note that since invokeExact is signature polymorphic, it is the compiler’s job to determine the method type of the method you are trying to call, based on the argument expressions. It doesn’t matter what type the expression evaluates to at runtime, if the compiler finds a non-matching method type.

Indeed, the type of the conditional expression (the ternary operator expression) is Object.

From JLS 15.25.3,

A reference conditional expression is a poly expression if it appears in an assignment context or an invocation context (§5.2. §5.3). Otherwise, it is a standalone expression.

The type of a poly reference conditional expression is the same as its target type.

In the case of

HANDLE$free.invokeExact(address != null ? address : MemorySegment.NULL);

The target type for the conditional operator is Object, as invokeExact is declared to take Object....

invoke is more permissive than invokeExact, so using invoke works.

In all the other cases you showed, the type of the argument expression can be easily shown to be MemorySegment, so they work too.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật