Skip to content

Commit

Permalink
Null safety improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
bubner committed Sep 3, 2024
1 parent 50e7da4 commit e1b07fe
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ object Exceptions {
var stack = sw.toString()
if (e is NullPointerException) {
for (component in Storage.memory().unusableComponents) {
if (stack.contains(component)) {
if (stack.contains(component, ignoreCase = true)) {
// This error is caused by a null component, which is handled by NullSafety
// As such, we can swallow it from appearing on the Driver Station
// Logcat will still receive this log, so we can just early exit.
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/org/murraybridgebunyips/bunyipslib/NullSafety.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,24 @@ public static boolean assertNotNull(List<Object> objs) {
* Telemetry will be added to a BunyipsOpMode if it is running.
*
* @param componentName name of the subsystem
* @param excludeClassName the class name that should be attempted to be suppressed from caught errors (experimental)
* @param excludeClassName the class name that should be attempted to be suppressed from caught errors (experimental as this will
* suppress all class name instances, note that this suppression does not apply to Logcat so debugging is not affected)
* @param objs Objects to check for null
* @return Whether the component is safe to instantiate
*/
public static boolean assertComponentArgs(String componentName, String excludeClassName, Object... objs) {
// We do not check now for the presence of excludeClassName in unusableComponents, since the same classes
// can be used for different subsystems. We don't want every HoldableActuator to not work because one failed.
for (Object o : objs) {
if (o == null) {
if (BunyipsOpMode.isRunning()) {
BunyipsOpMode opMode = BunyipsOpMode.getInstance();
opMode.telemetry.addRetained("<font color='red'><b>! COM_FAULT</b></font>: %", componentName);
opMode.telemetry.log("<font color='yellow'><b>warning!</b> % was disabled due to a null assertion fault.</font>", componentName);
opMode.telemetry.addRetained("<font color='red'><b>! SUBSYSTEM FAULT</b></font>: %", componentName);
opMode.telemetry.log("<font color='yellow'><b>warning!</b> <i>%</i> failed a null self-check and was auto disabled.</font>", componentName);
}
Dbg.warn(getCallingUserCodeFunction(), "Null object passed to % failed assertion, adding to unusable components...", componentName);
if (!Storage.memory().unusableComponents.contains(excludeClassName))
Storage.memory().unusableComponents.add(excludeClassName);
Storage.memory().unusableComponents.add(excludeClassName);
return false;
}
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ abstract class RobotConfig {
}
for (error in Storage.memory().hardwareErrors) {
if (opMode is BunyipsOpMode) {
opMode.t.addRetained("<font color='red'><b>! MISSING_DEVICE</b></font>: $error")
opMode.t.addRetained("<font color='red'><b>! MISSING DEVICE</b></font>: $error")
opMode.t.addRetained("<font color='red'>error:</font> <i>$error</i> was not found in the current saved configuration.")
} else {
opMode.telemetry.addData("", "! MISSING_DEVICE: $error").setRetained(true)
opMode.telemetry.addData("", "! MISSING DEVICE: $error").setRetained(true)
opMode.telemetry.log().add("error: '$error' was not found in the current saved configuration.")
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/murraybridgebunyips/bunyipslib/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.HashSet;

/**
* Global filesystem and volatile storage utilities for robot operation.
Expand Down Expand Up @@ -88,7 +88,7 @@ public static class Memory {
*
* @see NullSafety
*/
public final List<String> unusableComponents = new ArrayList<>();
public final HashSet<String> unusableComponents = new HashSet<>();
private final HashMap<String, Object> store = new HashMap<>();
/**
* The last known player Alliance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@
* @since 1.0.0-pre
*/
public class MecanumDrive extends BunyipsSubsystem implements RoadRunnerDrive {
private final MecanumRoadRunnerDrive drive;
private final IMU imu;
private MecanumRoadRunnerDrive drive;
private IMU imu;

private final Watchdog benji;
private Watchdog benji;
private volatile boolean updates;

/**
Expand All @@ -69,7 +69,7 @@ public class MecanumDrive extends BunyipsSubsystem implements RoadRunnerDrive {
* @param br The back right motor.
*/
public MecanumDrive(DriveConstants constants, MecanumCoefficients coefficients, @Nullable IMU imu, DcMotor fl, DcMotor fr, DcMotor bl, DcMotor br) {
assertParamsNotNull(constants, coefficients, imu, fl, fr, bl, br);
if (!assertParamsNotNull(constants, coefficients, imu, fl, fr, bl, br)) return;
drive = new MecanumRoadRunnerDrive(opMode.telemetry, constants, coefficients, opMode.hardwareMap.voltageSensor, imu, fl, fr, bl, br);
benji = new Watchdog(() -> {
if (opMode.isStopRequested()) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@
* @since 1.0.0-pre
*/
public class TankDrive extends BunyipsSubsystem implements RoadRunnerDrive {
private final TankRoadRunnerDrive drive;
private TankRoadRunnerDrive drive;

private final Watchdog benji;
private Watchdog benji;
private volatile boolean updates;

/**
Expand All @@ -70,7 +70,7 @@ public class TankDrive extends BunyipsSubsystem implements RoadRunnerDrive {
* @param rightMotors The motors on the right side of the robot (e.g. {@code Arrays.asList(fr, br)})
*/
public TankDrive(DriveConstants constants, TankCoefficients coefficients, @Nullable IMU imu, List<DcMotor> leftMotors, List<DcMotor> rightMotors) {
assertParamsNotNull(constants, coefficients, imu, leftMotors, rightMotors);
if (!assertParamsNotNull(constants, coefficients, imu, leftMotors, rightMotors)) return;
drive = new TankRoadRunnerDrive(opMode.telemetry, constants, coefficients, opMode.hardwareMap.voltageSensor, imu, leftMotors, rightMotors);
benji = new Watchdog(() -> {
if (opMode.isStopRequested()) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public BlinkinLights(RevBlinkinLedDriver lights, RevBlinkinLedDriver.BlinkinPatt
this.defaultPattern = defaultPattern;
currentPattern = defaultPattern;

if (!assertParamsNotNull(lights)) return;
lights.setPattern(this.defaultPattern);
}

Expand Down

0 comments on commit e1b07fe

Please sign in to comment.