JNA函数指针作为结构字段

时间:2015-11-09 02:19:37

标签: java jna

我有一个结构定义,函数指针为字段:

https://github.com/slembcke/Chipmunk2D/blob/master/include/chipmunk/cpBody.h#L44

我定义了像这样的JNA结构

package com.dewdropgames.chipmunk2djna.structs;

import com.dewdropgames.chipmunk2djna.CpVect;
import com.dewdropgames.chipmunk2djna.callbacks.CpBodyPositionFunc;
import com.sun.jna.Structure;

import java.util.Arrays;
import java.util.List;

public class CpBody extends Structure {
    private static final List<String> FIELD_ORDER = Arrays.asList(
/// ...
            "position_func",
/// ...
    );

    @Override
    protected List getFieldOrder() {
        return FIELD_ORDER;
    }

    public static class ByReference 
        extends CpBody 
        implements Structure.ByReference{};

// ...
    public CpBodyPositionFunc position_func;
// ...

并且,在实施之后 https://jna.java.net/nonav/javadoc/overview-summary.html#callbacks  我定义了一个像这样的回调接口:

package com.dewdropgames.chipmunk2djna.callbacks;

import com.dewdropgames.chipmunk2djna.structs.CpBody;

public interface CpBodyPositionFunc extends Callback {
    void invoke(CpBody body, double dt);
}
// typedef void (*cpBodyPositionFunc)(cpBody *body, cpFloat dt);

但是当我运行这段代码时,有一个例外:

Caused by: java.lang.IllegalArgumentException: 
The type "com.dewdropgames.chipmunk2djna.callbacks.CpBodyPositionFunc"
is not supported: 
Native size for type
"com.dewdropgames.chipmunk2djna.callbacks.CpBodyPositionFunc" 
is unknown

我在这里做错了什么?顺便说一句,如果我只是将这些字段定义为Pointer并且从不尝试调用它们,则表现良好。

Stacktrace:

:runAppException in thread "main" java.lang.IllegalArgumentException: Invalid Structure field in class com.dewdropgames.chipmunk2djna.structs.CpSpace$ByReference, field name '_staticBody' (class com.dewdropgames.chipmunk2djna.structs.CpBody): Invalid Structure field in class com.dewdropgames.chipmunk2djna.structs.CpBody, field name 'position_func' (interface com.dewdropgames.chipmunk2djna.callbacks.CpBodyPositionFunc): The type "com.dewdropgames.chipmunk2djna.callbacks.CpBodyPositionFunc" is not supported: Native size for type "com.dewdropgames.chipmunk2djna.callbacks.CpBodyPositionFunc" is unknown
        at com.sun.jna.Structure.validateField(Structure.java:1109)
        at com.sun.jna.Structure.validateFields(Structure.java:1119)
        at com.sun.jna.Structure.<init>(Structure.java:179)
        at com.sun.jna.Structure.<init>(Structure.java:172)
        at com.sun.jna.Structure.<init>(Structure.java:159)
        at com.sun.jna.Structure.<init>(Structure.java:151)
        at com.dewdropgames.chipmunk2djna.structs.CpSpace.<init>(CpSpace.java:13)
        at com.dewdropgames.chipmunk2djna.structs.CpSpace$ByReference.<init>(CpSpace.java:54)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
        at java.lang.Class.newInstance(Class.java:442)
        at com.sun.jna.Structure.newInstance(Structure.java:1773)
        at com.sun.jna.Structure.newInstance(Structure.java:1759)
        at com.sun.jna.Function.invoke(Function.java:424)
        at com.sun.jna.Function.invoke(Function.java:323)
        at com.sun.jna.Library$Handler.invoke(Library.java:236)
        at com.sun.proxy.$Proxy0.cpSpaceNew(Unknown Source)
        at com.dewdropgames.chipmunk2djna.FirstTry.main(FirstTry.java:31)
Caused by: java.lang.IllegalArgumentException: Invalid Structure field in class com.dewdropgames.chipmunk2djna.structs.CpBody, field name 'position_func' (interface com.dewdropgames.chipmunk2djna.callbacks.CpBodyPositionFunc): The type "com.dewdropgames.chipmunk2djna.callbacks.CpBodyPositionFunc" is not supported: Native size for type "com.dewdropgames.chipmunk2djna.callbacks.CpBodyPositionFunc" is unknown

Initializing cpSpace - Chipmunk v7.0.1 (Debug Enabled)
Compile with -DNDEBUG defined to disable debug mode and runtime assertion checks
        at com.sun.jna.Structure.validateField(Structure.java:1109)
        at com.sun.jna.Structure.validateFields(Structure.java:1119)
        at com.sun.jna.Structure.<init>(Structure.java:179)
        at com.sun.jna.Structure.<init>(Structure.java:172)
        at com.sun.jna.Structure.<init>(Structure.java:159)
        at com.sun.jna.Structure.<init>(Structure.java:151)
        at com.dewdropgames.chipmunk2djna.structs.CpBody.<init>(CpBody.java:15)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
        at java.lang.Class.newInstance(Class.java:442)
        at com.sun.jna.Structure.newInstance(Structure.java:1773)
        at com.sun.jna.Structure.newInstance(Structure.java:1759)
        at com.sun.jna.Structure.size(Structure.java:1030)
        at com.sun.jna.Native.getNativeSize(Native.java:1172)
        at com.sun.jna.Structure.getNativeSize(Structure.java:2072)
        at com.sun.jna.Structure.getNativeSize(Structure.java:2062)
        at com.sun.jna.Structure.validateField(Structure.java:1105)
        ... 19 more
Caused by: java.lang.IllegalArgumentException: The type "com.dewdropgames.chipmunk2djna.callbacks.CpBodyPositionFunc" is not supported: Native size for type "com.dewdropgames.chipmunk2djna.callbacks.CpBodyPositionFunc" is unknown
        at com.sun.jna.Native.getNativeSize(Native.java:1180)
        at com.sun.jna.Structure.getNativeSize(Structure.java:2072)
        at com.sun.jna.Structure.getNativeSize(Structure.java:2062)
        at com.sun.jna.Structure.validateField(Structure.java:1105)
        ... 37 more

1 个答案:

答案 0 :(得分:2)

确实,这次半自动导入失败了,导入了另一个界面,发现这一行:import javax.security.auth.callback.Callback;

本机函数的JNA接口应实现com.sun.jna.Callback