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.
- Package ID + Timestamp + Status—please see OpcUaDataValue.
- For standard node constants, see WellKnownNodes.
Public Methods
| Category | Property | Type | Access | Description |
|---|---|---|---|---|
| Status | handle() | Pointer | R | Internal native handle (for debugging) |
getDataType() | Enums.BuiltinType | R | Built-in data types | |
| Construction / Configuration | setBoolean / setSByte / setByte / setInt16 / setUInt16 / setInt32 / setUInt32 / setInt64 / setFloat / setDouble / setString / setDateTime | OpcUaVariant (this) | W | Chained assignment; returns the object itself |
| Value Retrieval | asBoolean() | Boolean | R | Returns null if the types do not match |
asInt32() | Integer | R | Returns null if the types do not match | |
asUInt32() | Long | R | Converts to an unsigned value using Long | |
asInt64() | Long | R | Returns null if the types do not match | |
asDouble() | Double | R | Returns null if the types do not match | |
asString() | String | R | Returns null if the types do not match | |
asFileTime() | long | R | DateTime Returns the Windows timestamp FileTime; 0 = empty | |
| tryGet Style | tryGetBoolean(IntByReference) | boolean | R | Returns true on success; the result is stored in IntByReference |
tryGetInt32 / tryGetUInt32 / tryGetInt64 / tryGetDouble | boolean | R | Same as above; outputs the corresponding type by reference | |
| Release | close() | 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:
| DataType | Output Example |
|---|---|
| Double / Float | 3.14 |
| Int32 / UInt32 | 42 |
| String | hello |
| DateTime | ft=132456... (FileTime raw, converted to Instant for display) |
| Other | <TypeName> |
Memory Ownership
OpcUaVariant Wrap the native handle; implementation AutoCloseable:
| Source | owns | Should I close it? |
|---|---|---|
| new OpcUaVariant() constructor | true | Yes |
| ua.read(...) Indirectly held (through OpcUaDataValue) | — | OpcUaDataValue Taken over by |
| ua.call(...) Returned outputs | true | Yes (close one by one) |
| OpcUaDataValue.variant() returns | false | No (released along DataValue with ) |
Best Practices:
try (OpcUaVariant v = new OpcUaVariant().setDouble(42.0)) {
ua.write("ns=2;s=X", v);
} // Auto-close
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.