Require non null (#580)

* Replace null checks with Objects.requireNonNull()

* Use PMD rule instead of checkstyle rule
This commit is contained in:
Austin Shalit
2017-07-18 20:32:08 -07:00
committed by Peter Johnson
parent 06321b8e87
commit 74df3fac4e
16 changed files with 134 additions and 139 deletions

View File

@@ -25,4 +25,37 @@
<property name="checkAll" value="true"/>
</properties>
</rule>
<!-- Custom Rules -->
<rule name="UseRequireNonNull"
message="Use Objects.requireNonNull() instead of throwing a NullPointerException yourself."
language="java"
class="net.sourceforge.pmd.lang.rule.XPathRule">
<description>
Use Objects.requireNonNull() instead of throwing a NullPointerException yourself.
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//IfStatement[child::Expression//NullLiteral]/Statement//ThrowStatement/Expression/PrimaryExpression/PrimaryPrefix/AllocationExpression/ClassOrInterfaceType[@Image='NullPointerException']
]]>
</value>
</property>
</properties>
<priority>3</priority>
<example>
<![CDATA[
public class Example {
public Example(Object example) {
if (example == null) {
throw new NullPointerException();
}
}
}
]]>
</example>
</rule>
</ruleset>

View File

@@ -13,6 +13,7 @@ import edu.wpi.first.wpilibj.livewindow.LiveWindow;
import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable;
import edu.wpi.first.wpilibj.tables.ITable;
import static java.util.Objects.requireNonNull;
/**
* Handle operation of an analog accelerometer. The accelerometer reads acceleration directly
@@ -58,10 +59,9 @@ public class AnalogAccelerometer extends SensorBase implements PIDSource, LiveWi
* connected to
*/
public AnalogAccelerometer(AnalogInput channel) {
requireNonNull(channel, "Analog Channel given was null");
m_allocatedChannel = false;
if (channel == null) {
throw new NullPointerException("Analog Channel given was null");
}
m_analogChannel = channel;
initAccelerometer();
}

View File

@@ -14,6 +14,8 @@ import edu.wpi.first.wpilibj.interfaces.Gyro;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable;
import static java.util.Objects.requireNonNull;
/**
* Use a rate gyro to return the robots heading relative to a starting position. The Gyro class
* tracks the robots heading based on the starting position. As the robot rotates the new heading is
@@ -70,10 +72,9 @@ public class AnalogGyro extends GyroBase implements Gyro, PIDSource, LiveWindowS
* on-board channels 0-1.
*/
public AnalogGyro(AnalogInput channel) {
requireNonNull(channel, "AnalogInput supplied to Gyro constructor is null");
m_analog = channel;
if (m_analog == null) {
throw new NullPointerException("AnalogInput supplied to Gyro constructor is null");
}
initGyro();
calibrate();
}
@@ -102,10 +103,9 @@ public class AnalogGyro extends GyroBase implements Gyro, PIDSource, LiveWindowS
* @param offset Preset uncalibrated value to use as the gyro offset.
*/
public AnalogGyro(AnalogInput channel, int center, double offset) {
requireNonNull(channel, "AnalogInput supplied to Gyro constructor is null");
m_analog = channel;
if (m_analog == null) {
throw new NullPointerException("AnalogInput supplied to Gyro constructor is null");
}
initGyro();
AnalogGyroJNI.setAnalogGyroParameters(m_gyroHandle, kDefaultVoltsPerDegreePerSecond,
offset, center);

View File

@@ -11,6 +11,8 @@ import edu.wpi.first.wpilibj.hal.AnalogJNI;
import edu.wpi.first.wpilibj.hal.FRCNetComm.tResourceType;
import edu.wpi.first.wpilibj.hal.HAL;
import static java.util.Objects.requireNonNull;
/**
* Class to represent a specific output from an analog trigger. This class is used to get the
* current output value and also as a DigitalSource to provide routing of an output to digital
@@ -66,15 +68,11 @@ public class AnalogTriggerOutput extends DigitalSource {
* @param outputType An enum that specifies the output on the trigger to represent.
*/
public AnalogTriggerOutput(AnalogTrigger trigger, final AnalogTriggerType outputType) {
if (trigger == null) {
throw new NullPointerException("Analog Trigger given was null");
}
if (outputType == null) {
throw new NullPointerException("Analog Trigger Type given was null");
}
requireNonNull(trigger, "Analog Trigger given was null");
requireNonNull(outputType, "Analog Trigger Type given was null");
m_trigger = trigger;
m_outputType = outputType;
HAL.report(tResourceType.kResourceType_AnalogTriggerOutput,
trigger.getIndex(), outputType.value);
}
@@ -86,7 +84,6 @@ public class AnalogTriggerOutput extends DigitalSource {
if (m_interrupt != 0) {
cancelInterrupts();
}
}
/**

View File

@@ -17,6 +17,8 @@ import edu.wpi.first.wpilibj.hal.HAL;
import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable;
import edu.wpi.first.wpilibj.tables.ITable;
import static java.util.Objects.requireNonNull;
/**
* Class for counting the number of ticks on a digital input channel.
*
@@ -108,9 +110,8 @@ public class Counter extends SensorBase implements CounterBase, LiveWindowSendab
*/
public Counter(DigitalSource source) {
this();
if (source == null) {
throw new NullPointerException("Digital Source given was null");
}
requireNonNull(source, "Digital Source given was null");
setUpSource(source);
}
@@ -140,19 +141,16 @@ public class Counter extends SensorBase implements CounterBase, LiveWindowSendab
public Counter(EncodingType encodingType, DigitalSource upSource, DigitalSource downSource,
boolean inverted) {
this(Mode.kExternalDirection);
if (encodingType == null) {
throw new NullPointerException("Encoding type given was null");
}
requireNonNull(encodingType, "Encoding type given was null");
requireNonNull(upSource, "Up Source given was null");
requireNonNull(downSource, "Down Source given was null");
if (encodingType != EncodingType.k1X && encodingType != EncodingType.k2X) {
throw new RuntimeException("Counters only support 1X and 2X quadreature decoding!");
}
if (upSource == null) {
throw new NullPointerException("Up Source given was null");
}
setUpSource(upSource);
if (downSource == null) {
throw new NullPointerException("Down Source given was null");
}
setDownSource(downSource);
if (encodingType == EncodingType.k1X) {
@@ -176,9 +174,9 @@ public class Counter extends SensorBase implements CounterBase, LiveWindowSendab
*/
public Counter(AnalogTrigger trigger) {
this();
if (trigger == null) {
throw new NullPointerException("The Analog Trigger given was null");
}
requireNonNull(trigger, "The Analog Trigger given was null");
setUpSource(trigger.createOutput(AnalogTriggerType.kState));
}
@@ -238,12 +236,9 @@ public class Counter extends SensorBase implements CounterBase, LiveWindowSendab
* @param triggerType The analog trigger output that will trigger the counter.
*/
public void setUpSource(AnalogTrigger analogTrigger, AnalogTriggerType triggerType) {
if (analogTrigger == null) {
throw new NullPointerException("Analog Trigger given was null");
}
if (triggerType == null) {
throw new NullPointerException("Analog Trigger Type given was null");
}
requireNonNull(analogTrigger, "Analog Trigger given was null");
requireNonNull(triggerType, "Analog Trigger Type given was null");
setUpSource(analogTrigger.createOutput(triggerType));
m_allocatedUpSource = true;
}
@@ -292,9 +287,7 @@ public class Counter extends SensorBase implements CounterBase, LiveWindowSendab
* @param source the digital source to count
*/
public void setDownSource(DigitalSource source) {
if (source == null) {
throw new NullPointerException("The Digital Source given was null");
}
requireNonNull(source, "The Digital Source given was null");
if (m_downSource != null && m_allocatedDownSource) {
m_downSource.free();
@@ -312,12 +305,8 @@ public class Counter extends SensorBase implements CounterBase, LiveWindowSendab
* @param triggerType The analog trigger output that will trigger the counter.
*/
public void setDownSource(AnalogTrigger analogTrigger, AnalogTriggerType triggerType) {
if (analogTrigger == null) {
throw new NullPointerException("Analog Trigger given was null");
}
if (triggerType == null) {
throw new NullPointerException("Analog Trigger Type given was null");
}
requireNonNull(analogTrigger, "Analog Trigger given was null");
requireNonNull(triggerType, "Analog Trigger Type given was null");
setDownSource(analogTrigger.createOutput(triggerType));
m_allocatedDownSource = true;
@@ -331,9 +320,8 @@ public class Counter extends SensorBase implements CounterBase, LiveWindowSendab
* @param fallingEdge true to count the falling edge
*/
public void setDownSourceEdge(boolean risingEdge, boolean fallingEdge) {
if (m_downSource == null) {
throw new RuntimeException(" Down Source must be set before setting the edge!");
}
requireNonNull(m_downSource, "Down Source must be set before setting the edge!");
CounterJNI.setCounterDownSourceEdge(m_counter, risingEdge, fallingEdge);
}
@@ -539,9 +527,8 @@ public class Counter extends SensorBase implements CounterBase, LiveWindowSendab
* @param pidSource An enum to select the parameter.
*/
public void setPIDSourceType(PIDSourceType pidSource) {
if (pidSource == null) {
throw new NullPointerException("PID Source Parameter given was null");
} else if (pidSource != PIDSourceType.kDisplacement && pidSource != PIDSourceType.kRate) {
requireNonNull(pidSource, "PID Source Parameter given was null");
if (pidSource != PIDSourceType.kDisplacement && pidSource != PIDSourceType.kRate) {
throw new IllegalArgumentException("PID Source parameter was not valid type: " + pidSource);
}

View File

@@ -15,6 +15,8 @@ import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable;
import edu.wpi.first.wpilibj.tables.ITable;
import edu.wpi.first.wpilibj.util.AllocationException;
import static java.util.Objects.requireNonNull;
/**
* Class to read quadrature encoders.
*
@@ -131,12 +133,11 @@ public class Encoder extends SensorBase implements CounterBase, PIDSource, LiveW
*/
public Encoder(final int channelA, final int channelB, boolean reverseDirection,
final EncodingType encodingType) {
requireNonNull(encodingType, "Given encoding type was null");
m_allocatedA = true;
m_allocatedB = true;
m_allocatedI = false;
if (encodingType == null) {
throw new NullPointerException("Given encoding type was null");
}
m_aSource = new DigitalInput(channelA);
m_bSource = new DigitalInput(channelB);
initEncoder(reverseDirection, encodingType);
@@ -193,16 +194,13 @@ public class Encoder extends SensorBase implements CounterBase, PIDSource, LiveW
* if necessary so forward represents positive values.
*/
public Encoder(DigitalSource sourceA, DigitalSource sourceB, boolean reverseDirection) {
requireNonNull(sourceA, "Digital Source A was null");
requireNonNull(sourceB, "Digital Source B was null");
m_allocatedA = false;
m_allocatedB = false;
m_allocatedI = false;
if (sourceA == null) {
throw new NullPointerException("Digital Source A was null");
}
m_aSource = sourceA;
if (sourceB == null) {
throw new NullPointerException("Digital Source B was null");
}
m_bSource = sourceB;
initEncoder(reverseDirection, EncodingType.k4X);
}
@@ -241,19 +239,13 @@ public class Encoder extends SensorBase implements CounterBase, PIDSource, LiveW
*/
public Encoder(DigitalSource sourceA, DigitalSource sourceB, boolean reverseDirection,
final EncodingType encodingType) {
requireNonNull(sourceA, "Digital Source A was null");
requireNonNull(sourceB, "Digital Source B was null");
requireNonNull(encodingType, "Given encoding type was null");
m_allocatedA = false;
m_allocatedB = false;
m_allocatedI = false;
if (encodingType == null) {
throw new NullPointerException("Given encoding type was null");
}
if (sourceA == null) {
throw new NullPointerException("Digital Source A was null");
}
m_aSource = sourceA;
if (sourceB == null) {
throw new NullPointerException("Digital Source B was null");
}
m_aSource = sourceA;
m_bSource = sourceB;
initEncoder(reverseDirection, encodingType);
@@ -274,16 +266,12 @@ public class Encoder extends SensorBase implements CounterBase, PIDSource, LiveW
*/
public Encoder(DigitalSource sourceA, DigitalSource sourceB, DigitalSource indexSource,
boolean reverseDirection) {
requireNonNull(sourceA, "Digital Source A was null");
requireNonNull(sourceB, "Digital Source B was null");
m_allocatedA = false;
m_allocatedB = false;
m_allocatedI = false;
if (sourceA == null) {
throw new NullPointerException("Digital Source A was null");
}
m_aSource = sourceA;
if (sourceB == null) {
throw new NullPointerException("Digital Source B was null");
}
m_aSource = sourceA;
m_bSource = sourceB;
m_indexSource = indexSource;

View File

@@ -14,6 +14,8 @@ import edu.wpi.first.wpilibj.hal.HAL;
import edu.wpi.first.wpilibj.hal.I2CJNI;
import edu.wpi.first.wpilibj.util.BoundaryException;
import static java.util.Objects.requireNonNull;
/**
* I2C bus interface class.
*
@@ -201,13 +203,12 @@ public class I2C extends SensorBase {
* @return Transfer Aborted... false for success, true for aborted.
*/
public boolean read(int registerAddress, int count, byte[] buffer) {
requireNonNull(buffer, "Null return buffer was given");
if (count < 1) {
throw new BoundaryException("Value must be at least 1, " + count + " given");
}
if (buffer == null) {
throw new NullPointerException("Null return buffer was given");
}
byte[] registerAddressArray = new byte[1];
registerAddressArray[0] = (byte) registerAddress;
@@ -254,14 +255,11 @@ public class I2C extends SensorBase {
* @return Transfer Aborted... false for success, true for aborted.
*/
public boolean readOnly(byte[] buffer, int count) {
requireNonNull(buffer, "Null return buffer was given");
if (count < 1) {
throw new BoundaryException("Value must be at least 1, " + count + " given");
}
if (buffer == null) {
throw new NullPointerException("Null return buffer was given");
}
ByteBuffer dataReceivedBuffer = ByteBuffer.allocateDirect(count);
int retVal = I2CJNI.i2CRead(m_port, (byte) m_deviceAddress, dataReceivedBuffer,

View File

@@ -15,6 +15,8 @@ import edu.wpi.first.wpilibj.networktables.NetworkTable;
import edu.wpi.first.wpilibj.tables.ITable;
import edu.wpi.first.wpilibj.tables.ITableListener;
import static java.util.Objects.requireNonNull;
/**
* The preferences class provides a relatively simple way to save important values to the roboRIO to
* access the next time the roboRIO is booted.
@@ -95,9 +97,8 @@ public class Preferences {
* @throws NullPointerException if value is null
*/
public void putString(String key, String value) {
if (value == null) {
throw new NullPointerException("Value is null");
}
requireNonNull(value, "Provided value was null");
m_table.putString(key, value);
m_table.setPersistent(key);
}

View File

@@ -318,17 +318,13 @@ public class Relay extends SensorBase implements MotorSafety, LiveWindowSendable
* @param direction The direction for the relay to operate in
*/
public void setDirection(Direction direction) {
if (direction == null) {
throw new NullPointerException("Null Direction was given");
}
requireNonNull(direction, "Null Direction was given");
if (m_direction == direction) {
return;
}
free();
m_direction = direction;
initRelay();
}

View File

@@ -109,10 +109,9 @@ public class RobotDrive implements MotorSafety {
* @param rightMotor the right SpeedController object used to drive the robot.
*/
public RobotDrive(SpeedController leftMotor, SpeedController rightMotor) {
if (leftMotor == null || rightMotor == null) {
m_rearLeftMotor = m_rearRightMotor = null;
throw new NullPointerException("Null motor provided");
}
requireNonNull(leftMotor, "Provided left motor was null");
requireNonNull(rightMotor, "Provided right motor was null");
m_frontLeftMotor = null;
m_rearLeftMotor = leftMotor;
m_frontRightMotor = null;
@@ -204,9 +203,9 @@ public class RobotDrive implements MotorSafety {
* @param rightStick The joystick to control the right side of the robot.
*/
public void tankDrive(GenericHID leftStick, GenericHID rightStick) {
if (leftStick == null || rightStick == null) {
throw new NullPointerException("Null HID provided");
}
requireNonNull(leftStick, "Provided left stick was null");
requireNonNull(rightStick, "Provided right stick was null");
tankDrive(leftStick.getY(), rightStick.getY(), true);
}
@@ -219,9 +218,9 @@ public class RobotDrive implements MotorSafety {
* @param squaredInputs Setting this parameter to true decreases the sensitivity at lower speeds
*/
public void tankDrive(GenericHID leftStick, GenericHID rightStick, boolean squaredInputs) {
if (leftStick == null || rightStick == null) {
throw new NullPointerException("Null HID provided");
}
requireNonNull(leftStick, "Provided left stick was null");
requireNonNull(rightStick, "Provided right stick was null");
tankDrive(leftStick.getY(), rightStick.getY(), squaredInputs);
}
@@ -236,9 +235,9 @@ public class RobotDrive implements MotorSafety {
*/
public void tankDrive(GenericHID leftStick, final int leftAxis, GenericHID rightStick,
final int rightAxis) {
if (leftStick == null || rightStick == null) {
throw new NullPointerException("Null HID provided");
}
requireNonNull(leftStick, "Provided left stick was null");
requireNonNull(rightStick, "Provided right stick was null");
tankDrive(leftStick.getRawAxis(leftAxis), rightStick.getRawAxis(rightAxis), true);
}
@@ -254,9 +253,9 @@ public class RobotDrive implements MotorSafety {
*/
public void tankDrive(GenericHID leftStick, final int leftAxis, GenericHID rightStick,
final int rightAxis, boolean squaredInputs) {
if (leftStick == null || rightStick == null) {
throw new NullPointerException("Null HID provided");
}
requireNonNull(leftStick, "Provided left stick was null");
requireNonNull(rightStick, "Provided right stick was null");
tankDrive(leftStick.getRawAxis(leftAxis), rightStick.getRawAxis(rightAxis), squaredInputs);
}
@@ -545,9 +544,8 @@ public class RobotDrive implements MotorSafety {
* @param rightOutput The speed to send to the right side of the robot.
*/
public void setLeftRightMotorOutputs(double leftOutput, double rightOutput) {
if (m_rearLeftMotor == null || m_rearRightMotor == null) {
throw new NullPointerException("Null motor provided");
}
requireNonNull(m_rearLeftMotor, "Provided left motor was null");
requireNonNull(m_rearRightMotor, "Provided right motor was null");
if (m_frontLeftMotor != null) {
m_frontLeftMotor.set(limit(leftOutput) * m_maxOutput);

View File

@@ -13,6 +13,8 @@ import edu.wpi.first.wpilibj.livewindow.LiveWindow;
import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable;
import edu.wpi.first.wpilibj.tables.ITable;
import static java.util.Objects.requireNonNull;
/**
* Ultrasonic rangefinder class. The Ultrasonic rangefinder measures absolute distance based on the
* round-trip time of a ping generated by the controller. These sensors use two transducers, a
@@ -160,9 +162,9 @@ public class Ultrasonic extends SensorBase implements PIDSource, LiveWindowSenda
* @param units The units returned in either kInches or kMilliMeters
*/
public Ultrasonic(DigitalOutput pingChannel, DigitalInput echoChannel, Unit units) {
if (pingChannel == null || echoChannel == null) {
throw new NullPointerException("Null Channel Provided");
}
requireNonNull(pingChannel, "Provided ping channel was null");
requireNonNull(echoChannel, "Provided echo channel was null");
m_allocatedChannels = false;
m_pingChannel = pingChannel;
m_echoChannel = echoChannel;

View File

@@ -16,6 +16,8 @@ import edu.wpi.first.wpilibj.tables.ITable;
import edu.wpi.first.wpilibj.tables.ITableListener;
import edu.wpi.first.wpilibj.util.BoundaryException;
import static java.util.Objects.requireNonNull;
/**
* Class implements a PID Control Loop.
*
@@ -116,9 +118,8 @@ public class PIDController implements PIDInterface, LiveWindowSendable, Controll
private PIDController m_controller;
public PIDTask(PIDController controller) {
if (controller == null) {
throw new NullPointerException("Given PIDController was null");
}
requireNonNull(controller, "Given PIDController was null");
m_controller = controller;
}
@@ -143,13 +144,8 @@ public class PIDController implements PIDInterface, LiveWindowSendable, Controll
@SuppressWarnings("ParameterName")
public PIDController(double Kp, double Ki, double Kd, double Kf, PIDSource source,
PIDOutput output, double period) {
if (source == null) {
throw new NullPointerException("Null PIDSource was given");
}
if (output == null) {
throw new NullPointerException("Null PIDOutput was given");
}
requireNonNull(source, "Null PIDSource was given");
requireNonNull(output, "Null PIDOutput was given");
m_controlLoop = new java.util.Timer();
m_setpointTimer = new Timer();

View File

@@ -10,6 +10,8 @@ package edu.wpi.first.wpilibj.command;
import java.util.Enumeration;
import java.util.Vector;
import static java.util.Objects.requireNonNull;
/**
* A {@link CommandGroup} is a list of commands which are executed in sequence.
*
@@ -153,10 +155,8 @@ public class CommandGroup extends Command {
* @throws IllegalArgumentException if command is null
*/
public final synchronized void addParallel(Command command) {
requireNonNull(command, "Provided command was null");
validate("Can not add new command to command group");
if (command == null) {
throw new NullPointerException("Given null command");
}
command.setParent(this);
@@ -193,13 +193,11 @@ public class CommandGroup extends Command {
* @throws IllegalArgumentException if command is null
*/
public final synchronized void addParallel(Command command, double timeout) {
validate("Can not add new command to command group");
if (command == null) {
throw new NullPointerException("Given null command");
}
requireNonNull(command, "Provided command was null");
if (timeout < 0) {
throw new IllegalArgumentException("Can not be given a negative timeout");
}
validate("Can not add new command to command group");
command.setParent(this);

View File

@@ -13,6 +13,8 @@ import edu.wpi.first.wpilibj.Sendable;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.tables.ITable;
import static java.util.Objects.requireNonNull;
/**
* The {@link SendableChooser} class is a useful tool for presenting a selection of options to the
* {@link SmartDashboard}.
@@ -75,9 +77,8 @@ public class SendableChooser<V> implements Sendable {
* @param object the option
*/
public void addDefault(String name, V object) {
if (name == null) {
throw new NullPointerException("Name cannot be null");
}
requireNonNull(name, "Provided name was null");
m_defaultChoice = name;
if (m_table != null) {
m_table.putString(DEFAULT, m_defaultChoice);

View File

@@ -170,6 +170,7 @@ public abstract class MotorEncoderFixture<T extends SpeedController> implements
* deallocated.
*/
@Override
@SuppressWarnings("Regexp")
public boolean teardown() {
String type;
if (m_motor != null) {

View File

@@ -22,6 +22,7 @@ import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import java.util.regex.Pattern;
@@ -43,9 +44,7 @@ public class TestSuite extends AbstractTestSuite {
// Sets up the logging output
final InputStream inputStream = TestSuite.class.getResourceAsStream("/logging.properties");
try {
if (inputStream == null) {
throw new NullPointerException("./logging.properties was not loaded");
}
Objects.requireNonNull(inputStream, "./logging.properties was not loaded");
LogManager.getLogManager().readConfiguration(inputStream);
Logger.getAnonymousLogger().info("Loaded");
} catch (final IOException | NullPointerException ex) {