Skip to content

Commit f450c9d

Browse files
committed
address some SpotBugs warnings
1 parent 09db6df commit f450c9d

File tree

11 files changed

+33
-44
lines changed

11 files changed

+33
-44
lines changed

src/main/java/net/discordjug/javabot/data/h2db/commands/ExportSchemaSubcommand.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
import java.nio.file.Files;
1515
import java.nio.file.Path;
1616
import java.sql.Connection;
17-
import java.sql.PreparedStatement;
1817
import java.sql.SQLException;
18+
import java.sql.Statement;
1919
import java.util.concurrent.ExecutorService;
2020

2121
import javax.sql.DataSource;
@@ -51,9 +51,9 @@ public void execute(SlashCommandInteractionEvent event) {
5151
boolean includeData = event.getOption("include-data", false, OptionMapping::getAsBoolean);
5252
event.deferReply(false).queue();
5353
asyncPool.submit(() -> {
54-
try (Connection con = dataSource.getConnection()) {
55-
PreparedStatement stmt = con.prepareStatement(String.format("SCRIPT %s TO '%s';", includeData ? "" : "NODATA", SCHEMA_FILE));
56-
boolean success = stmt.execute();
54+
try (Connection con = dataSource.getConnection();
55+
Statement stmt = con.createStatement()) {
56+
boolean success = stmt.execute(String.format("SCRIPT %s TO '%s';", includeData ? "" : "NODATA", SCHEMA_FILE));
5757
if (!success) {
5858
event.getHook().sendMessage("Exporting the schema was not successful.").queue();
5959
} else {

src/main/java/net/discordjug/javabot/data/h2db/commands/ExportTableSubcommand.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import java.nio.file.Files;
1717
import java.nio.file.Path;
1818
import java.sql.Connection;
19-
import java.sql.PreparedStatement;
2019
import java.sql.SQLException;
20+
import java.sql.Statement;
2121
import java.util.concurrent.ExecutorService;
2222

2323
import javax.sql.DataSource;
@@ -71,9 +71,9 @@ public void execute(SlashCommandInteractionEvent event) {
7171
}
7272
event.deferReply(false).queue();
7373
asyncPool.submit(() -> {
74-
try (Connection con = dataSource.getConnection()) {
75-
PreparedStatement stmt = con.prepareStatement(String.format("SCRIPT %s TO '%s' TABLE %s;", includeData ? "COLUMNS" : "NODATA", TABLE_FILE, tableOption.getAsString()));
76-
boolean success = stmt.execute();
74+
try (Connection con = dataSource.getConnection();
75+
Statement stmt = con.createStatement()) {
76+
boolean success = stmt.execute(String.format("SCRIPT %s TO '%s' TABLE %s;", includeData ? "COLUMNS" : "NODATA", TABLE_FILE, tableOption.getAsString()));
7777
if (!success) {
7878
event.getHook().sendMessage("Exporting the table was not successful.").queue();
7979
} else {

src/main/java/net/discordjug/javabot/data/h2db/commands/MessageCacheInfoSubcommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private MessageEmbed buildInfoEmbed(GuildConfig config, User author) {
5050
.setTitle("Message Cache Info")
5151
.setColor(Responses.Type.DEFAULT.getColor())
5252
.addField("Table Size", dbActions.getLogicalSize("message_cache") + " bytes", false)
53-
.addField("Message Count", String.valueOf(messageCache.messageCount), true)
53+
.addField("Message Count", String.valueOf(messageCache.getMessageCount()), true)
5454
.addField("Cached (Memory)", String.format("%s/%s (%.2f%%)", messageCache.cache.size(), maxMessages, ((float) messageCache.cache.size() / maxMessages) * 100), true)
5555
.addField("Cached (Database)", String.format("%s/%s (%.2f%%)", messages, maxMessages, ((float) messages / maxMessages) * 100), true)
5656
.build();

src/main/java/net/discordjug/javabot/data/h2db/message_cache/MessageCache.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.discordjug.javabot.data.h2db.message_cache;
22

3+
import lombok.Getter;
34
import lombok.extern.slf4j.Slf4j;
45
import net.discordjug.javabot.data.config.BotConfig;
56
import net.discordjug.javabot.data.config.guild.MessageCacheConfig;
@@ -48,7 +49,8 @@ public class MessageCache {
4849
* If a certain threshold is reached, messages will be synchronized to reduce the chances of loosing
4950
* messages during an unexpected shutdown.
5051
*/
51-
public int messageCount = 0;
52+
@Getter
53+
private int messageCount = 0;
5254

5355
private final ExecutorService asyncPool;
5456
private final BotConfig botConfig;

src/main/java/net/discordjug/javabot/systems/qotw/submissions/SubmissionManager.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,8 @@ public WebhookMessageCreateAction<?> handleSubmission(@NotNull ButtonInteraction
8383
thread.sendMessage(member.getAsMention())
8484
.setEmbeds(buildSubmissionThreadEmbed(event.getUser(), questionOptional.get(), config))
8585
.setComponents(ActionRow.of(Button.danger("qotw-submission:delete", "Delete Submission")))
86-
.queue(s -> {
87-
}, err -> ExceptionLogger.capture(err, getClass().getSimpleName()));
88-
QOTWSubmission submission = new QOTWSubmission(thread);
89-
submission.setAuthor(member.getUser());
86+
.queue(s -> {},
87+
err -> ExceptionLogger.capture(err, getClass().getSimpleName()));
9088
} else {
9189
thread.sendMessage("Could not retrieve current QOTW Question. Please contact an Administrator if you think that this is a mistake.")
9290
.queue();

src/main/java/net/discordjug/javabot/systems/staff_commands/RunScheduledTaskCommand.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ public void execute(SlashCommandInteractionEvent event) {
6767
//CHECKSTYLE:OFF This is a handler for all sort of failures that could possibly happen
6868
}catch (RuntimeException e) {
6969
//CHECKSTYLE:ON
70-
Responses.error(event, "Task failed with an exception", e.getClass().getName() + (e.getMessage() == null ? "" : ": "+e.getMessage()));
70+
Responses.error(event,
71+
"Task failed with an exception: %s",
72+
e.getClass().getName() + (e.getMessage() == null ? "" : ": "+e.getMessage()))
73+
.queue();
7174
}
7275
}, () -> {
7376
Responses.error(event, "Cannot find task `%s`", name).queue();

src/main/java/net/discordjug/javabot/systems/staff_commands/tags/commands/TagsSubcommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
3737
handleCustomTagsSubcommand(event).queue();
3838
} catch (SQLException e) {
3939
ExceptionLogger.capture(e, getClass().getSimpleName());
40-
Responses.error(event, "An error occurred while executing this command.");
40+
Responses.error(event, "An error occurred while executing this command.").queue();
4141
}
4242
}
4343

src/main/java/net/discordjug/javabot/util/IndentationHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,6 @@ private static boolean isEscaped(StringBuilder builder, int index) {
199199
for (--index;index > 0 && index < builder.length() && builder.charAt(index) == '\\'; index--) {
200200
numberOfCharacters++;
201201
}
202-
return numberOfCharacters % 2 == 1;
202+
return numberOfCharacters % 2 != 0;
203203
}
204204
}

src/main/java/net/discordjug/javabot/util/Plotter.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import java.awt.Color;
44
import java.awt.Graphics2D;
55
import java.awt.image.BufferedImage;
6-
import java.util.HashMap;
76
import java.util.List;
8-
import java.util.Map;
97

108
/**
119
* Creates diagrams.
@@ -75,8 +73,6 @@ private void plotEntries(Graphics2D g2d, int x, int y, int width, int height) {
7573
Bar bar = entry.second();
7674
int totalBarHeight = 0;
7775

78-
Map<String, Color> colors = new HashMap<>();
79-
8076
double barSum = 0;
8177

8278
for (Pair<Color, Double> barSquares : bar.elements()) {

src/main/java/net/discordjug/javabot/util/StringResourceCache.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ private StringResourceCache() {
2424
* @return The resources' content as a String.
2525
*/
2626
public static String load(String resourceName) {
27-
String sql = CACHE.get(resourceName);
28-
if (sql == null) {
29-
InputStream is = StringResourceCache.class.getResourceAsStream(resourceName);
30-
if (is == null) throw new RuntimeException("Could not load " + resourceName);
31-
try {
32-
sql = new String(is.readAllBytes(), StandardCharsets.UTF_8);
33-
} catch (IOException e) {
27+
String content = CACHE.get(resourceName);
28+
if (content == null) {
29+
try(InputStream is = StringResourceCache.class.getResourceAsStream(resourceName)){
30+
if (is == null) throw new RuntimeException("Could not load " + resourceName);
31+
content = new String(is.readAllBytes(), StandardCharsets.UTF_8);
32+
}catch (IOException e) {
3433
ExceptionLogger.capture(e, StringResourceCache.class.getSimpleName());
3534
throw new UncheckedIOException(e);
3635
}
37-
CACHE.put(resourceName, sql);
36+
37+
CACHE.put(resourceName, content);
3838
}
39-
return sql;
39+
return content;
4040
}
4141
}

0 commit comments

Comments
 (0)