Skip navigation links
JNA API 5.12.1
com.sun.jna

Class Union

    • Constructor Detail

      • Union

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

        protected Union(Pointer p)
        Create a Union of the given size, using default alignment.
      • Union

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

        protected Union(TypeMapper mapper)
        Create a Union of the given size and alignment type.
      • Union

        protected Union(Pointer p,
                        int alignType,
                        TypeMapper mapper)
        Create a Union of the given size and alignment type.
    • Method Detail

      • getFieldOrder

        protected java.util.List<java.lang.String> getFieldOrder()
        Unions do not need a field order, so automatically provide a value to satisfy checking in the Structure superclass.
        Overrides:
        getFieldOrder in class Structure
        Returns:
        ordered list of field names
      • setType

        public void setType(java.lang.Class<?> type)
        Indicates by type which field will be used to write to native memory. If there are multiple fields of the same type, use setType(String) instead with the field name.
        Parameters:
        type - desired active type for the union
        Throws:
        java.lang.IllegalArgumentException - if the type does not correspond to any declared union field.
      • setType

        public void setType(java.lang.String fieldName)
        Indicates which field will be used to write to native memory.
        Parameters:
        fieldName - desired field to use for the active union type
        Throws:
        java.lang.IllegalArgumentException - if the name does not correspond to any declared union field.
      • readField

        public java.lang.Object readField(java.lang.String fieldName)
        Force a read of the given field from native memory.
        Overrides:
        readField in class Structure
        Parameters:
        fieldName - field to be read
        Returns:
        the new field value, after updating
        Throws:
        java.lang.IllegalArgumentException - if no field exists with the given name
      • writeField

        public void writeField(java.lang.String fieldName)
        Write the given field value to native memory. The given field will become the active one.
        Overrides:
        writeField in class Structure
        Parameters:
        fieldName - which field to synch
        Throws:
        java.lang.IllegalArgumentException - if no field exists with the given name
      • writeField

        public void writeField(java.lang.String fieldName,
                               java.lang.Object value)
        Write the given field value to the field and native memory. The given field will become the active one.
        Overrides:
        writeField in class Structure
        Parameters:
        fieldName - field to write
        value - value to write
        Throws:
        java.lang.IllegalArgumentException - if no field exists with the given name
      • getTypedValue

        public java.lang.Object getTypedValue(java.lang.Class<?> type)
        Reads the Structure field of the given type from memory, sets it as the active type and returns it. Convenience method for
        
         Union u;
         Class type;
         u.setType(type);
         u.read();
         value = u.field;
         
        Parameters:
        type - class type of the Structure field to read
        Returns:
        the Structure field with the given type
      • setTypedValue

        public java.lang.Object setTypedValue(java.lang.Object object)
        Set the active type and its value. Convenience method for
        
         Union u;
         Class type;
         u.setType(type);
         u.field = value;
         
        Parameters:
        object - instance of a class which is part of the union
        Returns:
        this Union object
      • writeField

        protected void writeField(Structure.StructField field)
        Only the currently selected field will be written.
        Overrides:
        writeField in class Structure
        Parameters:
        field - internal field representation to synch to native memory
      • readField

        protected java.lang.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
        Parameters:
        field - field to be read
        Returns:
        value of the requested field
      • getNativeAlignment

        protected int getNativeAlignment(java.lang.Class<?> type,
                                         java.lang.Object value,
                                         boolean isFirstElement)
        All fields are considered the "first" element.
        Overrides:
        getNativeAlignment in class Structure
        Parameters:
        type - field type
        value - field value, if available
        isFirstElement - is this field the first element in the struct?
        Returns:
        the native byte alignment
JNA API 5.12.1

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