You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2019/12/18 11:59:08 UTC

[plc4x] branch next-gen-core updated: fixed retrieval of lists

This is an automated email from the ASF dual-hosted git repository.

sruehl pushed a commit to branch next-gen-core
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/next-gen-core by this push:
     new dab9308  fixed retrieval of lists
dab9308 is described below

commit dab93088cadf02bbcb32cdd7639cd14b1a40ea1c
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Wed Dec 18 12:58:41 2019 +0100

    fixed retrieval of lists
---
 .../java/org/apache/plc4x/java/api/value/PlcList.java  | 18 ++++++++++--------
 .../org/apache/plc4x/java/api/value/PlcValues.java     | 17 ++++++++++++++++-
 2 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/plc4j/api/src/main/java/org/apache/plc4x/java/api/value/PlcList.java b/plc4j/api/src/main/java/org/apache/plc4x/java/api/value/PlcList.java
index e96af31..3b531e7 100644
--- a/plc4j/api/src/main/java/org/apache/plc4x/java/api/value/PlcList.java
+++ b/plc4j/api/src/main/java/org/apache/plc4x/java/api/value/PlcList.java
@@ -19,9 +19,6 @@
 
 package org.apache.plc4x.java.api.value;
 
-import org.apache.plc4x.java.api.value.PlcValue;
-import org.apache.plc4x.java.api.value.PlcValueAdapter;
-
 import java.util.Collections;
 import java.util.List;
 import java.util.stream.Collectors;
@@ -34,23 +31,28 @@ public class PlcList extends PlcValueAdapter {
         this.listItems = Collections.unmodifiableList(listItems);
     }
 
-    @Override public boolean isList() {
+    @Override
+    public boolean isList() {
         return true;
     }
 
-    @Override public int length() {
+    @Override
+    public int length() {
         return listItems.size();
     }
 
-    @Override public PlcValue getIndex(int i) {
+    @Override
+    public PlcValue getIndex(int i) {
         return listItems.get(i);
     }
 
-    @Override public List<? extends PlcValue> getList() {
+    @Override
+    public List<? extends PlcValue> getList() {
         return listItems;
     }
 
-    @Override public String toString() {
+    @Override
+    public String toString() {
         return "[" + listItems.stream().map(PlcValue::toString).collect(Collectors.joining(",")) + "]";
     }
 }
diff --git a/plc4j/api/src/main/java/org/apache/plc4x/java/api/value/PlcValues.java b/plc4j/api/src/main/java/org/apache/plc4x/java/api/value/PlcValues.java
index e856374..d485ef0 100644
--- a/plc4j/api/src/main/java/org/apache/plc4x/java/api/value/PlcValues.java
+++ b/plc4j/api/src/main/java/org/apache/plc4x/java/api/value/PlcValues.java
@@ -20,7 +20,10 @@
 package org.apache.plc4x.java.api.value;
 
 import org.apache.plc4x.java.api.exceptions.PlcIncompatibleDatatypeException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
+import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.util.Arrays;
 import java.util.Collections;
@@ -29,6 +32,8 @@ import java.util.Map;
 
 public class PlcValues {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(PlcValues.class);
+
     public static PlcValue of(String s) {
         return new PlcString(s);
     }
@@ -67,8 +72,18 @@ public class PlcValues {
 
     public static PlcValue of(Object o) {
         try {
-            return ((PlcValue) Class.forName(PlcValues.class.getPackage().getName() + ".Plc" + o.getClass().getSimpleName()).getConstructor(o.getClass()).newInstance(o));
+            String simpleName = o.getClass().getSimpleName();
+            Class<?> clazz = o.getClass();
+            switch (simpleName) {
+                case "ArrayList":
+                    simpleName = "List";
+                    clazz = List.class;
+            }
+            Constructor<?> constructor = Class.forName(PlcValues.class.getPackage().getName() + ".Plc" + simpleName).getDeclaredConstructor(clazz);
+            constructor.setAccessible(true);
+            return ((PlcValue) constructor.newInstance(o));
         } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | ClassNotFoundException e) {
+            LOGGER.warn("Cannot wrap", e);
             throw new PlcIncompatibleDatatypeException(o.getClass());
         }
     }