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 2021/11/25 12:55:52 UTC

[plc4x] branch develop updated: fix(plc4j/codegen): fix issue where unboxing didn't work properly

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

sruehl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new 5858893  fix(plc4j/codegen): fix issue where unboxing didn't work properly
5858893 is described below

commit 585889377626c7fae9a5cba323d00fb758ecedeb
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu Nov 25 13:55:44 2021 +0100

    fix(plc4j/codegen): fix issue where unboxing didn't work properly
---
 .../spi/codegen/fields/FieldReaderVirtual.java     | 42 +++++++++++++++++++++-
 1 file changed, 41 insertions(+), 1 deletion(-)

diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/codegen/fields/FieldReaderVirtual.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/codegen/fields/FieldReaderVirtual.java
index c2d5b18..4468eff 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/codegen/fields/FieldReaderVirtual.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/codegen/fields/FieldReaderVirtual.java
@@ -27,8 +27,48 @@ import org.slf4j.LoggerFactory;
 
 public class FieldReaderVirtual<T> implements FieldCommons {
 
-    @SuppressWarnings("unused")
+    @SuppressWarnings({"unused", "unchecked"})
     public T readVirtualField(Class<T> type, Object valueExpression, WithReaderArgs... readerArgs) throws ParseException {
+        if (type.isPrimitive()) {
+            // for primitives, we need to cast to the primitive as this does autoboxing
+            if (type == boolean.class) {
+                return (T) (Boolean) (boolean) valueExpression;
+            } else if (type == byte.class) {
+                Number valueExpressionNumber = (Number) valueExpression;
+                long longValue = valueExpressionNumber.longValue();
+                if ((byte) longValue != longValue) {
+                    throw new ArithmeticException("byte overflow");
+                }
+                return (T) (Byte) valueExpressionNumber.byteValue();
+            } else if (type == short.class) {
+                Number valueExpressionNumber = (Number) valueExpression;
+                long longValue = valueExpressionNumber.longValue();
+                if ((short) longValue != longValue) {
+                    throw new ArithmeticException("short overflow");
+                }
+                return (T) (Short) valueExpressionNumber.shortValue();
+            } else if (type == int.class) {
+                Number valueExpressionNumber = (Number) valueExpression;
+                long longValue = valueExpressionNumber.longValue();
+                if ((int) longValue != longValue) {
+                    throw new ArithmeticException("integer overflow");
+                }
+                return (T) (Integer) valueExpressionNumber.intValue();
+            } else if (type == long.class) {
+                Number valueExpressionNumber = (Number) valueExpression;
+                return (T) (Long) valueExpressionNumber.longValue();
+            } else if (type == char.class) {
+                return (T) (Character) (char) valueExpression;
+            } else if (type == float.class) {
+                Number valueExpressionNumber = (Number) valueExpression;
+                return (T) (Float) valueExpressionNumber.floatValue();
+            } else if (type == Double.class) {
+                Number valueExpressionNumber = (Number) valueExpression;
+                return (T) (Double) valueExpressionNumber.doubleValue();
+            } else {
+                throw new IllegalStateException("Unmapped primitive " + type);
+            }
+        }
         if (type == String.class) {
             return type.cast(String.valueOf(valueExpression));
         }