lundi 15 février 2021

Instantiating extended JNA Structure throws IllegalAccessException

I was having trouble getting com.sun.jna.Structure to work, so I tried copying the first JNA test I found and I can't even get that to work.

For convenience, try it online.


import com.sun.jna.Structure;
import java.util.Arrays;
import java.util.List;

public class MyClass {
    public void testSimpleSize() throws Exception {
        class TestStructure extends Structure {
            public int field;
            @Override
            protected List<String> getFieldOrder() {
                return Arrays.asList("field");
            }
        }
        Structure s = new TestStructure();
        //assertEquals("Wrong size", 4, s.size());
    }

    public static void main(String[] args)
    {
        try
        {
            new MyClass().testSimpleSize();
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }
}

The code compiles ok, but when running I get

Exception in thread "main" java.lang.Error: Exception reading field 'field' in class MyClass$1TestStructure
    at com.sun.jna.Structure.getFieldValue(Structure.java:639)
    at com.sun.jna.Structure.deriveLayout(Structure.java:1285)
    at com.sun.jna.Structure.calculateSize(Structure.java:1159)
    at com.sun.jna.Structure.calculateSize(Structure.java:1111)
    at com.sun.jna.Structure.allocateMemory(Structure.java:414)
    at com.sun.jna.Structure.<init>(Structure.java:205)
    at com.sun.jna.Structure.<init>(Structure.java:193)
    at com.sun.jna.Structure.<init>(Structure.java:180)
    at com.sun.jna.Structure.<init>(Structure.java:172)
    at MyClass$1TestStructure.<init>(MyClass.java:8)
    at MyClass.testSimpleSize(MyClass.java:15)
    at MyClass.main(MyClass.java:23)
Caused by: java.lang.IllegalAccessException: class com.sun.jna.Structure cannot access a member of class MyClass$1TestStructure with modifiers "public"
    at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:361)
    at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:591)
    at java.base/java.lang.reflect.Field.checkAccess(Field.java:1075)
    at java.base/java.lang.reflect.Field.get(Field.java:416)
    at com.sun.jna.Structure.getFieldValue(Structure.java:636)
    ... 11 more

Am I missing something?





Aucun commentaire:

Enregistrer un commentaire