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/10/23 08:20:27 UTC

[plc4x] 01/02: - Renamed the method to get an enum constant from a value from valueOf to enumForValue as for string based enums this was causing problems as enums always automatically have valueOf(String) methods

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

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

commit 1dec03b5b516c19958ec4a3b077f237eaf661e14
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Thu Oct 22 23:16:33 2020 +0200

    - Renamed the method to get an enum constant from a value from valueOf to enumForValue as for string based enums this was causing problems as enums always automatically have valueOf(String) methods
---
 .../src/main/resources/templates/java/enum-template.ftlh     |  4 ++--
 .../src/main/resources/templates/java/io-template.ftlh       |  2 +-
 .../org/apache/plc4x/java/ads/protocol/AdsProtocolLogic.java |  4 ++--
 .../java/org/apache/plc4x/java/modbus/field/ModbusField.java |  2 +-
 .../plc4x/java/modbus/protocol/ModbusProtocolLogic.java      | 12 ++++++------
 .../plc4x/java/s7/readwrite/utils/S7TsapIdEncoder.java       |  2 +-
 6 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/build-utils/language-java/src/main/resources/templates/java/enum-template.ftlh b/build-utils/language-java/src/main/resources/templates/java/enum-template.ftlh
index 6ebc76f..d77e7c8 100644
--- a/build-utils/language-java/src/main/resources/templates/java/enum-template.ftlh
+++ b/build-utils/language-java/src/main/resources/templates/java/enum-template.ftlh
@@ -57,7 +57,7 @@ import java.util.Map;
 public enum ${type.name} {
 
 <#list type.enumValues as enumValue>
-    ${enumValue.name}(<#if type.type?has_content>(${helper.getLanguageTypeNameForTypeReference(type.type, true)}) ${enumValue.value}</#if><#if type.constantNames?has_content><#if type.type?has_content>, </#if><#list type.constantNames as constantName>(${helper.getLanguageTypeNameForTypeReference(type.getConstantType(constantName), true)}) ${helper.escapeValue(type.getConstantType(constantName), enumValue.getConstant(constantName))}<#sep>, </#sep></#list></#if>)<#sep>,
+    ${enumValue.name}(<#if type.type?has_content>(${helper.getLanguageTypeNameForTypeReference(type.type, true)}) <#if helper.isStringTypeReference(type.type)>"${enumValue.value}"<#else>${enumValue.value}</#if></#if><#if type.constantNames?has_content><#if type.type?has_content>, </#if><#list type.constantNames as constantName>(${helper.getLanguageTypeNameForTypeReference(type.getConstantType(constantName), true)}) ${helper.escapeValue(type.getConstantType(constantName), enumValue.getCon [...]
 </#sep></#list>;
 
 <#if type.type?has_content>
@@ -103,7 +103,7 @@ public enum ${type.name} {
     </#list>
 </#if>
 <#if type.type?has_content>
-    public static ${type.name} valueOf(${helper.getLanguageTypeNameForTypeReference(type.type, true)} value) {
+    public static ${type.name} enumForValue(${helper.getLanguageTypeNameForTypeReference(type.type, true)} value) {
         if (!map.containsKey(value)) {
             logger.error("No ${type.name} for value {}", value);
         }
diff --git a/build-utils/language-java/src/main/resources/templates/java/io-template.ftlh b/build-utils/language-java/src/main/resources/templates/java/io-template.ftlh
index 5b00414..a29c531 100644
--- a/build-utils/language-java/src/main/resources/templates/java/io-template.ftlh
+++ b/build-utils/language-java/src/main/resources/templates/java/io-template.ftlh
@@ -246,7 +246,7 @@ public class ${type.name}IO implements <#if outputFlavor != "passive">MessageIO<
         <#assign enumField = field>
 
         // Enum field (${enumField.name})
-        ${helper.getLanguageTypeNameForField(field)} ${enumField.name} = ${helper.getLanguageTypeNameForField(field)}.valueOf(${helper.getReadBufferReadMethodCall(helper.getEnumBaseTypeReference(enumField.type))});
+        ${helper.getLanguageTypeNameForField(field)} ${enumField.name} = ${helper.getLanguageTypeNameForField(field)}.enumForValue(${helper.getReadBufferReadMethodCall(helper.getEnumBaseTypeReference(enumField.type))});
         <#break>
     <#case "implicit">
         <#assign implicitField = field>
diff --git a/plc4j/drivers/ads/src/main/java/org/apache/plc4x/java/ads/protocol/AdsProtocolLogic.java b/plc4j/drivers/ads/src/main/java/org/apache/plc4x/java/ads/protocol/AdsProtocolLogic.java
index 69a00ec..a9a1d8c 100644
--- a/plc4j/drivers/ads/src/main/java/org/apache/plc4x/java/ads/protocol/AdsProtocolLogic.java
+++ b/plc4j/drivers/ads/src/main/java/org/apache/plc4x/java/ads/protocol/AdsProtocolLogic.java
@@ -265,7 +265,7 @@ public class AdsProtocolLogic extends Plc4xProtocolBase<AmsTCPPacket> implements
             // in sequence and then come the values.
             for (String fieldName : readRequest.getFieldNames()) {
                 try {
-                    final ReturnCode result = ReturnCode.valueOf(readBuffer.readUnsignedLong(32));
+                    final ReturnCode result = ReturnCode.enumForValue(readBuffer.readUnsignedLong(32));
                     responseCodes.put(fieldName, parsePlcResponseCode(result));
                 } catch (ParseException e) {
                     responseCodes.put(fieldName, PlcResponseCode.INTERNAL_ERROR);
@@ -494,7 +494,7 @@ public class AdsProtocolLogic extends Plc4xProtocolBase<AmsTCPPacket> implements
             // in sequence and then come the values.
             for (String fieldName : writeRequest.getFieldNames()) {
                 try {
-                    final ReturnCode result = ReturnCode.valueOf(readBuffer.readUnsignedLong(32));
+                    final ReturnCode result = ReturnCode.enumForValue(readBuffer.readUnsignedLong(32));
                     responseCodes.put(fieldName, parsePlcResponseCode(result));
                 } catch (ParseException e) {
                     responseCodes.put(fieldName, PlcResponseCode.INTERNAL_ERROR);
diff --git a/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/field/ModbusField.java b/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/field/ModbusField.java
index 641296e..9a47936 100644
--- a/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/field/ModbusField.java
+++ b/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/field/ModbusField.java
@@ -67,7 +67,7 @@ public abstract class ModbusField implements PlcField {
             throw new IllegalArgumentException("quantity must be greater then zero. Was " + this.quantity);
         }
         this.dataType = dataType != null ? dataType : "INT";
-        ModbusDataType.valueOf(this.dataType);
+        ModbusDataTypeSizes.valueOf(this.dataType);
     }
 
     public int getAddress() {
diff --git a/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/protocol/ModbusProtocolLogic.java b/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/protocol/ModbusProtocolLogic.java
index 6b4f4ce..9df1bac 100644
--- a/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/protocol/ModbusProtocolLogic.java
+++ b/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/protocol/ModbusProtocolLogic.java
@@ -363,7 +363,7 @@ public class ModbusProtocolLogic extends Plc4xProtocolBase<ModbusTcpADU> impleme
             ModbusPDUReadInputRegistersRequest req = (ModbusPDUReadInputRegistersRequest) request;
             ModbusPDUReadInputRegistersResponse resp = (ModbusPDUReadInputRegistersResponse) response;
             ReadBuffer io = new ReadBuffer(resp.getValue());
-            return DataItemIO.staticParse(io, dataType, (short) Math.round(req.getQuantity()/(fieldDataTypeSize/2.0f)));
+            return DataItemIO.staticParse(io, dataType, Math.round(req.getQuantity()/(fieldDataTypeSize/2.0f)));
         } else if (request instanceof ModbusPDUReadHoldingRegistersRequest) {
             if (!(response instanceof ModbusPDUReadHoldingRegistersResponse)) {
                 throw new PlcRuntimeException("Unexpected response type. " +
@@ -372,7 +372,7 @@ public class ModbusProtocolLogic extends Plc4xProtocolBase<ModbusTcpADU> impleme
             ModbusPDUReadHoldingRegistersRequest req = (ModbusPDUReadHoldingRegistersRequest) request;
             ModbusPDUReadHoldingRegistersResponse resp = (ModbusPDUReadHoldingRegistersResponse) response;
             ReadBuffer io = new ReadBuffer(resp.getValue());
-            return DataItemIO.staticParse(io, dataType, (short) Math.round(req.getQuantity()/(fieldDataTypeSize/2.0f)));
+            return DataItemIO.staticParse(io, dataType, Math.round(req.getQuantity()/(fieldDataTypeSize/2.0f)));
         } else if (request instanceof ModbusPDUReadFileRecordRequest) {
             if (!(response instanceof ModbusPDUReadFileRecordResponse)) {
                 throw new PlcRuntimeException("Unexpected response type. " +
@@ -396,7 +396,7 @@ public class ModbusProtocolLogic extends Plc4xProtocolBase<ModbusTcpADU> impleme
                   "Expected " + req.getItems().length + ", but got " + resp.getItems().length);
             }
 
-            return DataItemIO.staticParse(io, dataType, (short) Math.round((dataLength/2.0f)/(fieldDataTypeSize/2.0f)));
+            return DataItemIO.staticParse(io, dataType, Math.round((dataLength/2.0f)/(fieldDataTypeSize/2.0f)));
         }
         return null;
     }
@@ -406,7 +406,7 @@ public class ModbusProtocolLogic extends Plc4xProtocolBase<ModbusTcpADU> impleme
         try {
             WriteBuffer buffer;
             if(plcValue instanceof PlcList) {
-                buffer = DataItemIO.staticSerialize(plcValue, fieldDataType, (short) ((PlcList) plcValue).getLength(), false);
+                buffer = DataItemIO.staticSerialize(plcValue, fieldDataType, plcValue.getLength(), false);
                 byte[] data = buffer.getData();
                 switch (((ModbusField) field).getDataType()) {
                     case "BOOL":
@@ -421,7 +421,7 @@ public class ModbusProtocolLogic extends Plc4xProtocolBase<ModbusTcpADU> impleme
                         return data;
                 }
             } else {
-                buffer = DataItemIO.staticSerialize(plcValue, fieldDataType, (short) 1, false);
+                buffer = DataItemIO.staticSerialize(plcValue, fieldDataType, 1, false);
                 if (buffer != null) {
                     return buffer.getData();
                 } else {
@@ -446,7 +446,7 @@ public class ModbusProtocolLogic extends Plc4xProtocolBase<ModbusTcpADU> impleme
     private PlcValue readBooleanList(int count, byte[] data) throws ParseException {
         ReadBuffer io = new ReadBuffer(data);
         if(count == 1) {
-            return DataItemIO.staticParse(io, "IEC61131_BOOL", (short) 1);
+            return DataItemIO.staticParse(io, "IEC61131_BOOL", 1);
         }
         // Make sure we read in all the bytes. Unfortunately when requesting 9 bytes
         // they are ordered like this: 8 7 6 5 4 3 2 1 | 0 0 0 0 0 0 0 9
diff --git a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/utils/S7TsapIdEncoder.java b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/utils/S7TsapIdEncoder.java
index edd8b3c..5204bdf 100644
--- a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/utils/S7TsapIdEncoder.java
+++ b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/utils/S7TsapIdEncoder.java
@@ -34,7 +34,7 @@ public class S7TsapIdEncoder {
 
     public static DeviceGroup decodeDeviceGroup(short tsapId) {
         byte deviceGroupCode = (byte) ((tsapId >> 8) & (0xFF));
-        return DeviceGroup.valueOf(deviceGroupCode);
+        return DeviceGroup.enumForValue(deviceGroupCode);
     }
 
     public static int decodeRack(short tsapId) {