Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bluetooth.bluez] null annotations and checkstyle #13980

Merged
merged 1 commit into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,14 @@ public void dispose() {
try {
dev.getAdapter().removeDevice(dev.getRawDevice());
} catch (DBusException ex) {
if (ex.getMessage().contains("Does Not Exist")) {
// this happens when the underlying device has already been removed
// but we don't have a way to check if that is the case beforehand so
// we will just eat the error here.
} else {
String exceptionMessage = ex.getMessage();
if (exceptionMessage == null || exceptionMessage.contains("Does Not Exist")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you do not want to rather check that exceptionMessage is not null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because it falls thru immediate and prevents the second part equals to throw an NPE.
If you remove it, the compiler will throw a warning. And the debug format method handles the null fine.

logger.debug("Exception occurred when trying to remove inactive device '{}': {}", address,
ex.getMessage());
}
// this codeblock will only be hit when the underlying device has already
// been removed but we don't have a way to check if that is the case beforehand
// so we will just eat the error here.
} catch (RuntimeException ex) {
// try to catch any other exceptions
logger.debug("Exception occurred when trying to remove inactive device '{}': {}", address,
Expand Down Expand Up @@ -169,7 +169,6 @@ public boolean connect() {
// Have to double check because sometimes, exception but still worked
logger.debug("Got a timeout - but sometimes happen. Is Connected ? {}", dev.isConnected());
if (Boolean.FALSE.equals(dev.isConnected())) {

notifyListeners(BluetoothEventType.CONNECTION_STATE,
new BluetoothConnectionStatusNotification(ConnectionState.DISCONNECTED));
return false;
Expand All @@ -182,7 +181,6 @@ public boolean connect() {
} catch (Exception e) {
logger.warn("error occured while trying to connect", e);
}

} else {
logger.debug("Device was already connected");
// we might be stuck in another state atm so we need to trigger a connected in this case
Expand Down Expand Up @@ -278,9 +276,10 @@ private void ensureConnected() {
try {
c.startNotify();
} catch (DBusException e) {
if (e.getMessage().contains("Already notifying")) {
String exceptionMessage = e.getMessage();
if (exceptionMessage != null && exceptionMessage.contains("Already notifying")) {
return null;
} else if (e.getMessage().contains("In Progress")) {
} else if (exceptionMessage != null && exceptionMessage.contains("In Progress")) {
// let's retry in half a second
throw new RetryException(500, TimeUnit.MILLISECONDS);
} else {
Expand Down Expand Up @@ -524,9 +523,10 @@ public boolean isNotifying(BluetoothCharacteristic characteristic) {
try {
c.stopNotify();
} catch (DBusException e) {
if (e.getMessage().contains("Already notifying")) {
String exceptionMessage = e.getMessage();
if (exceptionMessage != null && exceptionMessage.contains("Already notifying")) {
return null;
} else if (e.getMessage().contains("In Progress")) {
} else if (exceptionMessage != null && exceptionMessage.contains("In Progress")) {
// let's retry in half a second
throw new RetryException(500, TimeUnit.MILLISECONDS);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public DeviceManagerWrapper(@Nullable DeviceManager deviceManager) {
this.deviceManager = deviceManager;
}

@SuppressWarnings("null")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to hide warnings?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, define necesary :-) It is needed because it is a synchronized with a good null check on deviceManager but the compile throws a false postive warning about possible NPE on this variable

So it is only needede to satisfy the compiler, on runtime it won't matter.

public synchronized Collection<BluetoothAdapter> scanForBluetoothAdapters() {
if (deviceManager != null) {
return deviceManager.scanForBluetoothAdapters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
@NonNullByDefault
public class ServiceDataEvent extends BlueZEvent {

final private Map<String, byte[]> data;
private final Map<String, byte[]> data;

public ServiceDataEvent(String dbusPath, Map<String, byte[]> data) {
super(dbusPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import static org.junit.jupiter.api.Assertions.*;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.openhab.binding.bluetooth.BluetoothAddress;
import org.openhab.binding.bluetooth.bluez.internal.events.BlueZEvent;
Expand All @@ -25,6 +25,7 @@
* @author Benjamin Lafois - Initial Contribution
* @author Connor Petty - Added additional test cases
*/
@NonNullByDefault
public class BlueZEventTest {

@Test
Expand Down Expand Up @@ -83,7 +84,7 @@ public DummyBlueZEvent(String dbusPath) {
}

@Override
public void dispatch(@NonNull BlueZEventListener listener) {
public void dispatch(BlueZEventListener listener) {
listener.onDBusBlueZEvent(this);
}
}
Expand Down