diff --git a/styleguide/pmd-ruleset.xml b/styleguide/pmd-ruleset.xml new file mode 100644 index 0000000000..92d35dcb02 --- /dev/null +++ b/styleguide/pmd-ruleset.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + diff --git a/wpilibj/athena.gradle b/wpilibj/athena.gradle index bad1d4d681..1fabe96b57 100644 --- a/wpilibj/athena.gradle +++ b/wpilibj/athena.gradle @@ -99,6 +99,10 @@ jar { from sourceSets.shared.output } +pmd { + sourceSets = [sourceSets.shared, sourceSets.athena] +} + task wpilibjSources(type: Jar, dependsOn: classes) { description = 'Creates the sources jar for the maven publishing routine' group = 'WPILib' diff --git a/wpilibj/build.gradle b/wpilibj/build.gradle index 5b8257a5f7..b71b385602 100644 --- a/wpilibj/build.gradle +++ b/wpilibj/build.gradle @@ -1,6 +1,7 @@ apply plugin: 'java' apply plugin: 'maven-publish' apply plugin: 'net.ltgt.errorprone' +apply plugin: 'pmd' configurations.errorprone { resolutionStrategy.force 'com.google.errorprone:error_prone_core:2.0.9' @@ -52,6 +53,13 @@ clean { delete wpilibVersionFile } +pmd { + consoleOutput = true + reportsDir = file("$project.buildDir/reports/pmd") + ruleSetFiles = files(new File(rootDir, "styleguide/pmd-ruleset.xml")) + ruleSets = [] +} + sourceSets { shared } diff --git a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/CameraServer.java b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/CameraServer.java index 6ce2aed39b..773d08eaab 100644 --- a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/CameraServer.java +++ b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/CameraServer.java @@ -85,7 +85,7 @@ public class CameraServer { return m_tables.get(source); } - @SuppressWarnings("JavadocMethod") + @SuppressWarnings({"JavadocMethod", "PMD.AvoidUsingHardCodedIP"}) private synchronized void updateStreamValues() { // Over all the sinks... for (VideoSink i : m_sinks.values()) { diff --git a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/DoubleSolenoid.java b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/DoubleSolenoid.java index 0e7dc11c92..e85ff5c7a0 100644 --- a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/DoubleSolenoid.java +++ b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/DoubleSolenoid.java @@ -153,7 +153,7 @@ public class DoubleSolenoid extends SolenoidBase implements LiveWindowSendable { */ public boolean isFwdSolenoidBlackListed() { int blackList = getPCMSolenoidBlackList(); - return ((blackList & m_forwardMask) != 0); + return (blackList & m_forwardMask) != 0; } /** @@ -165,7 +165,7 @@ public class DoubleSolenoid extends SolenoidBase implements LiveWindowSendable { */ public boolean isRevSolenoidBlackListed() { int blackList = getPCMSolenoidBlackList(); - return ((blackList & m_reverseMask) != 0); + return (blackList & m_reverseMask) != 0; } /* diff --git a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/DriverStation.java b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/DriverStation.java index c2ea86e5f8..8e8955afa2 100644 --- a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/DriverStation.java +++ b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/DriverStation.java @@ -190,7 +190,7 @@ public class DriverStation implements RobotState.Interface { if (traces.length > 3) { locString = traces[3].toString(); } else { - locString = new String(); + locString = ""; } boolean haveLoc = false; String traceString = " at "; @@ -854,7 +854,7 @@ public class DriverStation implements RobotState.Interface { private void updateControlWord(boolean force) { long now = System.currentTimeMillis(); synchronized (m_controlWordMutex) { - if ((now - m_lastControlWordUpdate > 50) || force) { + if (now - m_lastControlWordUpdate > 50 || force) { HAL.getControlWord(m_controlWordCache); m_lastControlWordUpdate = now; } diff --git a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/InterruptableSensorBase.java b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/InterruptableSensorBase.java index ec47d1a059..5da5836ca5 100644 --- a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/InterruptableSensorBase.java +++ b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/InterruptableSensorBase.java @@ -75,7 +75,7 @@ public abstract class InterruptableSensorBase extends SensorBase { allocateInterrupts(false); - assert (m_interrupt != 0); + assert m_interrupt != 0; InterruptJNI.requestInterrupts(m_interrupt, getPortHandleForRouting(), getAnalogTriggerTypeForRouting()); @@ -96,7 +96,7 @@ public abstract class InterruptableSensorBase extends SensorBase { allocateInterrupts(true); - assert (m_interrupt != 0); + assert m_interrupt != 0; InterruptJNI.requestInterrupts(m_interrupt, getPortHandleForRouting(), getAnalogTriggerTypeForRouting()); diff --git a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/IterativeRobot.java b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/IterativeRobot.java index a3e59cb904..b917c8091f 100644 --- a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/IterativeRobot.java +++ b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/IterativeRobot.java @@ -185,6 +185,7 @@ public class IterativeRobot extends RobotBase { *

Users should override this method for initialization code which will be called each time the * robot enters test mode. */ + @SuppressWarnings("PMD.JUnit4TestShouldUseTestAnnotation") public void testInit() { System.out.println("Default IterativeRobot.testInit() method... Overload me!"); } @@ -283,6 +284,7 @@ public class IterativeRobot extends RobotBase { * disconnected. For most use cases the variable timing will not be an issue. If your code does * require guaranteed fixed periodic timing, consider using Notifier or PIDController instead. */ + @SuppressWarnings("PMD.JUnit4TestShouldUseTestAnnotation") public void testPeriodic() { if (m_tmpFirstRun) { System.out.println("Default IterativeRobot.testPeriodic() method... Overload me!"); diff --git a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/RobotDrive.java b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/RobotDrive.java index 4d76c32337..fc729bca54 100644 --- a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/RobotDrive.java +++ b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/RobotDrive.java @@ -282,12 +282,12 @@ public class RobotDrive implements MotorSafety { rightValue = limit(rightValue); if (squaredInputs) { if (leftValue >= 0.0) { - leftValue = (leftValue * leftValue); + leftValue = leftValue * leftValue; } else { leftValue = -(leftValue * leftValue); } if (rightValue >= 0.0) { - rightValue = (rightValue * rightValue); + rightValue = rightValue * rightValue; } else { rightValue = -(rightValue * rightValue); } @@ -395,12 +395,12 @@ public class RobotDrive implements MotorSafety { // square the inputs (while preserving the sign) to increase fine control // while permitting full power if (moveValue >= 0.0) { - moveValue = (moveValue * moveValue); + moveValue = moveValue * moveValue; } else { moveValue = -(moveValue * moveValue); } if (rotateValue >= 0.0) { - rotateValue = (rotateValue * rotateValue); + rotateValue = rotateValue * rotateValue; } else { rotateValue = -(rotateValue * rotateValue); } diff --git a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/SampleRobot.java b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/SampleRobot.java index dd971a033c..e7fb048b95 100644 --- a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/SampleRobot.java +++ b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/SampleRobot.java @@ -83,6 +83,7 @@ public class SampleRobot extends RobotBase { * Test code should go here. Users should add test code to this method that should run while the * robot is in test mode. */ + @SuppressWarnings("PMD.JUnit4TestShouldUseTestAnnotation") public void test() { System.out.println("Default test() method running, consider providing your own"); } diff --git a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/SensorBase.java b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/SensorBase.java index 4ebac1b451..c2ede4ab99 100644 --- a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/SensorBase.java +++ b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/SensorBase.java @@ -89,10 +89,10 @@ public abstract class SensorBase { protected static void checkSolenoidModule(final int moduleNumber) { if (!SolenoidJNI.checkSolenoidModule(moduleNumber)) { StringBuilder buf = new StringBuilder(); - buf.append("Requested solenoid module is out of range. Minimumm: 0, Maximum: "); - buf.append(kPCMModules); - buf.append(", Requested: "); - buf.append(moduleNumber); + buf.append("Requested solenoid module is out of range. Minimumm: 0, Maximum: ") + .append(kPCMModules) + .append(", Requested: ") + .append(moduleNumber); throw new IndexOutOfBoundsException(buf.toString()); } } @@ -106,10 +106,10 @@ public abstract class SensorBase { protected static void checkDigitalChannel(final int channel) { if (!DIOJNI.checkDIOChannel(channel)) { StringBuilder buf = new StringBuilder(); - buf.append("Requested DIO channel is out of range. Minimumm: 0, Maximum: "); - buf.append(kDigitalChannels); - buf.append(", Requested: "); - buf.append(channel); + buf.append("Requested DIO channel is out of range. Minimumm: 0, Maximum: ") + .append(kDigitalChannels) + .append(", Requested: ") + .append(channel); throw new IndexOutOfBoundsException(buf.toString()); } } @@ -123,10 +123,10 @@ public abstract class SensorBase { protected static void checkRelayChannel(final int channel) { if (!RelayJNI.checkRelayChannel(channel)) { StringBuilder buf = new StringBuilder(); - buf.append("Requested relay channel is out of range. Minimumm: 0, Maximum: "); - buf.append(kRelayChannels); - buf.append(", Requested: "); - buf.append(channel); + buf.append("Requested relay channel is out of range. Minimumm: 0, Maximum: ") + .append(kRelayChannels) + .append(", Requested: ") + .append(channel); throw new IndexOutOfBoundsException(buf.toString()); } } @@ -140,10 +140,10 @@ public abstract class SensorBase { protected static void checkPWMChannel(final int channel) { if (!PWMJNI.checkPWMChannel(channel)) { StringBuilder buf = new StringBuilder(); - buf.append("Requested PWM channel is out of range. Minimumm: 0, Maximum: "); - buf.append(kPwmChannels); - buf.append(", Requested: "); - buf.append(channel); + buf.append("Requested PWM channel is out of range. Minimumm: 0, Maximum: ") + .append(kPwmChannels) + .append(", Requested: ") + .append(channel); throw new IndexOutOfBoundsException(buf.toString()); } } @@ -157,10 +157,10 @@ public abstract class SensorBase { protected static void checkAnalogInputChannel(final int channel) { if (!AnalogJNI.checkAnalogInputChannel(channel)) { StringBuilder buf = new StringBuilder(); - buf.append("Requested analog input channel is out of range. Minimumm: 0, Maximum: "); - buf.append(kAnalogInputChannels); - buf.append(", Requested: "); - buf.append(channel); + buf.append("Requested analog input channel is out of range. Minimumm: 0, Maximum: ") + .append(kAnalogInputChannels) + .append(", Requested: ") + .append(channel); throw new IndexOutOfBoundsException(buf.toString()); } } @@ -174,10 +174,10 @@ public abstract class SensorBase { protected static void checkAnalogOutputChannel(final int channel) { if (!AnalogJNI.checkAnalogOutputChannel(channel)) { StringBuilder buf = new StringBuilder(); - buf.append("Requested analog output channel is out of range. Minimumm: 0, Maximum: "); - buf.append(kAnalogOutputChannels); - buf.append(", Requested: "); - buf.append(channel); + buf.append("Requested analog output channel is out of range. Minimumm: 0, Maximum: ") + .append(kAnalogOutputChannels) + .append(", Requested: ") + .append(channel); throw new IndexOutOfBoundsException(buf.toString()); } } @@ -190,10 +190,10 @@ public abstract class SensorBase { protected static void checkSolenoidChannel(final int channel) { if (!SolenoidJNI.checkSolenoidChannel(channel)) { StringBuilder buf = new StringBuilder(); - buf.append("Requested solenoid channel is out of range. Minimumm: 0, Maximum: "); - buf.append(kSolenoidChannels); - buf.append(", Requested: "); - buf.append(channel); + buf.append("Requested solenoid channel is out of range. Minimumm: 0, Maximum: ") + .append(kSolenoidChannels) + .append(", Requested: ") + .append(channel); throw new IndexOutOfBoundsException(buf.toString()); } } @@ -207,10 +207,10 @@ public abstract class SensorBase { protected static void checkPDPChannel(final int channel) { if (!PDPJNI.checkPDPChannel(channel)) { StringBuilder buf = new StringBuilder(); - buf.append("Requested PDP channel is out of range. Minimumm: 0, Maximum: "); - buf.append(kPDPChannels); - buf.append(", Requested: "); - buf.append(channel); + buf.append("Requested PDP channel is out of range. Minimumm: 0, Maximum: ") + .append(kPDPChannels) + .append(", Requested: ") + .append(channel); throw new IndexOutOfBoundsException(buf.toString()); } } @@ -223,10 +223,10 @@ public abstract class SensorBase { protected static void checkPDPModule(final int module) { if (!PDPJNI.checkPDPModule(module)) { StringBuilder buf = new StringBuilder(); - buf.append("Requested PDP module is out of range. Minimumm: 0, Maximum: "); - buf.append(kPDPModules); - buf.append(", Requested: "); - buf.append(module); + buf.append("Requested PDP module is out of range. Minimumm: 0, Maximum: ") + .append(kPDPModules) + .append(", Requested: ") + .append(module); throw new IndexOutOfBoundsException(buf.toString()); } } diff --git a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/buttons/Trigger.java b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/buttons/Trigger.java index d0cd695b06..5457fb4a1a 100644 --- a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/buttons/Trigger.java +++ b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/buttons/Trigger.java @@ -39,10 +39,9 @@ public abstract class Trigger implements Sendable { * * @return whether get() return true or the internal table for SmartDashboard use is pressed. */ + @SuppressWarnings("PMD.UselessParentheses") private boolean grab() { - // FIXME make is connected work? - return get() - || (m_table != null /* && m_table.isConnected() */ && m_table.getBoolean("pressed", false)); + return get() || (m_table != null && m_table.getBoolean("pressed", false)); } diff --git a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/LinkedListElement.java b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/LinkedListElement.java index 7dd500ead6..055150a879 100644 --- a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/LinkedListElement.java +++ b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/LinkedListElement.java @@ -46,6 +46,7 @@ class LinkedListElement { } } + @SuppressWarnings("PMD.EmptyIfStmt") public LinkedListElement remove() { if (m_previous == null && m_next == null) { // no-op