From f0c003e829bbd502f8ba492e6fa6c01a83e8b9c5 Mon Sep 17 00:00:00 2001 From: Gerd Zanker Date: Wed, 1 Mar 2023 20:35:35 +0100 Subject: [PATCH] Cleanup of static code analysis findings Three AvoidCatchingNPE findings are pending in the class BoschHttpClient. Could be false positive, because NullPointer exceptions appear during tests run. Signed-off-by: Gerd Zanker --- .../internal/devices/BoschSHCDeviceHandler.java | 1 - .../boschshc/internal/devices/BoschSHCHandler.java | 4 ---- .../internal/services/BoschSHCSystemService.java | 1 - .../services/batterylevel/BatteryLevelService.java | 2 -- .../internal/services/dto/BoschSHCServiceState.java | 11 +++++++---- .../smokedetectorcheck/SmokeDetectorCheckState.java | 1 - 6 files changed, 7 insertions(+), 13 deletions(-) diff --git a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/BoschSHCDeviceHandler.java b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/BoschSHCDeviceHandler.java index ffeaf49d7f105..2168e1223f114 100644 --- a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/BoschSHCDeviceHandler.java +++ b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/BoschSHCDeviceHandler.java @@ -53,7 +53,6 @@ public BoschSHCDeviceHandler(Thing thing) { @Override public void initialize() { - var config = this.config = getConfigAs(BoschSHCConfiguration.class); String deviceId = config.id; diff --git a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/BoschSHCHandler.java b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/BoschSHCHandler.java index 0ddb7ca420608..13f764f9386a5 100644 --- a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/BoschSHCHandler.java +++ b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/BoschSHCHandler.java @@ -124,7 +124,6 @@ protected BoschSHCHandler(Thing thing) { */ @Override public void initialize() { - // Initialize device services try { this.initializeServices(); @@ -304,7 +303,6 @@ protected , TState extends BoschSHCServ protected , TState extends BoschSHCServiceState> void registerService( TService service, Consumer stateUpdateListener, Collection affectedChannels, boolean shouldFetchInitialState) throws BoschSHCException { - String deviceId = verifyBoschID(); service.initialize(getBridgeHandler(), deviceId, stateUpdateListener); this.registerService(service, affectedChannels); @@ -325,7 +323,6 @@ protected , TState extends BoschSHCServ */ private , TState extends BoschSHCServiceState> void fetchInitialState( TService service, Consumer stateUpdateListener) { - try { @Nullable TState serviceState = service.getState(); @@ -353,7 +350,6 @@ private , TState extends BoschSHCServic */ protected void registerStatelessService(TService service) throws BoschSHCException { - String deviceId = verifyBoschID(); service.initialize(getBridgeHandler(), deviceId); // do not register in service list because the service can not receive state updates diff --git a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/services/BoschSHCSystemService.java b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/services/BoschSHCSystemService.java index 4df8799773fd9..71a02181dfc53 100644 --- a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/services/BoschSHCSystemService.java +++ b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/services/BoschSHCSystemService.java @@ -70,7 +70,6 @@ protected BoschSHCSystemService(String serviceName, Class stateClass, St @Override public @Nullable TState getState() throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException { - BridgeHandler bridgeHandler = getBridgeHandler(); if (bridgeHandler == null) { return null; diff --git a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/services/batterylevel/BatteryLevelService.java b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/services/batterylevel/BatteryLevelService.java index 7a8a7c0898820..a2afc39c1b215 100644 --- a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/services/batterylevel/BatteryLevelService.java +++ b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/services/batterylevel/BatteryLevelService.java @@ -38,12 +38,10 @@ public BatteryLevelService() { @Override public @Nullable DeviceServiceData getState() throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException { - String deviceId = getDeviceId(); if (deviceId == null) { return null; } - BridgeHandler bridgeHandler = getBridgeHandler(); if (bridgeHandler == null) { return null; diff --git a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/services/dto/BoschSHCServiceState.java b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/services/dto/BoschSHCServiceState.java index 6d0a28610c1db..ffe23619dc425 100644 --- a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/services/dto/BoschSHCServiceState.java +++ b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/services/dto/BoschSHCServiceState.java @@ -30,9 +30,12 @@ public class BoschSHCServiceState { /** * gson instance to convert a class to json string and back. */ - private static final Gson gson = new Gson(); + private static final Gson GSON = new Gson(); - private static final Logger logger = LoggerFactory.getLogger(BoschSHCServiceState.class); + /** + * Logger marked as transient to exclude the logger from JSON serialization. + */ + private final transient Logger logger = LoggerFactory.getLogger(BoschSHCServiceState.class); /** * State type. Initialized when instance is created. @@ -67,7 +70,7 @@ protected boolean isValid() { public static @Nullable TState fromJson(String json, Class stateClass) { - var state = gson.fromJson(json, stateClass); + var state = GSON.fromJson(json, stateClass); if (state == null || !state.isValid()) { return null; } @@ -77,7 +80,7 @@ protected boolean isValid() { public static @Nullable TState fromJson(JsonElement json, Class stateClass) { - var state = gson.fromJson(json, stateClass); + var state = GSON.fromJson(json, stateClass); if (state == null || !state.isValid()) { return null; } diff --git a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/services/smokedetectorcheck/SmokeDetectorCheckState.java b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/services/smokedetectorcheck/SmokeDetectorCheckState.java index bb194b1391c63..a7426b909c331 100644 --- a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/services/smokedetectorcheck/SmokeDetectorCheckState.java +++ b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/services/smokedetectorcheck/SmokeDetectorCheckState.java @@ -27,7 +27,6 @@ public enum SmokeDetectorCheckState { SMOKE_TEST_FAILED; public static SmokeDetectorCheckState from(String stateString) { - try { return SmokeDetectorCheckState.valueOf(stateString); } catch (Exception a) {