Provides generation of invocation plumbing for a defined native library interface. Also provides various utilities for native operations. #getTypeMapper and #getStructureAlignment are provided to avoid having to explicitly pass these parameters to Structures, which would
| 110 | * @author twall@users.sf.net |
| 111 | */ |
| 112 | public final class Native implements Version { |
| 113 | |
| 114 | private static final Logger LOG = Logger.getLogger(Native.class.getName()); |
| 115 | |
| 116 | public static final Charset DEFAULT_CHARSET; |
| 117 | public static final String DEFAULT_ENCODING; |
| 118 | static { |
| 119 | // JNA used the defaultCharset to determine which encoding to use when |
| 120 | // converting strings to native char*. The defaultCharset is set from |
| 121 | // the system property file.encoding. Up to JDK 17 its value defaulted |
| 122 | // to the system default encoding. From JDK 18 onwards its default value |
| 123 | // changed to UTF-8. |
| 124 | // JDK 18+ exposes the native encoding as the new system property |
| 125 | // native.encoding, prior versions don't have that property and will |
| 126 | // report NULL for it. |
| 127 | // The algorithm is simple: If native.encoding is set, it will be used |
| 128 | // else the original implementation of Charset#defaultCharset is used |
| 129 | String nativeEncoding = System.getProperty("native.encoding"); |
| 130 | Charset nativeCharset = null; |
| 131 | if (nativeEncoding != null) { |
| 132 | try { |
| 133 | nativeCharset = Charset.forName(nativeEncoding); |
| 134 | } catch (Exception ex) { |
| 135 | LOG.log(Level.WARNING, "Failed to get charset for native.encoding value : '" + nativeEncoding + "'", ex); |
| 136 | } |
| 137 | } |
| 138 | if (nativeCharset == null) { |
| 139 | nativeCharset = Charset.defaultCharset(); |
| 140 | } |
| 141 | DEFAULT_CHARSET = nativeCharset; |
| 142 | DEFAULT_ENCODING = nativeCharset.name(); |
| 143 | } |
| 144 | public static final boolean DEBUG_LOAD = Boolean.getBoolean("jna.debug_load"); |
| 145 | public static final boolean DEBUG_JNA_LOAD = Boolean.getBoolean("jna.debug_load.jna"); |
| 146 | private final static Level DEBUG_JNA_LOAD_LEVEL = DEBUG_JNA_LOAD ? Level.INFO : Level.FINE; |
| 147 | |
| 148 | // Used by tests, do not remove |
| 149 | static String jnidispatchPath = null; |
| 150 | private static final Map<Class<?>, Map<String, Object>> typeOptions = Collections.synchronizedMap(new WeakHashMap<Class<?>, Map<String, Object>>()); |
| 151 | private static final Map<Class<?>, Reference<?>> libraries = Collections.synchronizedMap(new WeakHashMap<Class<?>, Reference<?>>()); |
| 152 | private static final String _OPTION_ENCLOSING_LIBRARY = "enclosing-library"; |
| 153 | private static final UncaughtExceptionHandler DEFAULT_HANDLER = |
| 154 | new UncaughtExceptionHandler() { |
| 155 | @Override |
| 156 | public void uncaughtException(Callback c, Throwable e) { |
| 157 | LOG.log(Level.WARNING, "JNA: Callback " + c + " threw the following exception", e); |
| 158 | } |
| 159 | }; |
| 160 | private static UncaughtExceptionHandler callbackExceptionHandler = DEFAULT_HANDLER; |
| 161 | |
| 162 | /** The size of a native pointer (<code>void*</code>) on the current |
| 163 | * platform, in bytes. |
| 164 | */ |
| 165 | public static final int POINTER_SIZE; |
| 166 | /** Size of a native <code>long</code> type, in bytes. */ |
| 167 | public static final int LONG_SIZE; |
| 168 | /** Size of a native <code>wchar_t</code> type, in bytes. */ |
| 169 | public static final int WCHAR_SIZE; |
nothing calls this directly
no test coverage detected
searching dependent graphs…