Skip to main content

OpcUaVariant

OpcUaVariant It is a OPC UA generic value container that covers all OPC UA built-in data types. SDK Use it to pass values between Java and native code.

Implementation AutoCloseable — Hold the native handle, Must close.

Supporting

Public Methods​

CategoryPropertyTypeAccessDescription
Statushandle()PointerRInternal native handle (for debugging)
getDataType()Enums.BuiltinTypeRBuilt-in data types
Construction / ConfigurationsetBoolean / setSByte / setByte / setInt16 / setUInt16 / setInt32 / setUInt32 / setInt64 / setFloat / setDouble / setString / setDateTimeOpcUaVariant (this)WChained assignment; returns the object itself
Value RetrievalasBoolean()BooleanRReturns null if the types do not match
asInt32()IntegerRReturns null if the types do not match
asUInt32()LongRConverts to an unsigned value using Long
asInt64()LongRReturns null if the types do not match
asDouble()DoubleRReturns null if the types do not match
asString()StringRReturns null if the types do not match
asFileTime()longRDateTime Returns the Windows timestamp FileTime; 0 = empty
tryGet StyletryGetBoolean(IntByReference)booleanRReturns true on success; the result is stored in IntByReference
tryGetInt32 / tryGetUInt32 / tryGetInt64 / tryGetDoublebooleanRSame as above; outputs the corresponding type by reference
Releaseclose()void—Releases native memory (when owns=true)

Construction and Initialization (Chained)​

new OpcUaVariant().setBoolean(true)            // Boolean
new OpcUaVariant().setSByte((byte) -1) // SByte
new OpcUaVariant().setByte(255) // Byte
new OpcUaVariant().setInt16((short) 42) // Int16
new OpcUaVariant().setUInt16(42) // UInt16
new OpcUaVariant().setInt32(42) // Int32
new OpcUaVariant().setUInt32(42) // UInt32
new OpcUaVariant().setInt64(42L) // Int64
new OpcUaVariant().setFloat(3.14f) // Float
new OpcUaVariant().setDouble(3.14) // Double
new OpcUaVariant().setString("hello") // String
new OpcUaVariant().setDateTime(fileTimeLong) // DateTime (Windows FileTime)

Read (Strongly Typed)​

try (OpcUaDataValue dv = ua.read("ns=2;s=T1")) {
OpcUaVariant v = dv.variant(); // If you don't hold it, don't close it.
Double d = v.asDouble(); // null = Type mismatch
Integer i = v.asInt32();
String s = v.asString();
Boolean b = v.asBoolean();
long ft = v.asFileTime(); // DateTime
}

Type Mismatch Returns null (Java SDK Style), Do not throw exceptions—perform explicit null checks at the higher level.

tryGet (out-style)​

import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.DoubleByReference;

OpcUaVariant v = dv.variant();

IntByReference outI = new IntByReference();
if (v.tryGetInt32(outI)) {
int n = outI.getValue();
}

DoubleByReference outD = new DoubleByReference();
if (v.tryGetDouble(outD)) {
double x = outD.getValue();
}

DateTime ↔ Instant​

import java.time.Instant;

// FileTime → Instant
long ft = v.asFileTime();
Instant t = (ft > 0)
? Instant.ofEpochMilli((ft - 116444736000000000L) / 10000L)
: Instant.now();

// Instant → FileTime
long ft2 = t.toEpochMilli() * 10000L + 116444736000000000L;
v.setDateTime(ft2);

toString​

OpcUaVariant.toString() Click DataType Smart Formatting:

DataTypeOutput Example
Double / Float3.14
Int32 / UInt3242
Stringhello
DateTimeft=132456... (FileTime raw, converted to Instant for display)
Other<TypeName>

Memory Ownership​

OpcUaVariant Wrap the native handle; implementation AutoCloseable:

SourceownsShould I close it?
new OpcUaVariant() constructortrueYes
ua.read(...) Indirectly held (through OpcUaDataValue)—OpcUaDataValue Taken over by
ua.call(...) Returned outputstrueYes (close one by one)
OpcUaDataValue.variant() returnsfalseNo (released along DataValue with )

Best Practices:

try (OpcUaVariant v = new OpcUaVariant().setDouble(42.0)) {
ua.write("ns=2;s=X", v);
} // Auto-close
Consequences of Forgetting to Close Resources

Failing to close resources will cause native memory leaks (memory usage will increase after running the code 100k+ times). Always use resources created with the try-with-resources block OpcUaVariant.

BuiltinType Complete List (Enums.BuiltinType)​

Null (0), Boolean (1), SByte (2), Byte (3),
Int16 (4), UInt16 (5), Int32 (6), UInt32 (7), Int64 (8), UInt64 (9),
Float (10), Double (11), String (12), DateTime (13), Guid (14),
ByteString (15), XmlElement (16), NodeId (17), ExpandedNodeId (18),
StatusCode (19), QualifiedName (20), LocalizedText (21),
ExtensionObject (22), DataValue (23), Variant (24), DiagnosticInfo (25)

Array Types​

The setXxx/asXxx exposed by the current Java SDK Variant mainly target scalars. Array read/write operations are not currently supported; they will be added in future versions.

ExtensionObject (Complex Structure)​

OPC UA server-side custom Struct DataTypes are wrapped in ExtensionObjects. The SDK currently provides only basic ExtensionObject containers; full schema parsing arrives in a later version.

Next Step​