hutcheb commented on a change in pull request #192:
URL: https://github.com/apache/plc4x/pull/192#discussion_r500131203
##########
File path:
plc4j/api/src/main/java/org/apache/plc4x/java/api/value/PlcValues.java
##########
@@ -355,6 +356,28 @@ public static PlcValue of(Map<String, PlcValue> map) {
return new PlcStruct(map);
}
+ private static PlcValue constructorHelper(Constructor<?> constructor,
Object value) {
+ try {
+ return (PlcValue) constructor.newInstance(value);
+ } catch (InstantiationException | IllegalAccessException |
InvocationTargetException e) {
+ throw new PlcIncompatibleDatatypeException(value.getClass());
+ }
+ }
+
+ public static PlcValue of(Object[] values, Class clazz) {
+ //Encode values to the type defined in clazz
+ try {
+ Constructor<?> constructor =
clazz.getDeclaredConstructor(values[0].getClass());
+ if(values.length == 1) {
+ return ((PlcValue) constructor.newInstance(values[0]));
+ } else {
+ return PlcValues.of(Arrays.stream(values).map(value ->
(constructorHelper(constructor, value))).collect(Collectors.toList()));
+ }
Review comment:
The exception would be raised if there wasn't a class (PlcBYTE, PlcINT,
etc..) that matched the datatype that was passed. It shouldn't be raised in
normal operation.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]