Skip to content

Commit

Permalink
[#165] fix java code warnings and add nullness annotations (#166)
Browse files Browse the repository at this point in the history
* [#165] Add spotbugs/findbugs, and add package-info.java to key java packages

* [#165] Add @Nonnull/@nullable to all relevant methods - there may be unresolved nullness issues...

* [#165] Unused imports

* [#165] Add @Nonnull/@nullable to all relevant methods, esp in generated code

* [#165] Minor cleanup
  • Loading branch information
susanw1 authored Sep 19, 2024
1 parent 71cb7aa commit cf445a8
Show file tree
Hide file tree
Showing 108 changed files with 692 additions and 216 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.zscript.javaclient.commandbuilder;

import javax.annotation.Nonnull;
import java.nio.charset.StandardCharsets;
import java.util.BitSet;
import java.util.EnumSet;
Expand All @@ -23,6 +24,7 @@ public class Utils {
* @param <E> the enum type itself
* @return a Set containing the corresponding values defined in the enum
*/
@Nonnull
public static <E extends Enum<E>> EnumSet<E> bitsetToEnumSet(int bitFields, Class<E> enumClass) {
final E[] enumValues = enumClass.getEnumConstants();
return BitSet.valueOf(new long[] { bitFields }).stream()
Expand Down Expand Up @@ -51,6 +53,7 @@ public static <E extends Enum<E>> int checkInEnumRange(int value, Class<E> enumC
return value;
}

@Nonnull
public static byte[] formatField(byte f, int value) {
if (value >= 0x10000 || value < 0) {
throw new IllegalStateException("Command fields must be uint16s: " + value);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package net.zscript.javaclient.commandbuilder.commandnodes;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.zscript.javaclient.commandbuilder.commandnodes;

import javax.annotation.Nonnull;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
Expand Down Expand Up @@ -113,6 +114,7 @@ CommandSequenceNode optimize() {

public abstract List<CommandSequenceNode> getChildren();

@Nonnull
@Override
public Iterator<ZscriptCommandNode<?>> iterator() {
if (this instanceof ZscriptCommandNode) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package net.zscript.javaclient.commandbuilder.commandnodes;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import net.zscript.model.components.Zchars;

public class OrSequenceNode extends CommandSequenceNode {
final CommandSequenceNode before;
Expand All @@ -27,7 +22,7 @@ protected boolean canFail() {
boolean isCommand() {
return false;
}

@Override
CommandSequenceNode optimize() {
if (!before.canFail()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package net.zscript.javaclient.commandbuilder.commandnodes;

import java.util.NoSuchElementException;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import net.zscript.javaclient.commandbuilder.Respondable;
import net.zscript.javaclient.commandbuilder.ZscriptResponse;

public class ResponseCaptor<T extends ZscriptResponse> {
@Nonnull
public static <T extends ZscriptResponse> ResponseCaptor<T> create() {
return new ResponseCaptor<>();
}
Expand All @@ -19,6 +21,7 @@ public void setSource(Respondable<T> source) {
this.source = source;
}

@Nullable
public Respondable<T> getSource() {
return source;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.zscript.javaclient.commandbuilder.commandnodes;

import javax.annotation.Nonnull;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.BitSet;
Expand Down Expand Up @@ -95,6 +96,7 @@ protected final void setRequiredFields(byte[] keys) {
}
}

@Nonnull
public ZscriptCommandBuilder<T> capture(ResponseCaptor<T> captor) {
this.captor = captor;
return this;
Expand Down Expand Up @@ -129,5 +131,6 @@ protected void failIfInvalid() {
* @return the command that has been built
* @throws ZscriptMissingFieldException if builder doesn't yet pass validation
*/
@Nonnull
public abstract ZscriptCommandNode<T> build();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.zscript.javaclient.commandbuilder.commandnodes;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -29,7 +31,7 @@ protected ZscriptCommandNode() {
this(null, Collections.emptyList(), Collections.emptyMap());
}

protected ZscriptCommandNode(ResponseCaptor<T> captor, List<BigField> bigFields, Map<Byte, Integer> fields) {
protected ZscriptCommandNode(@Nullable ResponseCaptor<T> captor, List<BigField> bigFields, Map<Byte, Integer> fields) {
this.captor = captor;
this.bigFields = bigFields;
this.fields = fields;
Expand All @@ -38,23 +40,29 @@ protected ZscriptCommandNode(ResponseCaptor<T> captor, List<BigField> bigFields,
}
}

@Nullable
public ResponseCaptor<T> getCaptor() {
return captor;
}

@Nonnull
public abstract T parseResponse(ZscriptExpression response);

@Nonnull
public abstract Class<T> getResponseType();

@Nonnull
public List<CommandSequenceNode> getChildren() {
return Collections.emptyList();
}

@Nonnull
public ZscriptFieldSet asFieldSet() {
return ZscriptFieldSet.fromMap(bigFields.stream().map(BigField::getData).collect(Collectors.toList()),
bigFields.stream().map(BigField::isString).collect(Collectors.toList()), fields);
}

@Nonnull
@Override
public String asString() {
ByteString.ByteStringBuilder b = ByteString.builder();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@ParametersAreNonnullByDefault
package net.zscript.javaclient.commandbuilder.commandnodes;

import javax.annotation.ParametersAreNonnullByDefault;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.zscript.javaclient.commandbuilder.defaultCommands;

import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.Map;

Expand Down Expand Up @@ -60,11 +61,13 @@ protected boolean canFail() {
return false;
}

@Nonnull
@Override
public DefaultResponse parseResponse(ZscriptExpression response) {
return new DefaultResponse(response);
}

@Nonnull
@Override
public Class<DefaultResponse> getResponseType() {
return DefaultResponse.class;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.zscript.javaclient.commandbuilder.defaultCommands;

import javax.annotation.Nonnull;

import net.zscript.javaclient.commandbuilder.commandnodes.CommandSequenceNode;
import net.zscript.javaclient.commandbuilder.commandnodes.ZscriptCommandNode;
import net.zscript.tokenizer.ZscriptExpression;
Expand Down Expand Up @@ -51,11 +53,13 @@ protected boolean canFail() {
return false;
}

@Nonnull
@Override
public DefaultResponse parseResponse(ZscriptExpression response) {
return new DefaultResponse(response);
}

@Nonnull
@Override
public Class<DefaultResponse> getResponseType() {
return DefaultResponse.class;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.zscript.javaclient.commandbuilder.defaultCommands;

import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.Map;

Expand Down Expand Up @@ -55,11 +56,13 @@ public CommandSequenceNode onFail(CommandSequenceNode next) {
return next;
}

@Nonnull
@Override
public DefaultResponse parseResponse(ZscriptExpression response) {
return new DefaultResponse(response);
}

@Nonnull
@Override
public Class<DefaultResponse> getResponseType() {
return DefaultResponse.class;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.zscript.javaclient.commandbuilder.defaultCommands;

import javax.annotation.Nonnull;

import net.zscript.javaclient.commandbuilder.commandnodes.ZscriptCommandBuilder;
import net.zscript.javaclient.commandbuilder.commandnodes.ZscriptCommandNode;
import net.zscript.tokenizer.ZscriptExpression;
Expand Down Expand Up @@ -37,14 +39,17 @@ public ZscriptCommandBuilder<DefaultResponse> addBigField(String data) {
return super.addBigField(data);
}

@Nonnull
@Override
public ZscriptCommandNode<DefaultResponse> build() {
return new ZscriptCommandNode<DefaultResponse>() {
return new ZscriptCommandNode<>() {
@Nonnull
@Override
public DefaultResponse parseResponse(ZscriptExpression response) {
return new DefaultResponse(response);
}

@Nonnull
@Override
public Class<DefaultResponse> getResponseType() {
return DefaultResponse.class;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@ParametersAreNonnullByDefault
package net.zscript.javaclient.commandbuilder.defaultCommands;

import javax.annotation.ParametersAreNonnullByDefault;
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package net.zscript.javaclient.commandbuilder.notifications;

import javax.annotation.Nonnull;
import java.util.List;

import net.zscript.javaclient.commandbuilder.ZscriptResponse;

public abstract class NotificationHandle {
@Nonnull
public abstract <T extends ZscriptResponse> NotificationSection<T> getSection(NotificationSectionId<T> response);

@Nonnull
public abstract List<NotificationSection<?>> getSections();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@ParametersAreNonnullByDefault
package net.zscript.javaclient.commandbuilder.notifications;

import javax.annotation.ParametersAreNonnullByDefault;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@ParametersAreNonnullByDefault
package net.zscript.javaclient.commandbuilder;

import javax.annotation.ParametersAreNonnullByDefault;
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.zscript.javaclient.commandbuilder;

import javax.annotation.Nonnull;

import net.zscript.javaclient.commandbuilder.commandnodes.ZscriptCommandBuilder;
import net.zscript.javaclient.commandbuilder.commandnodes.ZscriptCommandNode;
import net.zscript.tokenizer.ZscriptExpression;
Expand All @@ -14,11 +16,13 @@ public static DemoActivateCommandBuilder builder() {
return new DemoActivateCommandBuilder();
}

@Nonnull
@Override
public DemoActivateCommandResponse parseResponse(ZscriptExpression resp) {
public DemoActivateCommandResponse parseResponse(@Nonnull ZscriptExpression resp) {
return new DemoActivateCommandResponse(resp, new byte[] {}, resp.getField('A').orElse(0) == 1);
}

@Nonnull
@Override
public Class<DemoActivateCommandResponse> getResponseType() {
return DemoActivateCommandResponse.class;
Expand All @@ -35,6 +39,7 @@ public DemoActivateCommandBuilder() {
setField('Z', 2);
}

@Nonnull
@Override
public DemoActivateCommand build() {
return new DemoActivateCommand(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.zscript.javaclient.commandbuilder.commandNodes;

import javax.annotation.Nonnull;

import static java.nio.charset.StandardCharsets.ISO_8859_1;

import net.zscript.javaclient.commandbuilder.commandnodes.ZscriptCommandBuilder;
Expand All @@ -26,22 +28,23 @@ public ZscriptCommandBuilder<DemoCapabilitiesCommandResponse> setField(char key,
return super.setField(key, value);
}

public ZscriptCommandBuilder<DemoCapabilitiesCommandResponse> addBigField(byte[] data) {
public ZscriptCommandBuilder<DemoCapabilitiesCommandResponse> addBigField(@Nonnull byte[] data) {
return super.addBigField(data);
}

public ZscriptCommandBuilder<DemoCapabilitiesCommandResponse> addBigField(byte[] data, boolean asString) {
public ZscriptCommandBuilder<DemoCapabilitiesCommandResponse> addBigField(@Nonnull byte[] data, boolean asString) {
return super.addBigField(data, asString);
}

public ZscriptCommandBuilder<DemoCapabilitiesCommandResponse> addBigFieldAsSmallest(byte[] data) {
public ZscriptCommandBuilder<DemoCapabilitiesCommandResponse> addBigFieldAsSmallest(@Nonnull byte[] data) {
return super.addBigField(data);
}

public ZscriptCommandBuilder<DemoCapabilitiesCommandResponse> addBigField(String data) {
public ZscriptCommandBuilder<DemoCapabilitiesCommandResponse> addBigField(@Nonnull String data) {
return super.addBigField(data);
}

@Nonnull
@Override
public ZscriptCommandNode<DemoCapabilitiesCommandResponse> build() {
return new DemoCapabilities(this);
Expand All @@ -57,11 +60,13 @@ public static DemoCapabilitiesCommandBuilder builder() {
return new DemoCapabilitiesCommandBuilder();
}

@Nonnull
@Override
public DemoCapabilitiesCommandResponse parseResponse(ZscriptExpression resp) {
public DemoCapabilitiesCommandResponse parseResponse(@Nonnull ZscriptExpression resp) {
return new DemoCapabilitiesCommandResponse(resp, new byte[] {}, resp.getField('V').getAsInt(), new String(resp.getBigFieldData(), ISO_8859_1));
}

@Nonnull
@Override
public Class<DemoCapabilitiesCommandResponse> getResponseType() {
return DemoCapabilitiesCommandResponse.class;
Expand Down
Loading

0 comments on commit cf445a8

Please sign in to comment.