[knx] Reduce compiler warnings (#12518)

added NonNullByDefault annotations
rename functions and local variables not matching the naming scheme

Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
This commit is contained in:
Holger Friedrich 2022-03-27 11:27:39 +02:00 committed by GitHub
parent 9b7478c1fb
commit 414d7a84e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 96 additions and 65 deletions

View File

@ -18,6 +18,7 @@ import java.util.Collections;
import java.util.Set;
import java.util.stream.Stream;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.thing.ThingTypeUID;
/**
@ -26,6 +27,7 @@ import org.openhab.core.thing.ThingTypeUID;
*
* @author Karel Goderis - Initial contribution
*/
@NonNullByDefault
public class KNXBindingConstants {
public static final String BINDING_ID = "knx";

View File

@ -17,7 +17,6 @@ import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.types.Type;
import tuwien.auto.calimero.datapoint.Datapoint;
import tuwien.auto.calimero.process.ProcessEvent;
/**
* This interface must be implemented by classes that provide a type mapping
@ -25,7 +24,7 @@ import tuwien.auto.calimero.process.ProcessEvent;
* When a command or status update is sent to an item on the openHAB event bus,
* it must be clear, in which format it must be sent to KNX and vice versa.
*
* @author Kai Kreuzer
* @author Kai Kreuzer - Initial contribution
*
*/
@NonNullByDefault
@ -45,7 +44,8 @@ public interface KNXTypeMapper {
* maps a datapoint value to an openHAB command or state
*
* @param datapoint the source datapoint
* @param data the datapoint value as an ASDU byte array (see <code>{@link ProcessEvent}.getASDU()</code>)
* @param data the datapoint value as an ASDU byte array (see
* <code>{@link tuwien.auto.calimero.process.ProcessEvent}.getASDU()</code>)
* @return a command or state of openHAB
*/
@Nullable

View File

@ -14,8 +14,6 @@ package org.openhab.binding.knx.internal.channel;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.knx.internal.client.InboundSpec;
import org.openhab.binding.knx.internal.client.OutboundSpec;
import tuwien.auto.calimero.GroupAddress;
import tuwien.auto.calimero.KNXFormatException;
@ -57,7 +55,8 @@ public abstract class AbstractSpec {
/**
* Return the data point type.
* <p>
* See {@link InboundSpec#getDPT()} and {@link OutboundSpec#getDPT()}.
* See {@link org.openhab.binding.knx.internal.client.InboundSpec#getDPT()} and
* {@link org.openhab.binding.knx.internal.client.OutboundSpec#getDPT()}.
*
* @return the data point type.
*/

View File

@ -17,6 +17,7 @@ import static java.util.stream.Collectors.toList;
import java.util.Collections;
import java.util.List;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.knx.internal.client.InboundSpec;
@ -28,6 +29,7 @@ import tuwien.auto.calimero.GroupAddress;
* @author Simon Kaufmann - initial contribution and API.
*
*/
@NonNullByDefault
public class ListenSpecImpl extends AbstractSpec implements InboundSpec {
private final List<GroupAddress> listenAddresses;

View File

@ -244,7 +244,7 @@ public abstract class AbstractKNXClient implements NetworkLinkListener, KNXClien
});
}
private <T> T nullify(T target, @Nullable Consumer<T> lastWill) {
private <T> @Nullable T nullify(T target, @Nullable Consumer<T> lastWill) {
if (target != null && lastWill != null) {
lastWill.accept(target);
}
@ -392,7 +392,8 @@ public abstract class AbstractKNXClient implements NetworkLinkListener, KNXClien
@Override
public boolean isConnected() {
return link != null && link.isOpen();
final var tmpLink = link;
return tmpLink != null && tmpLink.isOpen();
}
@Override

View File

@ -12,6 +12,8 @@
*/
package org.openhab.binding.knx.internal.client;
import org.eclipse.jdt.annotation.NonNullByDefault;
import tuwien.auto.calimero.KNXException;
import tuwien.auto.calimero.knxnetip.KNXnetIPConnection;
import tuwien.auto.calimero.link.KNXNetworkLinkIP;
@ -24,6 +26,7 @@ import tuwien.auto.calimero.link.medium.KNXMediumSettings;
* @author Simon Kaufmann - initial contribution and API
*
*/
@NonNullByDefault
public class CustomKNXNetworkLinkIP extends KNXNetworkLinkIP {
public static final int TUNNELING = KNXNetworkLinkIP.TUNNELING;

View File

@ -100,7 +100,7 @@ public class DeviceInspector {
OPERATION_TIMEOUT);
if ((elements == null ? 0 : toUnsigned(elements)) == 1) {
Thread.sleep(OPERATION_INTERVAL);
String ManufacturerID = Manufacturer.getName(toUnsigned(getClient().readDeviceProperties(address,
String manufacturerID = Manufacturer.getName(toUnsigned(getClient().readDeviceProperties(address,
DEVICE_OBJECT, PID.MANUFACTURER_ID, 1, 1, false, OPERATION_TIMEOUT)));
Thread.sleep(OPERATION_INTERVAL);
String serialNo = toHex(getClient().readDeviceProperties(address, DEVICE_OBJECT, PID.SERIAL_NUMBER, 1,
@ -112,7 +112,7 @@ public class DeviceInspector {
String firmwareRevision = Integer.toString(toUnsigned(getClient().readDeviceProperties(address,
DEVICE_OBJECT, PID.FIRMWARE_REVISION, 1, 1, false, OPERATION_TIMEOUT)));
ret.put(MANUFACTURER_NAME, ManufacturerID);
ret.put(MANUFACTURER_NAME, manufacturerID);
if (serialNo != null) {
ret.put(MANUFACTURER_SERIAL_NO, serialNo);
}
@ -121,7 +121,7 @@ public class DeviceInspector {
}
ret.put(MANUFACTURER_FIRMWARE_REVISION, firmwareRevision);
logger.debug("Identified device {} as a {}, type {}, revision {}, serial number {}", address,
ManufacturerID, hardwareType, firmwareRevision, serialNo);
manufacturerID, hardwareType, firmwareRevision, serialNo);
} else {
logger.debug("The KNX device with address {} does not expose a Device Object", address);
}

View File

@ -15,7 +15,7 @@ package org.openhab.binding.knx.internal.client;
import java.util.Enumeration;
import java.util.concurrent.ScheduledExecutorService;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.thing.ThingUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -33,6 +33,7 @@ import tuwien.auto.calimero.link.medium.TPSettings;
* @author Simon Kaufmann - initial contribution and API.
*
*/
@NonNullByDefault
public class SerialClient extends AbstractKNXClient {
private final Logger logger = LoggerFactory.getLogger(SerialClient.class);
@ -48,7 +49,7 @@ public class SerialClient extends AbstractKNXClient {
}
@Override
protected @NonNull KNXNetworkLink establishConnection() throws KNXException, InterruptedException {
protected KNXNetworkLink establishConnection() throws KNXException, InterruptedException {
try {
RXTXVersion.getVersion();
logger.debug("Establishing connection to KNX bus through FT1.2 on serial port {}.", serialPort);
@ -59,12 +60,13 @@ public class SerialClient extends AbstractKNXClient {
"The serial FT1.2 KNX connection requires the RXTX libraries to be available, but they could not be found!",
e);
} catch (KNXException e) {
if (e.getMessage().startsWith("can not open serial port")) {
final String msg = e.getMessage();
if ((msg != null) && (msg.startsWith("can not open serial port"))) {
StringBuilder sb = new StringBuilder("Available ports are:\n");
Enumeration<?> portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if ((id != null) && (id.getPortType() == CommPortIdentifier.PORT_SERIAL)) {
sb.append(id.getName());
sb.append("\n");
}

View File

@ -14,19 +14,20 @@ package org.openhab.binding.knx.internal.config;
import java.math.BigDecimal;
import org.openhab.binding.knx.internal.handler.KNXBridgeBaseThingHandler;
import org.eclipse.jdt.annotation.NonNullByDefault;
/**
* {@link KNXBridgeBaseThingHandler} configuration
* {@link org.openhab.binding.knx.internal.handler.KNXBridgeBaseThingHandler} configuration
*
* @author Simon Kaufmann - initial contribution and API
*
*/
@NonNullByDefault
public class BridgeConfiguration {
private int autoReconnectPeriod;
private BigDecimal readingPause;
private BigDecimal readRetriesLimit;
private BigDecimal responseTimeout;
private int autoReconnectPeriod = 0;
private BigDecimal readingPause = BigDecimal.valueOf(0);
private BigDecimal readRetriesLimit = BigDecimal.valueOf(0);
private BigDecimal responseTimeout = BigDecimal.valueOf(0);
public int getAutoReconnectPeriod() {
return autoReconnectPeriod;

View File

@ -14,18 +14,21 @@ package org.openhab.binding.knx.internal.config;
import java.math.BigDecimal;
import org.eclipse.jdt.annotation.NonNullByDefault;
/**
* Configuration object for the device thing handler.
*
* @author Karel Goderis - Initial contribution
* @author Simon Kaufmann - refactoring & cleanup
*/
@NonNullByDefault
public class DeviceConfig {
private String address;
private boolean fetch;
private BigDecimal pingInterval;
private BigDecimal readInterval;
private String address = "";
private boolean fetch = false;
private BigDecimal pingInterval = BigDecimal.valueOf(0);
private BigDecimal readInterval = BigDecimal.valueOf(0);
public String getAddress() {
return address;

View File

@ -14,20 +14,23 @@ package org.openhab.binding.knx.internal.config;
import java.math.BigDecimal;
import org.eclipse.jdt.annotation.NonNullByDefault;
/**
* IP Bridge handler configuration object.
*
* @author Simon Kaufmann - initial contribution and API
*
*/
@NonNullByDefault
public class IPBridgeConfiguration extends BridgeConfiguration {
private boolean useNAT;
private String type;
private String ipAddress;
private BigDecimal portNumber;
private String localIp;
private String localSourceAddr;
private boolean useNAT = false;
private String type = "";
private String ipAddress = "";
private BigDecimal portNumber = BigDecimal.valueOf(0);
private String localIp = "";
private String localSourceAddr = "";
public Boolean getUseNAT() {
return useNAT;

View File

@ -12,15 +12,18 @@
*/
package org.openhab.binding.knx.internal.config;
import org.eclipse.jdt.annotation.NonNullByDefault;
/**
* Serial Bridge configuration object.
*
* @author Simon Kaufmann - initial contribution and API.
*
*/
@NonNullByDefault
public class SerialBridgeConfiguration extends BridgeConfiguration {
private String serialPort;
private String serialPort = "";
public String getSerialPort() {
return serialPort;

View File

@ -55,8 +55,8 @@ public class KNXHandlerFactory extends BaseThingHandlerFactory {
public Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, ThingUID thingUID,
ThingUID bridgeUID) {
if (THING_TYPE_IP_BRIDGE.equals(thingTypeUID)) {
ThingUID IPBridgeUID = getIPBridgeThingUID(thingTypeUID, thingUID, configuration);
return super.createThing(thingTypeUID, configuration, IPBridgeUID, null);
ThingUID ipBridgeUID = getIPBridgeThingUID(thingTypeUID, thingUID, configuration);
return super.createThing(thingTypeUID, configuration, ipBridgeUID, null);
}
if (THING_TYPE_SERIAL_BRIDGE.equals(thingTypeUID)) {
ThingUID serialBridgeUID = getSerialBridgeThingUID(thingTypeUID, thingUID, configuration);

View File

@ -19,7 +19,6 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.knx.internal.client.DeviceInspector;
@ -118,7 +117,7 @@ public abstract class AbstractKNXThingHandler extends BaseThingHandler implement
}
@Override
public void bridgeStatusChanged(@NonNull ThingStatusInfo bridgeStatusInfo) {
public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE) {
attachToClient();
} else if (bridgeStatusInfo.getStatus() == ThingStatus.OFFLINE) {
@ -175,7 +174,7 @@ public abstract class AbstractKNXThingHandler extends BaseThingHandler implement
}
DeviceConfig config = getConfigAs(DeviceConfig.class);
try {
if (config.getAddress() != null && !config.getAddress().isEmpty()) {
if (!config.getAddress().isEmpty()) {
updateStatus(ThingStatus.UNKNOWN);
address = new IndividualAddress(config.getAddress());
@ -200,12 +199,14 @@ public abstract class AbstractKNXThingHandler extends BaseThingHandler implement
}
protected void detachFromClient() {
if (pollingJob != null) {
pollingJob.cancel(true);
final var pollingJobSynced = pollingJob;
if (pollingJobSynced != null) {
pollingJobSynced.cancel(true);
pollingJob = null;
}
if (descriptionJob != null) {
descriptionJob.cancel(true);
final var descriptionJobSynced = descriptionJob;
if (descriptionJobSynced != null) {
descriptionJobSynced.cancel(true);
descriptionJob = null;
}
cancelReadFutures();

View File

@ -12,9 +12,12 @@
*/
package org.openhab.binding.knx.internal.handler;
import org.eclipse.jdt.annotation.NonNullByDefault;
/**
* @author Karel Goderis - Initial contribution
*/
@NonNullByDefault
public class DeviceConstants {
private DeviceConstants() {

View File

@ -105,7 +105,7 @@ public class DeviceThingHandler extends AbstractKNXThingHandler {
private void cancelChannelFutures() {
for (ScheduledFuture<?> future : channelFutures.values()) {
if (future != null && !future.isDone()) {
if (!future.isDone()) {
future.cancel(true);
}
}
@ -121,7 +121,7 @@ public class DeviceThingHandler extends AbstractKNXThingHandler {
@Override
protected void cancelReadFutures() {
for (ScheduledFuture<?> future : readFutures.values()) {
if (future != null && !future.isDone()) {
if (!future.isDone()) {
future.cancel(true);
}
}

View File

@ -72,11 +72,11 @@ public class IPBridgeThingHandler extends KNXBridgeBaseThingHandler {
boolean useNAT = false;
int ipConnectionType;
if (MODE_TUNNEL.equalsIgnoreCase(connectionTypeString)) {
useNAT = config.getUseNAT() != null ? config.getUseNAT() : false;
useNAT = config.getUseNAT();
ipConnectionType = CustomKNXNetworkLinkIP.TUNNELING;
} else if (MODE_ROUTER.equalsIgnoreCase(connectionTypeString)) {
useNAT = false;
if (ip == null || ip.isEmpty()) {
if (ip.isEmpty()) {
ip = KNXBindingConstants.DEFAULT_MULTICAST_IP;
}
ipConnectionType = CustomKNXNetworkLinkIP.ROUTING;
@ -86,13 +86,8 @@ public class IPBridgeThingHandler extends KNXBridgeBaseThingHandler {
connectionTypeString));
return;
}
if (ip == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"The 'ipAddress' of the gateway must be configured in 'TUNNEL' mode");
return;
}
if (config.getLocalIp() != null && !config.getLocalIp().isEmpty()) {
if (!config.getLocalIp().isEmpty()) {
localEndPoint = new InetSocketAddress(config.getLocalIp(), 0);
} else {
localEndPoint = new InetSocketAddress(networkAddressService.getPrimaryIpv4HostAddress(), 0);
@ -103,14 +98,18 @@ public class IPBridgeThingHandler extends KNXBridgeBaseThingHandler {
thing.getUID(), config.getResponseTimeout().intValue(), config.getReadingPause().intValue(),
config.getReadRetriesLimit().intValue(), getScheduler(), this);
client.initialize();
final var tmpClient = client;
if (tmpClient != null) {
tmpClient.initialize();
}
}
@Override
public void dispose() {
super.dispose();
if (client != null) {
client.dispose();
final var tmpClient = client;
if (tmpClient != null) {
tmpClient.dispose();
client = null;
}
}

View File

@ -17,7 +17,7 @@ import static org.junit.jupiter.api.Assertions.*;
import java.util.Collections;
import java.util.Set;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -26,17 +26,19 @@ import org.junit.jupiter.api.Test;
* @author Simon Kaufmann - initial contribution and API.
*
*/
@NonNullByDefault
public class KNXChannelTypeTest {
private KNXChannelType ct;
private KNXChannelType ct = new MyKNXChannelType("");
@BeforeEach
public void setup() {
ct = new MyKNXChannelType("");
}
@SuppressWarnings("null")
@Test
public void testParse_withDPT_multiple_withRead() {
public void testParseWithDptMultipleWithRead() {
ChannelConfiguration res = ct.parse("5.001:<1/3/22+0/3/22+<0/8/15");
assertEquals("5.001", res.getDPT());
@ -46,8 +48,9 @@ public class KNXChannelTypeTest {
assertEquals(2, res.getReadGAs().size());
}
@SuppressWarnings("null")
@Test
public void testParse_withDPT_multiple_withoutRead() {
public void testParseWithDptMultipleWithoutRead() {
ChannelConfiguration res = ct.parse("5.001:1/3/22+0/3/22+0/8/15");
assertEquals("5.001", res.getDPT());
@ -57,8 +60,9 @@ public class KNXChannelTypeTest {
assertEquals(0, res.getReadGAs().size());
}
@SuppressWarnings("null")
@Test
public void testParse_withoutDPT_single_withoutRead() {
public void testParseWithoutDptSingleWithoutRead() {
ChannelConfiguration res = ct.parse("1/3/22");
assertNull(res.getDPT());
@ -68,8 +72,9 @@ public class KNXChannelTypeTest {
assertEquals(0, res.getReadGAs().size());
}
@SuppressWarnings("null")
@Test
public void testParse_withoutDPT_single_witRead() {
public void testParseWithoutDptSingleWitRead() {
ChannelConfiguration res = ct.parse("<1/3/22");
assertNull(res.getDPT());
@ -79,16 +84,18 @@ public class KNXChannelTypeTest {
assertEquals(1, res.getReadGAs().size());
}
@SuppressWarnings("null")
@Test
public void testParse_twoLevel() {
public void testParseTwoLevel() {
ChannelConfiguration res = ct.parse("5.001:<3/1024+<4/1025");
assertEquals("3/1024", res.getMainGA().getGA());
assertEquals(2, res.getListenGAs().size());
assertEquals(2, res.getReadGAs().size());
}
@SuppressWarnings("null")
@Test
public void testParse_freeLevel() {
public void testParseFreeLevel() {
ChannelConfiguration res = ct.parse("5.001:<4610+<4611");
assertEquals("4610", res.getMainGA().getGA());
assertEquals(2, res.getListenGAs().size());
@ -101,12 +108,12 @@ public class KNXChannelTypeTest {
}
@Override
protected @NonNull Set<@NonNull String> getAllGAKeys() {
protected Set<String> getAllGAKeys() {
return Collections.emptySet();
}
@Override
protected @NonNull String getDefaultDPT(@NonNull String gaConfigKey) {
protected String getDefaultDPT(String gaConfigKey) {
return "";
}
}

View File

@ -14,6 +14,7 @@ package org.openhab.binding.knx.internal.dpt;
import static org.junit.jupiter.api.Assertions.*;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.openhab.core.library.types.DecimalType;
@ -22,10 +23,11 @@ import org.openhab.core.library.types.DecimalType;
* @author Simon Kaufmann - initial contribution and API
*
*/
@NonNullByDefault
public class KNXCoreTypeMapperTest {
@Test
public void testToDPTValue_trailingZeroesStrippedOff() {
public void testToDPTValueTrailingZeroesStrippedOff() {
assertEquals("3", new KNXCoreTypeMapper().toDPTValue(new DecimalType("3"), "17.001"));
assertEquals("3", new KNXCoreTypeMapper().toDPTValue(new DecimalType("3.0"), "17.001"));
}