From dev-return-4837-apmail-plc4x-dev-archive=plc4x.apache.org@plc4x.apache.org Tue Oct 6 09:23:02 2020 Return-Path: X-Original-To: apmail-plc4x-dev-archive@locus.apache.org Delivered-To: apmail-plc4x-dev-archive@locus.apache.org Received: from mxout1-ec2-va.apache.org (mxout1-ec2-va.apache.org [3.227.148.255]) by minotaur.apache.org (Postfix) with ESMTP id E41E219852 for ; Tue, 6 Oct 2020 09:23:01 +0000 (UTC) Received: from mail.apache.org (mailroute1-lw-us.apache.org [207.244.88.153]) by mxout1-ec2-va.apache.org (ASF Mail Server at mxout1-ec2-va.apache.org) with SMTP id 948B143AD7 for ; Tue, 6 Oct 2020 09:23:01 +0000 (UTC) Received: (qmail 4438 invoked by uid 500); 6 Oct 2020 09:23:01 -0000 Delivered-To: apmail-plc4x-dev-archive@plc4x.apache.org Received: (qmail 4412 invoked by uid 500); 6 Oct 2020 09:23:01 -0000 Mailing-List: contact dev-help@plc4x.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@plc4x.apache.org Delivered-To: mailing list dev@plc4x.apache.org Received: (qmail 4332 invoked by uid 99); 6 Oct 2020 09:23:01 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 06 Oct 2020 09:23:01 +0000 From: =?utf-8?q?GitBox?= To: dev@plc4x.apache.org Subject: =?utf-8?q?=5BGitHub=5D_=5Bplc4x=5D_hutcheb_commented_on_a_change_in_pull_req?= =?utf-8?q?uest_=23192=3A_Refactor_Field_Handler_Classes?= Message-ID: <160197618109.32230.17118562436212636832.asfpy@gitbox.apache.org> Date: Tue, 06 Oct 2020 09:23:01 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit In-Reply-To: References: 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 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: users@infra.apache.org