You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2020/03/09 11:33:55 UTC

[plc4x] 01/03: - Changed the generated code to use getSizeInBits and keep getSizeInBytes as a convenience method (Needed if data types are not byte aligned)

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

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

commit db74d1182324c41fa7eb0c7e1ec8dd59bb52d60a
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Mon Mar 9 12:21:17 2020 +0100

    - Changed the generated code to use getSizeInBits and keep getSizeInBytes as a convenience method (Needed if data types are not byte aligned)
---
 .../src/main/resources/templates/java/pojo-template.ftlh | 16 +++++++++++-----
 .../org/apache/plc4x/java/spi/generation/Message.java    |  2 ++
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/build-utils/language-java/src/main/resources/templates/java/pojo-template.ftlh b/build-utils/language-java/src/main/resources/templates/java/pojo-template.ftlh
index a0bb32c..c689e0a 100644
--- a/build-utils/language-java/src/main/resources/templates/java/pojo-template.ftlh
+++ b/build-utils/language-java/src/main/resources/templates/java/pojo-template.ftlh
@@ -124,7 +124,13 @@ public<#if type.abstract> abstract</#if> class ${typeName}<#if type.parentType??
     @Override
     @JsonIgnore
     public int getLengthInBytes() {
-        int lengthInBits = <#if type.parentType??>super.getLengthInBytes() * 8<#else>0</#if>;
+        return getLengthInBits() / 8;
+    }
+
+    @Override
+    @JsonIgnore
+    public int getLengthInBits() {
+        int lengthInBits = <#if type.parentType??>super.getLengthInBits()<#else>0</#if>;
 <#list type.fields as field>
 <#switch field.typeName>
     <#case "array">
@@ -135,7 +141,7 @@ public<#if type.abstract> abstract</#if> class ${typeName}<#if type.parentType??
         <#else>
         if(${field.name} != null) {
             for(Message element : ${field.name}) {
-                lengthInBits += element.getLengthInBytes() * 8;
+                lengthInBits += element.getLengthInBits();
             }
         }
         </#if>
@@ -182,7 +188,7 @@ public<#if type.abstract> abstract</#if> class ${typeName}<#if type.parentType??
         <#if helper.isSimpleType(field.type)>
             lengthInBits += ${field.type.sizeInBits};
         <#else>
-            lengthInBits += ${field.name}.getLengthInBytes() * 8;
+            lengthInBits += ${field.name}.getLengthInBits();
         </#if>
         }
         <#break>
@@ -205,7 +211,7 @@ public<#if type.abstract> abstract</#if> class ${typeName}<#if type.parentType??
         <#if helper.isSimpleType(field.type)>
         lengthInBits += ${field.type.sizeInBits};
         <#else>
-        lengthInBits += ${field.name}.getLengthInBytes() * 8;
+        lengthInBits += ${field.name}.getLengthInBits();
         </#if>
         <#break>
     <#case "switch">
@@ -219,7 +225,7 @@ public<#if type.abstract> abstract</#if> class ${typeName}<#if type.parentType??
 </#switch>
 </#list>
 
-        return lengthInBits / 8;
+        return lengthInBits;
     }
 
     public PlcValue toPlcValue() {
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/Message.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/Message.java
index 84c1bbc..7d9afef 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/Message.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/Message.java
@@ -25,6 +25,8 @@ public interface Message {
 
     int getLengthInBytes();
 
+    int getLengthInBits();
+
     PlcValue toPlcValue();
 
     MessageIO<? extends Message, ? extends Message> getMessageIO();