struct*. When used as a field within another
Structure, it corresponds to struct. The
tagging interfaces ByReference and ByValue may be used
to alter the default behavior. Structures may have variable size, but only
by providing an array field (e.g. byte[]).
See the overview for supported type mappings for struct fields.
Structure alignment and type mappings are derived by default from the
enclosing interface definition (if any) by using
Native#getStructureAlignment and Native#getTypeMapper.
Alternatively you can explicitly provide alignment, field order, or type
mapping by calling the respective Structure functions in your subclass's
constructor.
Structure fields corresponding to native struct fields must be public. If your structure is to have no fields of its own, it must be declared abstract.
You must annotate the class with 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.
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).
Structure fields may additionally have the following modifiers:
volatile JNA will not write the field unless specifically
instructed to do so via writeField(String). This allows you to
prevent inadvertently overwriting memory that may be updated in real time
on another (possibly native) thread.
final JNA will overwrite the field via read(),
but otherwise the field is not modifiable from Java. Take care when using
this option, since the compiler will usually assume all accesses
to the field (for a given Structure instance) have the same value. This
modifier is invalid to use on J2ME.
Note
Strings are used to represent native C strings because usage of
char * is generally more common than wchar_t *.
You may provide a type mapper (example here) if you prefer to use String in place of WString if
your native code predominantly uses wchar_t *.
Note
In general, instances of this class are not synchronized.
| Modifier and Type | Class and Description |
|---|---|
| private static class | |
| public static interface | Structure.ByReference
Tagging interface to indicate the address of an instance of the
Structure type is to be used within a |
| public static interface | Structure.ByValue
Tagging interface to indicate the value of an instance of the
|
| pack-priv static class | Structure.FFIType
This class auto-generates an ffi_type structure appropriate for a given structure for use by libffi. |
| public static @interface | Structure.FieldOrder
Used to declare fields order as metadata instead of method. |
| private static class | Structure.LayoutInfo
Keep track of structure layout information. |
| private static class | Structure.NativeStringTracking
A class to keep NativeString instances alive and avoid writing the same value again and again |
| protected static class | |
| pack-priv static class | Structure.StructureSet
Avoid using a hash-based implementation since the hash code for a Structure is not immutable. |
| Modifier and Type | Field and Description |
|---|---|
| private int | |
| public static final int | ALIGN_DEFAULT
Use the platform default alignment. |
| public static final int | ALIGN_GNUC
validated for 32-bit x86 linux/gcc; align field size, max 4 bytes |
| public static final int | ALIGN_MSVC
validated for w32/msvc; align on field size |
| public static final int | ALIGN_NONE
No alignment, place all fields on nearest 1-byte boundary |
| private int | |
| private Structure[] | |
| private boolean | |
| private boolean | |
| private static final ThreadLocal | |
| protected static final int | CALCULATE_SIZE
Align to an 8-byte boundary. |
| private String | |
| pack-priv static final Map | |
| pack-priv static final Map | |
| private static final Logger | |
| private Pointer | |
| private final Map | |
| private static final Pointer | PLACEHOLDER_MEMORY
Placeholder pointer to help avoid auto-allocation of memory where a Structure needs a valid pointer but want to avoid actually reading from it. |
| private boolean | |
| private static final ThreadLocal | |
| private int | |
| private int | |
| private Map | |
| private long | |
| private TypeMapper |
| Access | Constructor and Description |
|---|---|
| protected | |
| protected | |
| protected | |
| protected | |
| protected | |
| protected | |
| protected |
| Modifier and Type | Method and Description |
|---|---|
| private int | |
| private int | |
| protected void | |
| private void | |
| protected void | allocateMemory(int
how much memory to allocate size)Provided for derived classes to indicate a different size than the default. |
| protected Memory | Returns: newly-allocated memorydesired size size)Obtain auto-allocated memory for use with struct represenations. |
| public static void | |
| public void | |
| public static void | |
| public void | |
| private Class | |
| pack-priv static Set | |
| protected void | cacheTypeInfo(Pointer
Native pointer to the type information p)Cache native type information for use in native code. |
| protected int | Returns: calculated size, orCALCULATE_SIZE if the size can not
yet be determined.whether to force size calculation force)Calculate the amount of native memory required for this structure. |
| pack-priv int | Returns: calculated size, orCALCULATE_SIZE if there is not yet
enough information to perform the size calculation.whether to force size calculation. force, boolean set false in certain situations to avoid recursive
type lookup. avoidFFIType)
|
| public void | |
| pack-priv void | |
| public static List | |
| public static List | |
| public static List | Returns: @return An un-modifiable list containing the field nameThe (single) field name field)
|
| public static List | Returns: An un-modifiable list of the fieldsThe structure field names in correct order fields)
|
| public boolean | Returns: equality resultStructure to compare s)Return whether the given Structure's native backing data is identical to this one. |
| public boolean | Returns: equality resultStructure to compare s, boolean whether to clear native memory clear)Return whether the given Structure's backing data is identical to this one, optionally clearing and re-writing native memory before checking. |
| private Structure. | deriveLayout(boolean force, boolean avoidFFIType)
Calculates the size, alignment, and field layout of this structure. |
| protected void | |
| private void | ensureAllocated(boolean
used when computing FFI type information
to avoid recursion avoidFFIType)Ensure this memory has its size and layout calculated and its memory allocated. |
| public boolean | Returns: whether the given structure's type and pointer match.the reference object with which to compare. o)Overrides java. |
| protected int | Returns: return offset of the given fieldfield to examine name)Returns the calculated offset of the given field. |
| private List | |
| pack-priv Map | |
| private String | |
| public boolean | Returns: whether automatic synch from native memory is enabled.Returns whether the structure is read from native memory after a native function call. |
| public boolean | Returns: whether automatic synch to native memory is enabled.Returns whether the structure is written to native memory prior to a native function call. |
| protected List | |
| protected List | Returns: ordered list of field namesReturns this Structure's field names in their proper order. |
| protected List | Returns: null if not yet able to provide fields, and force is false.set if results are required immediately force)Returns all field names (sorted) provided so far by
|
| pack-priv Structure. | Returns: Native pointer to the corresponding type informationinternal field representation fOverride to supply native type information for the given field. |
| pack-priv Object | Returns: current field value (Java-side only)field to look up field)Obtain the value currently in the Java field. |
| protected int | Returns: the native byte alignmentfield type type, Object field value, if available value, boolean is this field the first element in the struct? isFirstElement)Overridable in subclasses. |
| protected int | Returns: native size (in bytes) of the requested field typefield type to examine nativeType)Return the native size of the given Java type, from the perspective of this Structure. |
| protected int | Returns: native size (in bytes) of the requested field typefield type to examine nativeType, Object instance of the field type value)Return the native size of the given Java type, from the perspective of this Structure. |
| public Pointer | Returns: Native pointer representation of this structure.Return a |
| private static < the type T> Constructor | Returns: a constructor with a single Pointer argument, null if none is foundthe class type)Returns a constructor for the given type with a single Pointer argument, null if no such constructor is found. |
| protected String | Returns: Current encodingEncoding to use to convert |
| protected int | |
| pack-priv Pointer | |
| pack-priv static Structure. | Returns: native pointer to type informationobject to query obj)Exposed for testing purposes only. |
| pack-priv TypeMapper | |
| public int | Returns: hash code for this structure's pointer.Overrides java. |
| private Object | |
| private void | |
| private void | initializeTypeMapper(TypeMapper
Find the type mapper appropriate for this structure's
context if none was explicitly set. mapper)Initialize the type mapper for this structure. |
| private void | |
| private static <T extends Structure> T | newInstance(Class<T> type, long init)
Called from native code only; same as |
| public static <T extends Structure> T | Returns: the new instancedesired Structure type type, Pointer initial memory init)Create a new Structure instance of the given type, initialized with the given memory. |
| public static <T extends Structure> T | Returns: the new instancedesired Structure type type)Create a new Structure instance of the given type |
| public void | |
| public Object | |
| protected Object | Returns: value of the requested fieldfield to be read structFieldRead the given field and return its value. |
| pack-priv static Map | |
| protected void | |
| public void | setAutoRead(boolean
whether to automatically synch from native memory. auto)Set whether the structure is read from native memory after a native function call. |
| public void | setAutoSynch(boolean
whether to automatically synch with native memory. auto)Set whether the structure is automatically synchronized to native memory before and after a native function call. |
| public void | setAutoWrite(boolean
whether to automatically synch to native memory. auto)Set whether the structure is written to native memory prior to a native function call. |
| pack-priv void | |
| private void | |
| protected void | setStringEncoding(String
desired encoding encoding)Set the desired encoding to use when writing String fields to native memory. |
| public int | Returns: Native size of this structure, in bytes.Returns the size in memory occupied by this Structure. |
| pack-priv static int | |
| pack-priv static <T extends Structure> int | |
| private static <T extends Comparable | |
| protected void | sortFields(List<Field>
list of fields to be sorted fields, List<String> list of names representing the desired sort order names)Sort the structure fields according to the given array of names. |
| private static void | |
| public Structure[] | |
| public Structure[] | Returns: array of Structure (individual elements will be of the appropriate type, as will the Structure[]).desired number of elements size)Returns a view of this structure's memory as an array of structures. |
| public String | Returns: String representation of this object.Overrides java. jna.dump_memory is true, will include a native memory dump
of the Structure's backing memory.
|
| public String | Returns: String representation of this object.If true, will include a native memory dump of the
Structure's backing memory. debug)
|
| private String | |
| pack-priv static <T extends Structure> T | Returns: UpdatedStructure.ByReference objectStructure subclass type, T Original Structure object s, Pointer the native address)struct *Only keep the original structure if its native address is unchanged. |
| protected void | |
| protected void | |
| pack-priv void | |
| pack-priv static void | |
| private void | |
| private void | |
| public void | |
| public void | |
| public void | writeField(String
field to write name, Object value to write value)Write the given field value to the field and native memory. |
| protected void | writeField(Structure.
internal field representation to synch to native memory structField
|
| private void | writeField(Structure.
internal field representation to synch to native memory structField,value to write value)
|