{"record":{"id":"dbc2dfac7f0d9f15","repo":"java-native-access/jna","slug":"instantiation-of-type-pointer-not-allowed-is-it-public","errorCode":null,"errorMessage":"Instantiation of \" + type + \" (Pointer) not allowed, is it public?\"","messagePattern":"Instantiation of \" \\+ type \\+ \" \\(Pointer\\) not allowed, is it public\\?\"","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/com/sun/jna/Structure.java","lineNumber":1956,"sourceCode":"     */\n    public static <T extends Structure> T newInstance(Class<T> type, Pointer init) throws IllegalArgumentException {\n        try {\n            Constructor<T> ctor = getPointerConstructor(type);\n            if (ctor != null) {\n                return ctor.newInstance(init);\n            }\n            // Not defined, fall back to the default\n        }\n        catch(SecurityException e) {\n            // Might as well try the fallback\n        }\n        catch(InstantiationException e) {\n            String msg = \"Can't instantiate \" + type;\n            throw new IllegalArgumentException(msg, e);\n        }\n        catch(IllegalAccessException e) {\n            String msg = \"Instantiation of \" + type + \" (Pointer) not allowed, is it public?\";\n            throw new IllegalArgumentException(msg, e);\n        }\n        catch(InvocationTargetException e) {\n            String msg = \"Exception thrown while instantiating an instance of \" + type;\n            throw new IllegalArgumentException(msg, e);\n        }\n        T s = newInstance(type);\n        if (init != PLACEHOLDER_MEMORY) {\n            s.useMemory(init);\n        }\n        return s;\n    }\n\n    /**\n     * Create a new Structure instance of the given type\n     * @param type desired Structure type\n     * @return the new instance\n     * @throws IllegalArgumentException if the instantiation fails\n     */","sourceCodeStart":1938,"sourceCodeEnd":1974,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/Structure.java#L1938-L1974","documentation":"JNA tries reflectively to construct the Structure via its (Pointer) constructor, which must be public. If it exists but is inaccessible (private, protected, package-private, or defined in a non-public class), Constructor.newInstance throws IllegalAccessException and JNA rethrows it as IllegalArgumentException asking \"is it public?\". This is a visibility problem in the user's Structure class definition.","triggerScenarios":"Declaring a Structure with a (Pointer) constructor marked non-public (or the class itself non-public, e.g. a package-private or inner class), then instantiating it via Structure.newInstance(type, pointer) or having JNA create it for nested/by-value layout or a native return value.","commonSituations":"Making the Pointer constructor package-private to 'hide' it from users; declaring the Structure as a non-static private inner class in a test; Kotlin/data-class conversions making constructors non-public; moving a Structure class out of a public file without a public modifier.","solutions":["Make the (Pointer) constructor public: `public MyStruct(Pointer p) { super(p); read(); }`.","Declare the Structure class itself public (and static if nested), since a non-public class makes even public constructors inaccessible.","If the constructor was added for JNA, keep it public but document it; alternatively remove it so JNA falls back to the public no-arg constructor.","Provide both a public no-arg constructor and a public (Pointer) constructor to cover all instantiation paths."],"exampleFix":"// before\nMyStruct(Pointer p) { super(p); read(); }  // package-private\n// after\npublic MyStruct(Pointer p) { super(p); read(); }","handlingStrategy":"validation","validationCode":"static void requirePublicPointerCtor(Class<? extends Structure> type) {\n    try {\n        java.lang.reflect.Constructor<? extends Structure> c = type.getConstructor(Pointer.class);\n        if (!java.lang.reflect.Modifier.isPublic(type.getModifiers()))\n            throw new IllegalStateException(type + \" class must be public\");\n    } catch (NoSuchMethodException e) {\n        try { type.getConstructor(); }\n        catch (NoSuchMethodException e2) { throw new IllegalStateException(type + \" needs public no-arg or (Pointer) constructor\"); }\n    }\n}","typeGuard":"static boolean isPubliclyConstructible(Class<?> c) {\n    if (!java.lang.reflect.Modifier.isPublic(c.getModifiers())) return false;\n    for (java.lang.reflect.Constructor<?> k : c.getConstructors()) {\n        Class<?>[] p = k.getParameterTypes();\n        if (p.length == 0 || (p.length == 1 && p[0] == Pointer.class)) return true;\n    }\n    return false;\n}","tryCatchPattern":"try {\n    MyStruct s = Structure.newInstance(MyStruct.class, ptr);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"not allowed, is it public?\")) {\n        throw new IllegalStateException(\"Make \" + MyStruct.class + \" and its (Pointer) constructor public\", e);\n    }\n    throw e;\n}","preventionTips":["Always declare the (Pointer) constructor public when you provide one for JNA","Declare Structure classes public and static when nested - a non-public class makes public constructors inaccessible via reflection","Prefer relying on the default public no-arg constructor unless you need to wrap an existing Pointer","Add a reflection-based test that constructs every Structure class the way JNA will"],"tags":["jna","structure","reflection","access-modifier","constructor"],"backgroundTag":"permission-denied","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}