映射自定义类型 Fortran-to-Java(使用 JNA)

Mapping custom types Fortran-to-Java (with JNA)

我必须编写要从 Java 调用的 Fortran 子例程的接口。 Fortran 子例程中的一些参数是派生类型(自定义类型/结构)。有可能用 JNA 映射那些吗?到目前为止,我还不知道这是怎么回事。 JNI 呢?

例如像这样的子程序:

subroutine mysub(arg)
implicit none
type mytype
   integer:: i
   real*8 :: a(3)
end type mytype

type(mytype) arg

! do stuff...

end subroutine mysub

是的,JNA 通过引用和值支持聚合类型(struct 在 C 中)。参数的默认约定是按值,例如

public interface MyLibrary extends Library {
    MyLibrary INSTANCE = (MyLibrary)Native.loadLibrary("mylib", MyLibrary.class);

    class MyStruct extends Structure {
        public static class ByValue extends MyStruct implements Structure.ByValue {}
        public int i;
        public double a[3];
        protected List getFieldOrder() {
            return Arrays.asList("i", "a");
        }
    }

    void mysub(MyStruct.ByValue arg);
}