Top Description Inners Fields Constructors Methods
com.sun.jna

public abstract Class Structure

extends Object
Class Inheritance
Known Direct Subclasses
com.sun.jna.Structure.FFIType, com.sun.jna.Union, com.sun.jna.examples.unix.X11.Xrender.XRenderDirectFormat, com.sun.jna.examples.unix.X11.Xrender.XRenderPictFormat, com.sun.jna.examples.unix.X11.XWMHints, com.sun.jna.examples.unix.X11.XTextProperty, com.sun.jna.examples.unix.X11.XSizeHints, com.sun.jna.examples.unix.X11.XWindowAttributes, com.sun.jna.examples.unix.X11.XSetWindowAttributes, com.sun.jna.examples.unix.X11.XVisualInfo, com.sun.jna.examples.win32.GDI32.RECT, com.sun.jna.examples.win32.GDI32.RGNDATAHEADER, com.sun.jna.examples.win32.GDI32.RGNDATA, com.sun.jna.examples.win32.GDI32.BITMAPINFOHEADER, com.sun.jna.examples.win32.GDI32.RGBQUAD, com.sun.jna.examples.win32.GDI32.BITMAPINFO, com.sun.jna.examples.win32.Kernel32.SYSTEMTIME, com.sun.jna.examples.win32.Kernel32.SECURITY_ATTRIBUTES, com.sun.jna.examples.win32.Kernel32.FILE_NOTIFY_INFORMATION, com.sun.jna.examples.win32.Kernel32.OVERLAPPED, com.sun.jna.examples.win32.Shell32.SHFILEOPSTRUCT, com.sun.jna.examples.win32.User32.FLASHWINFO, com.sun.jna.examples.win32.User32.GUITHREADINFO, com.sun.jna.examples.win32.User32.WINDOWINFO, com.sun.jna.examples.win32.User32.POINT, com.sun.jna.examples.win32.User32.SIZE, com.sun.jna.examples.win32.User32.BLENDFUNCTION, com.sun.jna.examples.win32.User32.KBDLLHOOKSTRUCT, com.sun.jna.examples.win32.User32.MSG
Imports
java.lang.reflect.Array, .Field, .Modifier, java.nio.Buffer, java.util.ArrayList, .Collections, .HashMap, .HashSet, .Iterator, .LinkedHashMap, .List, .Map, .Set, .WeakHashMap

Represents a native structure with a Java peer class. When used as a function parameter or return value, this class corresponds to 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.

See the overview for supported type mappings.

Structure alignment and type mappings are derived by default from the enclosing interface definition (if any) by using Native#getStructureAlignment and Native#getTypeMapper.

Note

Strings are used to represent native C strings because usage of char * is generally more common than wchar_t *.

Note

This class assumes that fields are returned in Class#getFields in the same or reverse order as declared. If your VM returns them in no particular order, you're out of luck.

Authors
Todd Fast, [email protected], [email protected]

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
Structure.ByReference

Tagging interface to indicate the address of an instance of the Structure type is to be used within a Structure definition rather than nesting the full Structure contents.

public static interface
Structure.ByValue

Tagging interface to indicate the value of an instance of the Structure type is to be used in function invocations rather than its address.

private static class
Structure.FFIType

This class auto-generates an ffi_type structure appropriate for a given structure for use by libffi.

private static class
pack-priv class

Field Summary

Modifier and TypeField and Description
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
protected static final int
pack-priv static final boolean
pack-priv static final boolean
private static final int
private Pointer
private Map<K, V>
private static Set<E>
private static final boolean
private int
private int
private Map<K, V>
private long
private TypeMapper

Constructor Summary

AccessConstructor and Description
protected
protected
Structure(int size)

protected
Structure(int size, int alignment)

Method Summary

Modifier and TypeMethod and Description
protected void
allocateMemory()

Attempt to allocate memory if sufficient information is available.

protected void
allocateMemory(int size)

Provided for derived classes to indicate a different size than the default.

pack-priv int
calculateAlignedSize(int calculatedSize)

pack-priv int
calculateSize(boolean force)

Calculate the amount of native memory required for this structure.

public void
public boolean
equals(Object
the reference object with which to compare.
o
)

Overrides java.lang.Object.equals.

This structure is only equal to another based on the same native memory address and data type.
pack-priv Map<K, V>
fields()

Return all fields in this structure (ordered).

private String
format(Class<T> type)

pack-priv Object
getField(Structure.StructField structField)

Obtain the value currently in the Java field.

protected int
getNativeAlignment(Class<T> type, Object value, boolean isFirstElement)

Overridable in subclasses.

private static int
getNativeSize(Class<T> type, Object value)

Returns the native size of the given class, in bytes.

public Pointer
getPointer()

Return a Pointer object to this structure.

protected int
pack-priv Pointer
getTypeInfo()

Obtain native type information for this structure.

pack-priv static Pointer
getTypeInfo(Object obj)

Exposed for testing purposes only.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Since equals depends on the native address, use that as the hash code.
public static Structure

Returns:

the new instance
newInstance
(Class<T> type)

Create a new Structure instance of the given type

public void
read()

Reads the fields of the struct from native memory

public Object

Returns:

the new field value, after updating
readField
(String name)

Force a read of the given field from native memory.

pack-priv Object
readField(Structure.StructField structField)

Read the given field and return its value.

protected void
setAlignType(int alignType)

Change the alignment of this structure.

private void
setField(Structure.StructField structField, Object value)

protected void
setTypeMapper(TypeMapper mapper)

Change the type mapping for this structure.

public int
size()

public Structure[]
toArray(Structure[] array)

Returns a view of this structure's memory as an array of structures.

public Structure[]
toArray(int size)

Returns a view of this structure's memory as an array of structures.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
private String
toString(int indent)

pack-priv static Structure

Returns:

Updated Structure.ByReference object
updateStructureByReference
(Class<T>
Structure subclass
type
,
Structure
Original Structure object
s
,
Pointer
the native struct *
address
)

Only keep the original structure if its address is unchanged.

protected void
useMemory(Pointer m)

Set the memory used by this structure.

protected void
useMemory(Pointer m, int offset)

Set the memory used by this structure.

public void
write()

Writes the fields of the struct to native memory

public void
writeField(String name)

Write the given field to native memory.

pack-priv void
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

ALIGN_DEFAULTback to summary
public static final int ALIGN_DEFAULT

Use the platform default alignment.

ALIGN_GNUCback to summary
public static final int ALIGN_GNUC

validated for 32-bit x86 linux/gcc; align field size, max 4 bytes

ALIGN_MSVCback to summary
public static final int ALIGN_MSVC

validated for w32/msvc; align on field size

ALIGN_NONEback to summary
public static final int ALIGN_NONE

No alignment, place all fields on nearest 1-byte boundary

alignTypeback to summary
private int alignType
CALCULATE_SIZEback to summary
protected static final int CALCULATE_SIZE
isPPCback to summary
pack-priv static final boolean isPPC
isSPARCback to summary
pack-priv static final boolean isSPARC
MAX_GNUC_ALIGNMENTback to summary
private static final int MAX_GNUC_ALIGNMENT
memoryback to summary
private Pointer memory
nativeStringsback to summary
private Map<K, V> nativeStrings
readingback to summary
private static Set<E> reading
REVERSE_FIELDSback to summary
private static final boolean REVERSE_FIELDS
sizeback to summary
private int size
structAlignmentback to summary
private int structAlignment
structFieldsback to summary
private Map<K, V> structFields
typeInfoback to summary
private long typeInfo
typeMapperback to summary
private TypeMapper typeMapper

Constructor Detail

Structureback to summary
protected Structure()
Structureback to summary
protected Structure(int size)
Structureback to summary
protected Structure(int size, int alignment)

Method Detail

allocateMemoryback to summary
protected void allocateMemory()

Attempt to allocate memory if sufficient information is available. Returns whether the operation was successful.

allocateMemoryback to summary
protected void allocateMemory(int size)

Provided for derived classes to indicate a different size than the default. Returns whether the operation was successful.

calculateAlignedSizeback to summary
pack-priv int calculateAlignedSize(int calculatedSize)
calculateSizeback to summary
pack-priv int calculateSize(boolean force)

Calculate the amount of native memory required for this structure. May return CALCULATE_SIZE if the size can not yet be determined (usually due to fields in the derived class not yet being initialized).

If the force parameter is true will throw an IllegalStateException if the size can not be determined.

Exceptions
IllegalStateException:
an array field is not initialized
IllegalArgumentException:
when an unsupported field type is encountered
clearback to summary
public void clear()
equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

This structure is only equal to another based on the same native memory address and data type.

Parameters
o:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

fieldsback to summary
pack-priv Map<K, V> fields()

Return all fields in this structure (ordered).

formatback to summary
private String format(Class<T> type)
getFieldback to summary
pack-priv Object getField(Structure.StructField structField)

Obtain the value currently in the Java field. Does not read from memory.

getNativeAlignmentback to summary
protected int getNativeAlignment(Class<T> type, Object value, boolean isFirstElement)

Overridable in subclasses.

getNativeSizeback to summary
private static int getNativeSize(Class<T> type, Object value)

Returns the native size of the given class, in bytes.

getPointerback to summary
public Pointer getPointer()

Return a Pointer object to this structure. Note that if you use the structure's pointer as a function argument, you are responsible for calling write() prior to the call and read() after the call. These calls are normally handled automatically by the Function object when it encounters a Structure argument or return value.

getStructAlignmentback to summary
protected int getStructAlignment()
getTypeInfoback to summary
pack-priv Pointer getTypeInfo()

Obtain native type information for this structure.

getTypeInfoback to summary
pack-priv static Pointer getTypeInfo(Object obj)

Exposed for testing purposes only.

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Since equals depends on the native address, use that as the hash code.

Returns:int

Doc from java.lang.Object.hashCode.

a hash code value for this object.

newInstanceback to summary
public static Structure newInstance(Class<T> type) throws IllegalArgumentException

Create a new Structure instance of the given type

Returns:Structure

the new instance

Exceptions
IllegalArgumentException:
if the instantiation fails
readback to summary
public void read()

Reads the fields of the struct from native memory

readFieldback to summary
public Object readField(String name)

Force a read of the given field from native memory.

Returns:Object

the new field value, after updating

Exceptions
IllegalArgumentException:
if no field exists with the given name
readFieldback to summary
pack-priv Object readField(Structure.StructField structField)

Read the given field and return its value.

setAlignTypeback to summary
protected void setAlignType(int alignType)

Change the alignment of this structure. Re-allocates memory if necessary. If alignment is ALIGN_DEFAULT, the default alignment for the defining class will be used.

setFieldback to summary
private void setField(Structure.StructField structField, Object value)
setTypeMapperback to summary
protected void setTypeMapper(TypeMapper mapper)

Change the type mapping for this structure. May cause the structure to be resized and any existing memory to be reallocated. If null, the default mapper for the defining class will be used.

sizeback to summary
public int size()
toArrayback to summary
public Structure[] toArray(Structure[] array)

Returns a view of this structure's memory as an array of structures. Note that this Structure must have a public, no-arg constructor. If the structure is currently using a Memory backing, the memory will be resized to fit the entire array.

toArrayback to summary
public Structure[] toArray(int size)

Returns a view of this structure's memory as an array of structures. Note that this Structure must have a public, no-arg constructor. If the structure is currently using a Memory backing, the memory will be resized to fit the entire array.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

toStringback to summary
private String toString(int indent)
updateStructureByReferenceback to summary
pack-priv static Structure updateStructureByReference(Class<T> type, Structure s, Pointer address)

Only keep the original structure if its address is unchanged. Otherwise replace it with a new object.

Parameters
type:Class<T>

Structure subclass

s:Structure

Original Structure object

address:Pointer

the native struct *

Returns:Structure

Updated Structure.ByReference object

useMemoryback to summary
protected void useMemory(Pointer m)

Set the memory used by this structure. This method is used to indicate the given structure is nested within another or otherwise overlaid on some other memory block and thus does not own its own memory.

useMemoryback to summary
protected void useMemory(Pointer m, int offset)

Set the memory used by this structure. This method is used to indicate the given structure is nested within another or otherwise overlaid on some other memory block and thus does not own its own memory.

writeback to summary
public void write()

Writes the fields of the struct to native memory

writeFieldback to summary
public void writeField(String name)

Write the given field to native memory.

writeFieldback to summary
pack-priv void writeField(Structure.StructField structField)
com.sun.jna back to summary

public Interface Structure.ByReference


Tagging interface to indicate the address of an instance of the Structure type is to be used within a Structure definition rather than nesting the full Structure contents. The default behavior is to inline Structure fields.
com.sun.jna back to summary

public Interface Structure.ByValue


Tagging interface to indicate the value of an instance of the Structure type is to be used in function invocations rather than its address. The default behavior is to treat Structure function parameters and return values as by reference, meaning the address of the structure is used.
com.sun.jna back to summary

private Class Structure.FFIType

extends Structure
Class Inheritance

This class auto-generates an ffi_type structure appropriate for a given structure for use by libffi. The lifecycle of this structure is easier to manage on the Java side than in native code.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
public static class

Field Summary

Modifier and TypeField and Description
public short
public Pointer
private static final int
public Structure.FFIType.size_t
public short
private static Map<K, V>
Inherited from com.sun.jna.Structure:
ALIGN_DEFAULTALIGN_GNUCALIGN_MSVCALIGN_NONECALCULATE_SIZEisPPCisSPARC

Constructor Summary

AccessConstructor and Description
private
private
FFIType(Object array, Class<T> type)

Method Summary

Modifier and TypeMethod and Description
pack-priv static Pointer
get(Object obj)

private static Pointer
get(Object obj, Class<T> cls)

private void
init(Pointer[] els)

Inherited from com.sun.jna.Structure:
allocateMemoryallocateMemorycalculateAlignedSizecalculateSizeclearequalsfieldsgetFieldgetNativeAlignmentgetPointergetStructAlignmentgetTypeInfogetTypeInfohashCodenewInstancereadreadFieldreadFieldsetAlignTypesetTypeMappersizetoArraytoArraytoStringupdateStructureByReferenceuseMemoryuseMemorywritewriteFieldwriteField

Field Detail

alignmentback to summary
public short alignment
elementsback to summary
public Pointer elements
FFI_TYPE_STRUCTback to summary
private static final int FFI_TYPE_STRUCT
sizeback to summary
public Structure.FFIType.size_t size

Hides com.sun.jna.Structure.size.

typeback to summary
public short type
typeInfoMapback to summary
private static Map<K, V> typeInfoMap

Constructor Detail

FFITypeback to summary
private FFIType(Structure ref)
FFITypeback to summary
private FFIType(Object array, Class<T> type)

Method Detail

getback to summary
pack-priv static Pointer get(Object obj)
getback to summary
private static Pointer get(Object obj, Class<T> cls)
initback to summary
private void init(Pointer[] els)
com.sun.jna back to summary

private Class Structure.FFIType.FFITypes

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private static Pointer
private static Pointer
private static Pointer
private static Pointer
private static Pointer
private static Pointer
private static Pointer
private static Pointer
private static Pointer
private static Pointer
private static Pointer
private static Pointer
private static Pointer

Constructor Summary

AccessConstructor and Description
private

Field Detail

ffi_type_doubleback to summary
private static Pointer ffi_type_double
ffi_type_floatback to summary
private static Pointer ffi_type_float
ffi_type_longdoubleback to summary
private static Pointer ffi_type_longdouble
ffi_type_pointerback to summary
private static Pointer ffi_type_pointer
ffi_type_sint16back to summary
private static Pointer ffi_type_sint16
ffi_type_sint32back to summary
private static Pointer ffi_type_sint32
ffi_type_sint64back to summary
private static Pointer ffi_type_sint64
ffi_type_sint8back to summary
private static Pointer ffi_type_sint8
ffi_type_uint16back to summary
private static Pointer ffi_type_uint16
ffi_type_uint32back to summary
private static Pointer ffi_type_uint32
ffi_type_uint64back to summary
private static Pointer ffi_type_uint64
ffi_type_uint8back to summary
private static Pointer ffi_type_uint8
ffi_type_voidback to summary
private static Pointer ffi_type_void

Constructor Detail

FFITypesback to summary
private FFITypes()
com.sun.jna back to summary

public Class Structure.FFIType.size_t

extends IntegerType
Class Inheritance

Constructor Summary

AccessConstructor and Description
public
public
size_t(long value)

Constructor Detail

size_tback to summary
public size_t()
size_tback to summary
public size_t(long value)
com.sun.jna back to summary

private Class Structure.MemberOrder

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
public int
public int
public int

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

firstback to summary
public int first
lastback to summary
public int last
middleback to summary
public int middle

Constructor Detail

MemberOrderback to summary
private MemberOrder()
com.sun.jna back to summary

pack-priv Class Structure.StructField

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
public FromNativeContext
public Field
public boolean
public String
public int
public FromNativeConverter
public int
public Class<T>
public ToNativeConverter

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

contextback to summary
public FromNativeContext context
fieldback to summary
public Field field
isVolatileback to summary
public boolean isVolatile
nameback to summary
public String name
offsetback to summary
public int offset
readConverterback to summary
public FromNativeConverter readConverter
sizeback to summary
public int size
typeback to summary
public Class<T> type
writeConverterback to summary
public ToNativeConverter writeConverter

Constructor Detail

StructFieldback to summary
pack-priv StructField()