JNA API 3.0.3

com.sun.jna
Class Union

java.lang.Object
  extended by com.sun.jna.Structure
      extended by com.sun.jna.Union

public abstract class Union
extends Structure

Represents a native union. When writing to native memory, the field corresponding to the type passed to setType(java.lang.Class) will be written to native memory. Upon reading from native memory, Structure, String, or WString fields will not be initialized unless they are the current field as identified by a call to setType(java.lang.Class). The current field is always unset by default to avoid accidentally attempting to read a field that is not valid. In the case of a String, for instance, an invalid pointer may result in a memory fault when attempting to initialize the String.


Nested Class Summary
 
Nested classes/interfaces inherited from class com.sun.jna.Structure
Structure.ByReference, Structure.ByValue, Structure.StructField
 
Field Summary
 
Fields inherited from class com.sun.jna.Structure
ALIGN_DEFAULT, ALIGN_GNUC, ALIGN_MSVC, ALIGN_NONE, CALCULATE_SIZE, isPPC, isSPARC
 
Constructor Summary
protected Union()
          Create a Union whose size and alignment will be calculated automatically.
protected Union(int size)
          Create a Union of the given size, using default alignment.
protected Union(int size, int alignType)
          Create a Union of the given size and alignment type.
 
Method Summary
(package private)  int calculateSize(boolean force)
          Adjust the size to be the size of the largest element, and ensure all fields begin at offset zero.
protected  int getNativeAlignment(Class type, Object value, boolean isFirstElement)
          All fields are considered the "first" element.
(package private)  Pointer getTypeInfo()
          Return type information for the largest field.
(package private)  Object readField(Structure.StructField field)
          Avoid reading pointer-based fields and structures unless explicitly selected.
 void setType(Class type)
          Indicates which field will be used to write to native memory.
(package private)  void writeField(Structure.StructField field)
          Only the currently selected field will be written.
 
Methods inherited from class com.sun.jna.Structure
allocateMemory, allocateMemory, calculateAlignedSize, clear, equals, fields, getField, getPointer, getStructAlignment, getTypeInfo, hashCode, newInstance, read, readField, setAlignType, setTypeMapper, size, toArray, toArray, toString, updateStructureByReference, useMemory, useMemory, write, writeField
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Union

protected Union()
Create a Union whose size and alignment will be calculated automatically.


Union

protected Union(int size)
Create a Union of the given size, using default alignment.


Union

protected Union(int size,
                int alignType)
Create a Union of the given size and alignment type.

Method Detail

setType

public void setType(Class type)
Indicates which field will be used to write to native memory.

Throws:
IllegalArgumentException - if the type does not correspond to any declared union field.

writeField

void writeField(Structure.StructField field)
Only the currently selected field will be written.

Overrides:
writeField in class Structure

readField

Object readField(Structure.StructField field)
Avoid reading pointer-based fields and structures unless explicitly selected. Structures may contain pointer-based fields which can crash the VM if not properly initialized.

Overrides:
readField in class Structure

calculateSize

int calculateSize(boolean force)
Adjust the size to be the size of the largest element, and ensure all fields begin at offset zero.

Overrides:
calculateSize in class Structure

getNativeAlignment

protected int getNativeAlignment(Class type,
                                 Object value,
                                 boolean isFirstElement)
All fields are considered the "first" element.

Overrides:
getNativeAlignment in class Structure

getTypeInfo

Pointer getTypeInfo()
Return type information for the largest field.

Overrides:
getTypeInfo in class Structure

JNA API 3.0.3

Copyright © 2007-2008 Timothy Wall. All Rights Reserved.