Skip to content

Commit 07d0413

Browse files
committed
Fix java linting
1 parent 7428cc5 commit 07d0413

File tree

16 files changed

+66
-54
lines changed

16 files changed

+66
-54
lines changed

bot/build.gradle.kts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ plugins {
3434
id("com.github.breadmoirai.github-release")
3535
}
3636

37+
val pmdVersion = "7.0.0-rc4"
38+
3739
val numberVersion = "3.108.0"
3840

3941
project.group = "me.duncte123.skybot"
@@ -69,6 +71,9 @@ dependencies {
6971
implementation(libs.redis)
7072
implementation(libs.mariadb)
7173
implementation(libs.bundles.database)
74+
75+
pmd("net.sourceforge.pmd:pmd-ant:$pmdVersion")
76+
pmd("net.sourceforge.pmd:pmd-java:$pmdVersion")
7277
}
7378

7479
val compileKotlin: KotlinCompile by tasks
@@ -183,7 +188,7 @@ kotlinter {
183188

184189
pmd {
185190
isConsoleOutput = true
186-
toolVersion = "6.55.0"
191+
toolVersion = pmdVersion
187192
rulesMinimumPriority.set(5)
188193
ruleSets = listOf()
189194
ruleSetFiles(File("linters/pmd.xml"))

bot/linters/pmd.xml

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
<exclude name="OnlyOneReturn"/>
4444
<exclude name="UseUnderscoresInNumericLiterals"/>
4545
<exclude name="TooManyStaticImports"/>
46-
<exclude name="DefaultPackage"/>
46+
<exclude name="CommentDefaultAccessModifier"/>
47+
<exclude name="UselessParentheses"/>
4748
<exclude name="LinguisticNaming"/>
4849
</rule>
4950

@@ -66,6 +67,7 @@
6667
<exclude name="TooManyMethods"/>
6768
<exclude name="NcssCount"/>
6869
<exclude name="ExcessiveMethodLength"/>
70+
<exclude name="MutableStaticState"/>
6971
</rule>
7072

7173
<rule ref="category/java/design.xml/NPathComplexity">
@@ -74,6 +76,26 @@
7476
</properties>
7577
</rule>
7678

79+
<!-- TODO: make sure this is as low as you can get it -->
80+
<rule ref="category/java/design.xml/CognitiveComplexity">
81+
<properties>
82+
<property name="reportLevel" value="40" />
83+
</properties>
84+
</rule>
85+
86+
<rule ref="category/java/bestpractices.xml/LooseCoupling">
87+
<properties>
88+
<property name="allowedTypes" value="java.util.Properties,com.jagrosh.jagtag.Environment,me.duncte123.skybot.objects.DBMap" />
89+
</properties>
90+
</rule>
91+
92+
<!-- TODO: make sure this is as low as you can get it -->
93+
<rule ref="category/java/design.xml/CouplingBetweenObjects">
94+
<properties>
95+
<property name="threshold" value="40" />
96+
</properties>
97+
</rule>
98+
7799
<rule ref="category/java/errorprone.xml/AssignmentInOperand">
78100
<properties>
79101
<property name="allowIf" value="false" />
@@ -89,18 +111,19 @@
89111

90112
<rule ref="category/java/errorprone.xml">
91113
<exclude name="AvoidCatchingThrowable"/>
114+
<exclude name="AssignmentToNonFinalStatic"/>
92115
<exclude name="CallSuperFirst"/>
93116
<exclude name="CallSuperLast"/>
94117
<exclude name="CloseResource"/>
95118
<exclude name="DoNotCallGarbageCollectionExplicitly"/>
96119
<exclude name="DoNotTerminateVM"/>
97120
<exclude name="MissingSerialVersionUID"/>
98121
<exclude name="OverrideBothEqualsAndHashcode"/>
99-
<exclude name="BeanMembersShouldSerialize"/>
100122
<exclude name="AvoidLiteralsInIfCondition"/>
101123
<exclude name="UseLocaleWithCaseConversions"/>
102124
<exclude name="AvoidDuplicateLiterals"/>
103125
<exclude name="SimpleDateFormatNeedsLocale"/>
126+
<exclude name="ReturnEmptyCollectionRatherThanNull"/>
104127
</rule>
105128

106129
<rule ref="category/java/multithreading.xml">

bot/src/main/java/fredboat/audio/player/LavalinkManager.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,7 @@ public boolean isConnected(Guild guild) {
107107
}
108108

109109
public boolean isConnected(long guildId) {
110-
if (!isEnabled()) {
111-
return false;
112-
}
113-
114-
return lavalink.getLink(guildId).getState() == LinkState.CONNECTED;
110+
return !isEnabled() || lavalink.getLink(guildId).getState() == LinkState.CONNECTED;
115111
}
116112

117113
@SuppressWarnings("ConstantConditions") // cache is enabled

bot/src/main/java/me/duncte123/skybot/SkyBot.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@
5656
import static net.dv8tion.jda.api.utils.MemberCachePolicy.PENDING;
5757

5858
public final class SkyBot {
59-
public static ScheduledExecutorService SYSTEM_POOL = Executors.newSingleThreadScheduledExecutor((r) -> {
60-
final Thread t = new Thread(r, "System Pool");
61-
t.setDaemon(true);
62-
return t;
59+
public static final ScheduledExecutorService SYSTEM_POOL = Executors.newSingleThreadScheduledExecutor((r) -> {
60+
final Thread thread = new Thread(r, "System Pool");
61+
thread.setDaemon(true);
62+
return thread;
6363
});
6464

6565
private static SkyBot instance;

bot/src/main/java/me/duncte123/skybot/commands/essentials/TestTagCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import static me.duncte123.botcommons.messaging.MessageUtils.sendEmbed;
3232
import static me.duncte123.botcommons.messaging.MessageUtils.sendMsg;
3333

34+
@SuppressWarnings("PMD.TestClassWithoutTestCases")
3435
public class TestTagCommand extends Command {
3536

3637
public TestTagCommand() {

bot/src/main/java/me/duncte123/skybot/commands/guild/mod/CleanupCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ private void handleComplete(int count, CommandContext ctx) {
268268
*
269269
* @return the future with a different timeout
270270
*/
271-
@SuppressWarnings({"rawtypes", "unchecked"})
271+
@SuppressWarnings({"rawtypes", "unchecked", "PMD.AvoidAccessibilityAlteration"})
272272
private CompletableFuture<Void> hackTimeout(CompletableFuture<Void> future) {
273273
if (!(future instanceof RestFuture)) {
274274
// Ignore stuff that is not a rest future

bot/src/main/java/me/duncte123/skybot/commands/image/ImageCommandBase.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private boolean canSendFile(CommandContext ctx) {
6262
}
6363

6464
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
65-
/* package */ boolean passes(CommandContext ctx) {
65+
/* package */ boolean passes(CommandContext ctx) {
6666
return passes(ctx, true);
6767
}
6868

@@ -120,18 +120,18 @@ protected String getImageFromCommand(CommandContext ctx) {
120120

121121
// I hate this so much
122122
// But I won't change one pmd rule just for the sake of using !isEmpty here
123-
if (ctx.getMessage().getAttachments().size() > 0) {
123+
if (ctx.getMessage().getAttachments().isEmpty()) {
124124
url = tryGetAttachment(ctx);
125125
} else if (!mentionedUsers.isEmpty()) {
126-
url = getAvatarUrl(mentionedUsers.get(0));
126+
url = getAvatarUrl(mentionedUsers.getFirst());
127127
} else if (!args.isEmpty()) {
128-
if (AirUtils.isURL(args.get(0))) {
129-
url = tryGetUrl(ctx, args.get(0));
128+
if (AirUtils.isURL(args.getFirst())) {
129+
url = tryGetUrl(ctx, args.getFirst());
130130
} else {
131131
final List<Member> textMentions = FinderUtils.searchMembers(ctx.getArgsJoined(), ctx);
132132

133133
if (!textMentions.isEmpty()) {
134-
url = getAvatarUrl(textMentions.get(0).getUser());
134+
url = getAvatarUrl(textMentions.getFirst().getUser());
135135
}
136136
}
137137
}
@@ -145,7 +145,7 @@ protected String getImageFromCommand(CommandContext ctx) {
145145

146146
@Nullable
147147
private String tryGetAttachment(CommandContext ctx) {
148-
final Attachment attachment = ctx.getMessage().getAttachments().get(0);
148+
final Attachment attachment = ctx.getMessage().getAttachments().getFirst();
149149

150150
try (attachment) {
151151
final File file = new File(attachment.getFileName());
@@ -200,7 +200,7 @@ protected String[] splitString(CommandContext ctx) {
200200
return null;
201201
}
202202

203-
if ("".equals(split[0].trim()) || "".equals(split[1].trim())) {
203+
if (split[0].isBlank() || split[1].isBlank()) {
204204
sendMsg(ctx, "Missing arguments, check `" + ctx.getPrefix() + "help " + getName() + '`');
205205
return null;
206206
}

bot/src/main/java/me/duncte123/skybot/database/RedisConnection.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public RedisConnection() {
4444
connect();
4545
}
4646

47+
@SuppressWarnings("PMD.AvoidThrowingRawExceptionTypes")
4748
private void connect() {
4849
if (!canConnect) {
4950
throw new RuntimeException("Shutdown method was called, new connection not allowed");

bot/src/main/java/me/duncte123/skybot/listeners/GuildMemberListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ private void onGuildMemberRemove(GuildMemberRemoveEvent event) {
164164

165165
final Guild guild = event.getGuild();
166166
final long guildId = guild.getIdLong();
167-
final GuildMemberInfo guildCounts = GuildUtils.GUILD_MEMBER_COUNTS.getIfPresent(guildId);
167+
final GuildMemberInfo guildCounts = GuildUtils.GUILD_MEMBER_COUNTS.get(guildId);
168168

169169
if (guildCounts != null) {
170170
if (user.isBot()) {
@@ -327,7 +327,7 @@ private void logMemberNotification(User user, Guild guild, String titlePart, Str
327327
}
328328

329329
private void updateGuildCount(GuildMemberJoinEvent event, Guild guild) {
330-
final GuildMemberInfo guildCounts = GuildUtils.GUILD_MEMBER_COUNTS.getIfPresent(guild.getIdLong());
330+
final GuildMemberInfo guildCounts = GuildUtils.GUILD_MEMBER_COUNTS.get(guild.getIdLong());
331331

332332
if (guildCounts != null) {
333333
final User user = event.getUser();

bot/src/main/java/me/duncte123/skybot/listeners/MessageListener.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ private void handleMessageEventChecked(String raw, Guild guild, MessageReceivedE
384384
}
385385
}
386386

387+
@SuppressWarnings("PMD.SimplifyBooleanReturns")
387388
private boolean doesNotStartWithPrefix(long selfId, String raw, String customPrefix) {
388389
final String rwLower = raw.toLowerCase();
389390

@@ -418,11 +419,7 @@ private boolean hasCorrectCategory(@Nonnull String raw, @Nonnull String category
418419
getCommandName(customPrefix, raw).toLowerCase()
419420
);
420421

421-
if (command == null) {
422-
return false;
423-
}
424-
425-
return command.getCategory() == CommandCategory.valueOf(categoryName.toUpperCase());
422+
return command == null || command.getCategory() == CommandCategory.valueOf(categoryName.toUpperCase());
426423
}
427424

428425
@SuppressWarnings("ConstantConditions")

0 commit comments

Comments
 (0)