Skip to content

Commit

Permalink
Replace spotbugs by errorprone
Browse files Browse the repository at this point in the history
  • Loading branch information
bertrik committed Sep 6, 2024
1 parent 5ae3424 commit 0ff6d76
Show file tree
Hide file tree
Showing 16 changed files with 83 additions and 87 deletions.
9 changes: 7 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
alias libs.plugins.spotbugs
alias libs.plugins.errorprone
alias libs.plugins.versions
alias libs.plugins.dependencycheck
alias libs.plugins.git.version
Expand All @@ -18,7 +18,7 @@ dependencyCheck {
subprojects {
apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "com.github.spotbugs"
apply plugin: 'net.ltgt.errorprone'
apply plugin: 'com.github.ben-manes.versions'

java {
Expand All @@ -39,8 +39,13 @@ subprojects {

// Standard libraries added to all projects
dependencies {
errorprone("com.google.errorprone:error_prone_core:2.31.0")

annotationProcessor libs.errorprone.annotationprocessor
implementation libs.errorprone.annotations
implementation libs.slf4j.api

testAnnotationProcessor libs.errorprone.annotationprocessor
testImplementation libs.junit
testImplementation libs.mockito.core
}
Expand Down
45 changes: 22 additions & 23 deletions cayenne/src/main/java/nl/sikken/bertrik/cayenne/CayenneMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ public CayenneMessage() {
*/
public CayenneMessage(ECayennePayloadFormat format) {
switch (format) {
case DYNAMIC_SENSOR_PAYLOAD:
case PACKED_SENSOR_PAYLOAD:
break;
default:
throw new IllegalArgumentException("Payload format not supported: " + format);
case DYNAMIC_SENSOR_PAYLOAD:
case PACKED_SENSOR_PAYLOAD:
break;
default:
throw new IllegalArgumentException("Payload format not supported: " + format);
}
this.format = format;
}

/**
* Parses the byte array into a cayenne message.
*
*
* @param data the raw data
* @throws CayenneException in case of a parsing problem
*/
Expand All @@ -46,23 +46,23 @@ public void parse(byte[] data) throws CayenneException {
while (bb.hasRemaining()) {
CayenneItem item;
switch (format) {
case DYNAMIC_SENSOR_PAYLOAD:
item = CayenneItem.parse(bb);
break;
case PACKED_SENSOR_PAYLOAD:
item = CayenneItem.parsePacked(bb, channel);
channel++;
break;
default:
throw new IllegalStateException("Unsupported cayenne payload: " + format);
case DYNAMIC_SENSOR_PAYLOAD:
item = CayenneItem.parse(bb);
break;
case PACKED_SENSOR_PAYLOAD:
item = CayenneItem.parsePacked(bb, channel);
channel++;
break;
default:
throw new IllegalStateException("Unsupported cayenne payload: " + format);
}
add(item);
}
}

/**
* Adds a cayenne measurement item to the message.
*
*
* @param item the item to add
*/
public void add(CayenneItem item) {
Expand All @@ -71,7 +71,7 @@ public void add(CayenneItem item) {

/**
* Encodes the cayenne message into a byte array.
*
*
* @param maxSize the maximum size of the cayenne message
* @return the byte array.
* @throws CayenneException in case something went wrong during encoding (e.g.
Expand All @@ -86,17 +86,16 @@ public byte[] encode(int maxSize) throws CayenneException {
}

/**
* @return an immutable list of measurement items in the order it appears in the
* raw data
* Returns a list of measurement items in the order it appears in the raw data
*/
public List<CayenneItem> getItems() {
return List.copyOf(items);
}

/**
* Finds an item by type and channel.
*
* @param type the desired type
*
* @param type the desired type
* @param channel the desired channel
* @return the item, or null if it does not exist
*/
Expand All @@ -106,7 +105,7 @@ public CayenneItem find(ECayenneItem type, int channel) {

/**
* Finds an item by type.
*
*
* @param type the desired type
* @return the item, or null if it does not exist
*/
Expand All @@ -116,7 +115,7 @@ public CayenneItem ofType(ECayenneItem type) {

/**
* Finds an item by channel.
*
*
* @param channel the desired channel
* @return the item, or null if it does not exist
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package nl.sikken.bertrik.cayenne;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

import nl.sikken.bertrik.cayenne.formatter.FloatFormatter;
import nl.sikken.bertrik.cayenne.formatter.GpsFormatter;
import nl.sikken.bertrik.cayenne.formatter.IFormatter;
import nl.sikken.bertrik.cayenne.formatter.IntegerFormatter;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

/**
* Enumeration of possible Cayenne item types.
*/
Expand Down Expand Up @@ -53,7 +53,6 @@ public enum ECayenneItem {
*
* @param type the type code
* @return the enum, or null if not found
* @throws CayenneException
*/
public static ECayenneItem parse(int type) throws CayenneException {
ECayenneItem item = LOOKUP.get(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public enum ECayennePayloadFormat {
SENSOR_PERIOD_CONFIGURATION(13), //
SENSOR_ENABLE_CONFIGURATION(14); //

private int port;
private final int port;

ECayennePayloadFormat(int port) {
this.port = port;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.nio.ByteBuffer;
import java.util.Locale;
import java.util.stream.Stream;

/**
* Formatter for cayenne items which represent a GPS position.
Expand All @@ -16,13 +17,14 @@ public Double[] parse(ByteBuffer bb) {
double lat = LAT_LON_SCALE * getValue(bb, 3, true);
double lon = LAT_LON_SCALE * getValue(bb, 3, true);
double alt = ALT_SCALE * getValue(bb, 3, true);
return new Double[] { lat, lon, alt };
return new Double[]{lat, lon, alt};
}

@Override
public String[] format(Number[] values) {
return new String[] { String.format(Locale.ROOT, "%.4f", values[0]),
String.format(Locale.ROOT, "%.4f", values[1]), String.format(Locale.ROOT, "%.2f", values[2]) };
public String[] format(Number[] numbers) {
double[] values = Stream.of(numbers).mapToDouble(Number::doubleValue).toArray();
return new String[]{String.format(Locale.ROOT, "%.4f", values[0]),
String.format(Locale.ROOT, "%.4f", values[1]), String.format(Locale.ROOT, "%.2f", values[2])};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package nl.sikken.bertrik.cayenne.formatter;

import com.google.errorprone.annotations.Immutable;

import java.nio.ByteBuffer;

/**
* Interface for cayenne data structures that can be formatted as an array of
* strings.
*/
@Immutable
public interface IFormatter {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,6 @@ public void testDigitalInput() throws CayenneException {

/**
* Verifies encoding/decoding of a presence value (e.g. number of satellites)
*
* @throws CayenneException
*/
@Test
public void testPresence() throws CayenneException {
Expand Down
27 changes: 13 additions & 14 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ jackson = "2.17.2"
jersey = "3.1.8"
retrofit = "2.11.0"
slf4j = "2.0.16"
spotbugs = "4.8.6"
errorprone = "2.31.0"

[libraries]
slf4j-api = { module = "org.slf4j:slf4j-api", version.ref="slf4j" }
slf4j-reload4j = { module = "org.slf4j:slf4j-reload4j", version.ref="slf4j" }
errorprone_annotationprocessor = { module = "com.google.errorprone:error_prone_core:2.30.0", version.ref = "errorprone" }
errorprone_annotations = { module = "com.google.errorprone:error_prone_annotations:2.30.0", version.ref = "errorprone" }

jackson-databind = {module = "com.fasterxml.jackson.core:jackson-databind", version.ref="jackson" }
jackson-dataformat-yaml = {module = "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml", version.ref="jackson" }
slf4j-api = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" }
slf4j-reload4j = { module = "org.slf4j:slf4j-reload4j", version.ref = "slf4j" }

spotbugs = { module = "com.github.spotbugs:spotbugs", version.ref = "spotbugs" }
spotbugs-annotations = { module = "com.github.spotbugs:spotbugs-annotations", version.ref = "spotbugs" }
jackson-databind = { module = "com.fasterxml.jackson.core:jackson-databind", version.ref = "jackson" }
jackson-dataformat-yaml = { module = "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml", version.ref = "jackson" }

retrofit = { module = "com.squareup.retrofit2:retrofit", version.ref = "retrofit" }
retrofit-converter-jackson = { module = "com.squareup.retrofit2:converter-jackson", version.ref = "retrofit" }
Expand All @@ -26,22 +26,21 @@ paho-client-mqttv3 = "org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5"

jakarta-ws-rs-api = "jakarta.ws.rs:jakarta.ws.rs-api:4.0.0"

jersey-jetty = { module = "org.glassfish.jersey.containers:jersey-container-jetty-http", version.ref="jersey" }
jersey-server = { module = "org.glassfish.jersey.core:jersey-server", version.ref="jersey" }
jersey-inject = { module = "org.glassfish.jersey.inject:jersey-hk2", version.ref="jersey" }
jersey-jackson = { module = "org.glassfish.jersey.media:jersey-media-json-jackson", version.ref="jersey" }
jersey-jetty = { module = "org.glassfish.jersey.containers:jersey-container-jetty-http", version.ref = "jersey" }
jersey-server = { module = "org.glassfish.jersey.core:jersey-server", version.ref = "jersey" }
jersey-inject = { module = "org.glassfish.jersey.inject:jersey-hk2", version.ref = "jersey" }
jersey-jackson = { module = "org.glassfish.jersey.media:jersey-media-json-jackson", version.ref = "jersey" }

mockito-core = "org.mockito:mockito-core:5.12.0"
mockito-core = "org.mockito:mockito-core:5.13.0"

[bundles]
jackson = ["jackson-databind", "jackson-dataformat-yaml"]
retrofit = ["retrofit", "retrofit-converter-jackson", "retrofit-converter-scalars"]
jersey = ["jakarta-ws-rs-api", "jersey-jetty", "jersey-server", "jersey-inject", "jersey-jackson"]

[plugins]
errorprone = "net.ltgt.errorprone:4.0.1"
versions = "com.github.ben-manes.versions:0.51.0"
spotbugs = "com.github.spotbugs:5.0.14"
dependencycheck = "org.owasp.dependencycheck:8.4.3"
version-catalog-update = "nl.littlerobots.version-catalog-update:0.8.0"
git-version = "com.palantir.git-version:3.1.0"

2 changes: 0 additions & 2 deletions sensor-data-bridge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ dependencies {
// REST client
implementation libs.bundles.retrofit

compileOnly libs.spotbugs.annotations

testImplementation libs.retrofit.mock
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;

@SuppressWarnings("UnusedVariable")
public final class GeoLocationRequest {

@JsonProperty("considerIp")
Expand All @@ -19,6 +20,8 @@ public GeoLocationRequest(boolean considerIp) {
}

/**
* Adds an SSID.
*
* @param mac the 6-byte mac address of the access point
* @param signalStrength signal strength in dBm
* @param channel the WiFi channel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public static GeoLocationService create(GeoLocationConfig config) {
*
* @param request containing the WiFi AP information
* @return the response with the result
* @throws IOException
*/
public GeoLocationResponse geoLocate(GeoLocationRequest request) throws IOException {
Response<GeoLocationResponse> response = restClient.geoLocate(key, request).execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

@SuppressFBWarnings("URF_UNREAD_FIELD")
@JsonIgnoreProperties(ignoreUnknown = true)
public final class HeliumUplinkMessage {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
package nl.bertriksikken.loraforwarder;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import nl.bertriksikken.gls.GeoLocationRequest;
import nl.bertriksikken.gls.GeoLocationResponse;
import nl.bertriksikken.gls.GeoLocationService;
Expand All @@ -18,6 +8,15 @@
import nl.bertriksikken.ttn.enddevice.EndDevice;
import nl.bertriksikken.ttn.enddevice.EndDeviceRegistry;
import nl.bertriksikken.ttn.enddevice.Location;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* Handles commands and response from LoRaWAN devices.
Expand Down Expand Up @@ -47,20 +46,18 @@ public void stop() {

/**
* Test message on port 100: 00 9C 1C 12 F6 EB C0 D3 01 9C 1C 12 F6 F5 42 C9 01
*
* @param uplink
*/
void processResponse(TtnUplinkMessage uplink) {
ByteBuffer bb = ByteBuffer.wrap(uplink.getRawPayload()).order(ByteOrder.BIG_ENDIAN);

int cmd = bb.get() & 0xFF;
switch (cmd) {
case 0:
executor.execute(new CatchingRunnable(LOG, () -> handleWifiLocalisation(bb, uplink.getDevId())));
break;
default:
LOG.warn("Unhandled command {}", cmd);
break;
case 0:
executor.execute(new CatchingRunnable(LOG, () -> handleWifiLocalisation(bb, uplink.getDevId())));
break;
default:
LOG.warn("Unhandled command {}", cmd);
break;
}
}

Expand All @@ -80,7 +77,7 @@ void handleWifiLocalisation(ByteBuffer bb, String devId) {
EndDevice endDevice = endDeviceRegistry.buildEndDevice(devId);
Location location = new Location(response.getLatitude(), response.getLongitude());
endDevice.setLocation(EndDevice.LOCATION_USER, location);
endDeviceRegistry.updateEndDevice(endDevice, Arrays.asList("locations"));
endDeviceRegistry.updateEndDevice(endDevice, List.of("locations"));
}
} catch (IOException e) {
LOG.warn("Caught IOException", e);
Expand Down
Loading

0 comments on commit 0ff6d76

Please sign in to comment.