0
我有函數指針的結構定義的字段:JNA函數指針作爲結構字段
https://github.com/slembcke/Chipmunk2D/blob/master/include/chipmunk/cpBody.h#L44
我限定JNA sructure這樣
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;
// ...
並且,ducumentation 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);
但是當我運行這段代碼,有一個exeption:
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
並永遠不會嘗試調用它們。
堆棧跟蹤:
: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
看起來你的類型沒有被識別爲'com.sun.jna.Callback'的實現。請包括你的完整堆棧跟蹤。 – technomage
完成,請參閱帖子下方的堆棧跟蹤。 – Utgarda
確保您導入了'com.sun.jna.Callback'而不是其他一些名爲'Callback'的類。 – technomage