From b5fbee2f1205c31220021b8acf879699e8f1287d Mon Sep 17 00:00:00 2001 From: csowada Date: Tue, 29 Dec 2020 21:39:48 +0100 Subject: [PATCH 1/3] Fix NPE for NestedValues, simplify CfgReader --- .../ebus/cfg/std/EBusConfigurationReader.java | 119 ++++++++++-------- .../ebus/command/EBusCommandNestedValue.java | 11 +- 2 files changed, 69 insertions(+), 61 deletions(-) diff --git a/src/main/java/de/csdev/ebus/cfg/std/EBusConfigurationReader.java b/src/main/java/de/csdev/ebus/cfg/std/EBusConfigurationReader.java index 244b981..0441e19 100644 --- a/src/main/java/de/csdev/ebus/cfg/std/EBusConfigurationReader.java +++ b/src/main/java/de/csdev/ebus/cfg/std/EBusConfigurationReader.java @@ -22,6 +22,9 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.function.BiConsumer; +import java.util.function.Consumer; +import java.util.function.Function; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -164,8 +167,8 @@ public EBusConfigurationReader() { // add md5 hash commandCollection.setIdentification(collection.getIdentification()); - // parse the template block - parseTemplateConfiguration(collection); + // parse the template list block + parseTemplateListConfiguration(collection); List commands = collection.getCommands(); if (commands != null) { @@ -179,7 +182,7 @@ public EBusConfigurationReader() { return commandCollection; } - protected void parseTemplateConfiguration(@NonNull EBusCollectionDTO collection) + protected void parseTemplateListConfiguration(@NonNull EBusCollectionDTO collection) throws EBusConfigurationReaderException { Objects.requireNonNull(collection, "collection"); @@ -188,36 +191,42 @@ protected void parseTemplateConfiguration(@NonNull EBusCollectionDTO collection) List templateSection = collection.getTemplates(); if (templateSection != null) { for (EBusCommandTemplatesDTO templates : templateSection) { - List templateValues = templates.getTemplate(); - if (templateValues != null) { + parseTemplateBlockConfiguration(templates.getTemplate(), collection, templates); + } + } + } - Collection blockList = new ArrayList<>(); + protected void parseTemplateBlockConfiguration(@Nullable List templateValues, @NonNull EBusCollectionDTO collection, @NonNull EBusCommandTemplatesDTO templates) + throws EBusConfigurationReaderException { - for (EBusValueDTO value : templateValues) { - if (value != null) { - Collection<@NonNull EBusCommandValue> pv = parseValueConfiguration(value, null, null, null); + if (templateValues == null) { + return; + } - if (pv != null && !pv.isEmpty()) { - blockList.addAll(pv); + Collection blockList = new ArrayList<>(); - // global id - String id = collection.getId() + "." + templates.getName() + "." + value.getName(); - logger.trace("Add template with global id {} to registry ...", id); - templateValueRegistry.put(id, pv); - } - } - } + for (EBusValueDTO value : templateValues) { + if (value != null) { + Collection<@NonNull EBusCommandValue> pv = parseValueConfiguration(value, null, null, null); - if (!blockList.isEmpty()) { - String id = collection.getId() + "." + templates.getName(); + if (!pv.isEmpty()) { + blockList.addAll(pv); - // global id - logger.trace("Add template block with global id {} to registry ...", id); - templateBlockRegistry.put(id, blockList); - } + // global id + String id = collection.getId() + "." + templates.getName() + "." + value.getName(); + logger.trace("Add template with global id {} to registry ...", id); + templateValueRegistry.put(id, pv); } } } + + if (!blockList.isEmpty()) { + String id = collection.getId() + "." + templates.getName(); + + // global id + logger.trace("Add template block with global id {} to registry ...", id); + templateBlockRegistry.put(id, blockList); + } } /** @@ -255,18 +264,13 @@ protected EBusCommand parseTelegramConfiguration(@NonNull IEBusCommandCollection Byte source = EBusUtils.toByte(commandElement.getSrc()); // read in template block - List templates = commandElement.getTemplate(); - if (templates != null) { - for (EBusValueDTO template : templates) { - if (template != null) { - for (EBusCommandValue templateCfg : parseValueConfiguration(template, null, null, null)) { - if (StringUtils.isEmpty(templateCfg.getName())) { - templateMap.put(templateCfg.getName(), templateCfg); - } - - templateList.add(templateCfg); - } + for (EBusValueDTO template : checkedList(commandElement.getTemplate())) { + for (EBusCommandValue templateCfg : parseValueConfiguration(template, null, null, null)) { + if (StringUtils.isEmpty(templateCfg.getName())) { + templateMap.put(templateCfg.getName(), templateCfg); } + + templateList.add(templateCfg); } } @@ -315,28 +319,16 @@ protected EBusCommand parseTelegramConfiguration(@NonNull IEBusCommandCollection commandMethod.setDestinationAddress(destination); commandMethod.setSourceAddress(source); - List master = commandMethodElement.getMaster(); - if (master != null) { - for (EBusValueDTO template : master) { - if (template != null) { - for (EBusCommandValue ev : parseValueConfiguration(template, templateMap, templateList, - commandMethod)) { - commandMethod.addMasterValue(ev); - } + for (EBusValueDTO template : checkedList(commandMethodElement.getMaster())) { + for (EBusCommandValue ev : parseValueConfiguration(template, templateMap, templateList, commandMethod)) { + commandMethod.addMasterValue(ev); } - } } - List slave = commandMethodElement.getSlave(); - if (slave != null) { - for (EBusValueDTO template : slave) { - if (template != null) { - for (EBusCommandValue ev : parseValueConfiguration(template, templateMap, templateList, - commandMethod)) { - commandMethod.addSlaveValue(ev); - } + for (EBusValueDTO template : checkedList(commandMethodElement.getSlave())) { + for (EBusCommandValue ev : parseValueConfiguration(template, templateMap, templateList, commandMethod)) { + commandMethod.addSlaveValue(ev); } - } } // default type is always master-slave if not explicit set or a broadcast @@ -363,6 +355,23 @@ protected EBusCommand parseTelegramConfiguration(@NonNull IEBusCommandCollection return cfg; } + /** + * Helper function to work with a secure non null list + * @param master + * @return + */ + protected @NonNull List<@NonNull EBusValueDTO> checkedList(List master) { + List templates = new ArrayList<>(); + if (master != null) { + for (EBusValueDTO template : master) { + if (template != null) { + templates.add(template); + } + } + } + return templates; + } + /** * @param valueDto * @param templateMap @@ -447,7 +456,7 @@ protected EBusCommand parseTelegramConfiguration(@NonNull IEBusCommandCollection } else if (typeStr != null && typeStr.equals("template")) { String id = (String) valueDto.getProperty("id"); - String globalId = collectionId + "." + id; + String globalId = collectionId != null ? collectionId + "." + id : null; Collection<@NonNull EBusCommandValue> templateCollection = null; if (StringUtils.isEmpty(id)) { @@ -457,7 +466,7 @@ protected EBusCommand parseTelegramConfiguration(@NonNull IEBusCommandCollection if (templateValueRegistry.containsKey(id)) { templateCollection = templateValueRegistry.get(id); - } else if (templateValueRegistry.containsKey(globalId)) { + } else if (globalId != null && templateValueRegistry.containsKey(globalId)) { templateCollection = templateValueRegistry.get(globalId); } else if (templateMap != null && templateMap.containsKey(id)) { diff --git a/src/main/java/de/csdev/ebus/command/EBusCommandNestedValue.java b/src/main/java/de/csdev/ebus/command/EBusCommandNestedValue.java index db4dab2..16002ef 100644 --- a/src/main/java/de/csdev/ebus/command/EBusCommandNestedValue.java +++ b/src/main/java/de/csdev/ebus/command/EBusCommandNestedValue.java @@ -25,13 +25,12 @@ public class EBusCommandNestedValue extends EBusCommandValue implements IEBusNes @Override public void setParent(@Nullable EBusCommandMethod parent) { + if (parent != null) { + super.setParent(parent); - Objects.requireNonNull(parent, "parent"); - - super.setParent(parent); - - for (IEBusValue value : list) { - ((EBusCommandValue) value).setParent(parent); + for (IEBusValue value : list) { + ((EBusCommandValue) value).setParent(parent); + } } } From a707b519b60336c43f83a91e5967c37da3fa7173 Mon Sep 17 00:00:00 2001 From: csowada Date: Tue, 29 Dec 2020 22:12:46 +0100 Subject: [PATCH 2/3] remove unused imports --- .../java/de/csdev/ebus/cfg/std/EBusConfigurationReader.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/de/csdev/ebus/cfg/std/EBusConfigurationReader.java b/src/main/java/de/csdev/ebus/cfg/std/EBusConfigurationReader.java index 0441e19..18bb83c 100644 --- a/src/main/java/de/csdev/ebus/cfg/std/EBusConfigurationReader.java +++ b/src/main/java/de/csdev/ebus/cfg/std/EBusConfigurationReader.java @@ -22,9 +22,6 @@ import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.function.BiConsumer; -import java.util.function.Consumer; -import java.util.function.Function; import com.google.gson.Gson; import com.google.gson.GsonBuilder; From 7f00f2992454b715a1fa72c76474b6a17b66e633 Mon Sep 17 00:00:00 2001 From: csowada Date: Sun, 31 Jan 2021 12:38:49 +0100 Subject: [PATCH 3/3] Preparing next release --- CHANGELOG.md | 7 +++++++ pom.xml | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 455248b..8b15478 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,13 @@ All notable changes to this project will be documented in this file. ## Unreleased +## [1.1.5] - 2021-01-31 +### Changed +- Removed clone() from DataType objects +- Simplified EbusConfigurationReader methods +### Fixed +- Fix NPE for NestedValues + ## [1.1.4] - 2020-12-27 ### Changed - Enhanced the internal ThreadPool from 30 to 60 to prevent issues on startup diff --git a/pom.xml b/pom.xml index 913a1f4..ff8a96b 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ eBUS core library - This library handles the communication with heating engineering via the BUS specification. This protocol is used by many heating manufacturers in Europe. de.cs-dev.ebus ebus-core - 1.1.5-SNAPSHOT + 1.1.5 https://github.com/csowada/ebus bundle