Skip to content

Commit

Permalink
Add pmd and fix warnings
Browse files Browse the repository at this point in the history
Signed-off-by: Jade Turner <spacey-sooty@proton.me>
  • Loading branch information
spacey-sooty committed Dec 1, 2024
1 parent a19e6ab commit e404907
Show file tree
Hide file tree
Showing 14 changed files with 223 additions and 38 deletions.
1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
*.traj text eol=lf
*.chor text eol=lf
src/main/java/frc/robot/generated/** linguist-generated
src/main/java/frc/robot/BuildConstants.java linguist-generated
*.traj linguist-generated
*.chor linguist-generated
41 changes: 41 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Build

on: [pull_request, push]

jobs:
build:
name: "Build Java"
runs-on: ubuntu-latest
container: wpilib/roborio-cross-ubuntu:2024-22.04
steps:
- uses: actions/checkout@v4
- name: Compile robot code
run: ./gradlew build

format:
name: "Lint and Format Java"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Fetch all history and metadata
run: |
git checkout -b pr
git branch -f master origin/master
- uses: actions/setup-java@v4
with:
java-version: 17
distribution: temurin
- uses: gradle/gradle-build-action@v3
- uses: actions/setup-python@v5
with:
python-version: 3.8
- name: Install wpiformat
run: pip3 install wpiformat
- name: Run
run: wpiformat
- name: Check output
run: git --no-pager diff --exit-code HEAD
- name: Run spotless
run: ./gradlew build -PwithJavaFormat
17 changes: 14 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ dependencies {
// Create version file
project.compileJava.dependsOn(createVersionFile)
gversion {
srcDir = "src/main/java/"
classPackage = "frc.robot"
srcDir = "src/main/java/generated/"
classPackage = "frc.robot.generated"
className = "BuildConstants"
dateFormat = "yyyy-MM-dd HH:mm:ss z"
timeZone = "Australia/Perth"
Expand Down Expand Up @@ -152,7 +152,7 @@ spotless {
java {
target fileTree('.') {
include '**/*.java'
exclude '**/build/**', '**/build-*/**'
exclude '**/build/**', '**/build-*/**', 'src/main/java/generated/**'
}
toggleOffOn()
googleJavaFormat()
Expand Down Expand Up @@ -190,3 +190,14 @@ spotless {
endWithNewline()
}
}

apply plugin: "pmd"

pmd {
toolVersion = "7.3.0"
consoleOutput = true
sourceSets = [project.sourceSets["main"]]
reportsDir = file("${project.buildDir}/reports/pmd")
ruleSetFiles = files(file("$rootDir/pmd-ruleset.xml"))
ruleSets = []
}
125 changes: 125 additions & 0 deletions pmd-ruleset.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?xml version="1.0"?>
<ruleset name="CurtinFRC Ruleset"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">

<description>PMD Ruleset for Curtin FRC Projects</description>

<exclude-pattern>.*/*JNI.*</exclude-pattern>

<rule ref="category/java/bestpractices.xml">
<exclude name="AccessorClassGeneration" />
<exclude name="AccessorMethodGeneration" />
<exclude name="AvoidPrintStackTrace" />
<exclude name="AvoidReassigningCatchVariables" />
<exclude name="AvoidReassigningParameters" />
<exclude name="AvoidUsingHardCodedIP" />
<exclude name="ConstantsInInterface" />
<exclude name="JUnitAssertionsShouldIncludeMessage" />
<exclude name="JUnitTestContainsTooManyAsserts" />
<exclude name="JUnitTestsShouldIncludeAssert" />
<exclude name="JUnit4TestShouldUseAfterAnnotation" />
<exclude name="JUnit4TestShouldUseBeforeAnnotation" />
<exclude name="JUnit4TestShouldUseTestAnnotation" />
<exclude name="LooseCoupling" />
<exclude name="PreserveStackTrace" />
<exclude name="ReplaceHashtableWithMap" />
<exclude name="ReplaceVectorWithList" />
<exclude name="SwitchStmtsShouldHaveDefault" />
<exclude name="SystemPrintln" />
<exclude name="UseVarargs" />
</rule>
<rule ref="category/java/bestpractices.xml/UnusedPrivateMethod">
<properties>
<property name="violationSuppressRegex"
value=".*'.*Arguments\(\)'.*" />
</properties>
</rule>
<rule ref="category/java/bestpractices.xml/UnusedLocalVariable">
<properties>
<property name="violationSuppressXPath" value="//Resource" />
</properties>
</rule>

<rule ref="category/java/design.xml">
<exclude name="AvoidThrowingRawExceptionTypes" />
<exclude name="AvoidThrowingNewInstanceOfSameException" />
<exclude name="CognitiveComplexity" />
<exclude name="CouplingBetweenObjects" />
<exclude name="CyclomaticComplexity" />
<exclude name="DataClass" />
<exclude name="ExceptionAsFlowControl" />
<exclude name="ExcessiveImports" />
<exclude name="ExcessiveParameterList" />
<exclude name="ExcessivePublicCount" />
<exclude name="GodClass" />
<exclude name="LawOfDemeter" />
<exclude name="LoosePackageCoupling" />
<exclude name="NPathComplexity" />
<exclude name="NcssCount" />
<exclude name="TooManyFields" />
<exclude name="TooManyMethods" />
</rule>

<rule ref="category/java/errorprone.xml">
<exclude name="AssignmentToNonFinalStatic" />
<exclude name="AvoidCatchingThrowable" />
<exclude name="AvoidDuplicateLiterals" />
<exclude name="AvoidLiteralsInIfCondition" />
<exclude name="CloseResource" />
<exclude name="ConstructorCallsOverridableMethod" />
<exclude name="DoNotTerminateVM" />
<exclude name="EmptyCatchBlock" />
<exclude name="FinalizeDoesNotCallSuperFinalize" />
<exclude name="JUnitSpelling" />
<exclude name="MissingSerialVersionUID" />
<exclude name="NonSerializableClass" />
<exclude name="NullAssignment" />
</rule>

<rule ref="category/java/multithreading.xml">
<exclude name="AvoidSynchronizedAtMethodLevel" />
<exclude name="AvoidUsingVolatile" />
<exclude name="DoNotUseThreads" />
<exclude name="UseConcurrentHashMap" />
</rule>

<rule ref="category/java/performance.xml">
<exclude name="AvoidInstantiatingObjectsInLoops" />
</rule>

<rule name="UnnecessaryCastRule" language="java"
message="Avoid unnecessary casts"
class="net.sourceforge.pmd.lang.java.rule.codestyle.UnnecessaryCastRule"
externalInfoUrl="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryCastRule.java" />

<!-- Custom Rules -->
<rule name="UseRequireNonNull"
message="Use Objects.requireNonNull() instead of throwing a NullPointerException yourself."
language="java" class="net.sourceforge.pmd.lang.rule.xpath.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>
5 changes: 3 additions & 2 deletions src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import edu.wpi.first.wpilibj2.command.Commands;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
import edu.wpi.first.wpilibj2.command.sysid.SysIdRoutine.Direction;
import frc.robot.generated.BuildConstants;
import frc.robot.generated.TunerConstants;
import frc.robot.subsystems.drive.Drive;
import frc.robot.subsystems.drive.DriveIO;
Expand All @@ -48,9 +49,9 @@
import org.littletonrobotics.urcl.URCL;

public class Robot extends LoggedRobot {
private double MaxSpeed =
private final double MaxSpeed =
TunerConstants.kSpeedAt12Volts.in(MetersPerSecond); // kSpeedAt12Volts desired top speed
private double MaxAngularRate =
private final double MaxAngularRate =
RotationsPerSecond.of(0.75)
.in(RadiansPerSecond); // 3/4 of a rotation per second max angular velocity

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/frc/robot/generated/BuildConstants.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/main/java/frc/robot/subsystems/drive/Drive.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public boolean isTeleopAtSetpoint() {
* SysId routine for characterizing steer. This is used to find PID gains for
* the steer motors.
*/
@SuppressWarnings("PMD.UnusedPrivateField")
private final SysIdRoutine m_sysIdRoutineSteer =
new SysIdRoutine(
new SysIdRoutine.Config(
Expand All @@ -84,6 +85,7 @@ public boolean isTeleopAtSetpoint() {
* See the documentation of SwerveRequest.SysIdSwerveRotation for info on
* importing the log to SysId.
*/
@SuppressWarnings("PMD.UnusedPrivateField")
private final SysIdRoutine m_sysIdRoutineRotation =
new SysIdRoutine(
new SysIdRoutine.Config(
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/frc/robot/subsystems/drive/DriveIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public static class DriveIOInputs {
public SwerveModuleState[] moduleTargetStates = new SwerveModuleState[4];
public Pose2d pose = new Pose2d();
public ChassisSpeeds speeds = new ChassisSpeeds();
public double odometryPeriodSeconds = 0.0;
public int successfulDaqs = 0;
public int failedDaqs = 0;
public double odometryPeriodSeconds;
public int successfulDaqs;
public int failedDaqs;

public double gyroRate = 0.0;
public double gyroRate;
public Rotation3d rotation3d = new Rotation3d();
public boolean odometryIsValid = false;
public boolean odometryIsValid;
}

public default void updateInputs(DriveIOInputs inputs) {}
Expand Down
15 changes: 2 additions & 13 deletions src/main/java/frc/robot/subsystems/drive/DriveIOCTRE.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
import com.ctre.phoenix6.swerve.SwerveDrivetrain;
import com.ctre.phoenix6.swerve.SwerveDrivetrainConstants;
import com.ctre.phoenix6.swerve.SwerveModuleConstants;
import com.ctre.phoenix6.swerve.SwerveRequest;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.wpilibj.Notifier;
import edu.wpi.first.wpilibj.RobotController;

/**
* Class that extends the Phoenix SwerveDrivetrain class and implements subsystem so it can be used
* in command-based projects easily.
*/
@SuppressWarnings("PMD.SingularField")
public class DriveIOCTRE extends SwerveDrivetrain implements DriveIO {
private static final double kSimLoopPeriod = 0.005; // 5 ms
private Notifier m_simNotifier = null;
private Notifier m_simNotifier;
private double m_lastSimTime;

public DriveIOCTRE(
Expand Down Expand Up @@ -55,16 +54,6 @@ public void updateInputs(DriveIOInputs inputs) {
inputs.odometryIsValid = isOdometryValid();
}

@Override
public void resetPose(Pose2d pose) {
super.resetPose(pose);
}

@Override
public void setControl(SwerveRequest request) {
super.setControl(request);
}

private void startSimThread() {
m_lastSimTime = Utils.getCurrentTimeSeconds();

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/frc/robot/subsystems/shooter/ShooterIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
public interface ShooterIO {
@AutoLog
public static class ShooterIOInputs {
public double position = 0.0;
public double velocity = 0.0;
public double appliedVolts = 0.0;
public double currentAmps = 0.0;
public double position;
public double velocity;
public double appliedVolts;
public double currentAmps;
}

/** Updates the set of loggable inputs. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public class VisionIOLimelight implements VisionIO {
private final NetworkTable impl;
private final DoubleArraySubscriber botpose;
double[] validIds = null;
double[] validIds;
private final String camera;

public VisionIOLimelight(String camera) {
Expand Down Expand Up @@ -41,6 +41,6 @@ public void updateInputs(VisionIOInputs inputs) {
@Override
public void setValidIds(double[] validIds) {
impl.getDoubleArrayTopic("fiducial_id_filters_set").publish().set(validIds);
this.validIds = validIds;
this.validIds = validIds.clone();
}
}
2 changes: 1 addition & 1 deletion src/main/java/frc/robot/subsystems/vision/VisionIOSim.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void updateInputs(VisionIOInputs inputs) {
inputs.camera = cameraName;

var results = camera.getAllUnreadResults();
if (results.size() > 0) {
if (results.isEmpty()) {
var result = results.get(results.size() - 1);
if (result.getMultiTagResult().isPresent()) {
inputs.estimatedPose =
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/frc/robot/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import edu.wpi.first.math.geometry.Pose3d;
import edu.wpi.first.util.WPIUtilJNI;

public class Util {
public final class Util {
private Util() {}

/** Time in milliseconds */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package frc.robot;
package frc.robot.generated;

/**
* Automatically generated file containing build version information.
Expand All @@ -7,12 +7,12 @@ public final class BuildConstants {
public static final String MAVEN_GROUP = "";
public static final String MAVEN_NAME = "2025-Reefscape";
public static final String VERSION = "unspecified";
public static final int GIT_REVISION = 1;
public static final String GIT_SHA = "9bf7dbcdc5f4ab1a52da29bfb97fb310e4722d02";
public static final String GIT_DATE = "2024-12-01 15:18:41 AWST";
public static final int GIT_REVISION = 3;
public static final String GIT_SHA = "a19e6abe401ffc7d43c87ddc67ae93211b27a8ab";
public static final String GIT_DATE = "2024-12-01 19:28:10 AWST";
public static final String GIT_BRANCH = "master";
public static final String BUILD_DATE = "2024-12-01 19:22:02 AWST";
public static final long BUILD_UNIX_TIME = 1733052122409L;
public static final String BUILD_DATE = "2024-12-01 19:43:18 AWST";
public static final long BUILD_UNIX_TIME = 1733053398752L;
public static final int DIRTY = 1;

private BuildConstants(){}
Expand Down

0 comments on commit e404907

Please sign in to comment.