Internal Struct.fieldOrder always empty if nested class inside interface?

14 views
Skip to first unread message

Osiris Team

unread,
Aug 24, 2024, 5:19:53 PM8/24/24
to Java Native Access
I am on Java 21, and JNA 5.14.0.

Code looks something like this:
```java
public interface CustomLib{
  public class SomeStruct extends Struct{
      public int someField;
  }
}
```

and it seems like fieldOrder always is empty.
The error message I get looks something like this:

Caused by: java.lang.Error: Structure.getFieldOrder() on class com.path.to. CustomLib$SomeStruct  does not provide enough names [0] ([]) to match declared fields [7] ([x, x, x, x, x, x, x])

I need to redact the info since its a proprietary lib, so x are the actual field names.

Osiris Team

unread,
Aug 24, 2024, 5:22:29 PM8/24/24
to Java Native Access
The order of the fields in the error message seems to be correct, so optimally can I somehow configure it to use that list instead?

Daniel B. Widdis

unread,
Aug 24, 2024, 5:39:29 PM8/24/24
to jna-...@googlegroups.com
You need to either use the @FieldOrder annotation or override getFieldOrder().

You must annotate the class with Structure.FieldOrder or implement getFieldOrder(), whichever you choose it must contain the field names (Strings) indicating the proper order of the fields. If you chose to implement getFieldOrder() notice that when dealing with multiple levels of subclasses of Structure, you must add to the list provided by the superclass getFieldOrder() the fields defined in the current class.

Note the order seems to be predictable but that isn't guaranteed, so you can't just use that order.

In the past, most VMs would return them in a predictable order, but the JVM spec does not require it, so getFieldOrder() is now required to ensure JNA knows the proper order).






--
You received this message because you are subscribed to the Google Groups "Java Native Access" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jna-users/2b4cb114-ca7b-4b5f-8afc-3e22c833e976n%40googlegroups.com.


--
Dan Widdis

Osiris Team

unread,
Aug 24, 2024, 5:50:37 PM8/24/24
to Java Native Access
Great, thanks did the override variant!
Reply all
Reply to author
Forward
0 new messages