Skip to content

Commit 8a18f74

Browse files
committed
Fix resource leaks from unclosed Autocloseables
1 parent 73cb354 commit 8a18f74

File tree

8 files changed

+94
-79
lines changed

8 files changed

+94
-79
lines changed

src/main/java/org/javacord/bot/Main.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
import org.javacord.bot.commands.SetupCommand;
1717
import org.javacord.bot.commands.WikiCommand;
1818

19+
import java.io.BufferedReader;
1920
import java.io.IOException;
21+
import java.io.InputStream;
2022
import java.nio.file.Files;
2123
import java.nio.file.Path;
2224
import java.nio.file.Paths;
@@ -45,7 +47,9 @@ public static void main(String[] args) throws IOException {
4547
// Token
4648
Path tokenFile = Paths.get(args[0]);
4749
if (Files.isRegularFile(tokenFile)) {
48-
apiBuilder.setToken(Files.newBufferedReader(tokenFile).readLine());
50+
try (BufferedReader tokenFileReader = Files.newBufferedReader(tokenFile)) {
51+
apiBuilder.setToken(tokenFileReader.readLine());
52+
}
4953
} else {
5054
apiBuilder.setToken(args[0]);
5155
}
@@ -76,7 +80,9 @@ private static void setupLogging() throws IOException {
7680
if (log4jConfigurationFileProperty != null) {
7781
Path log4jConfigurationFile = Paths.get(log4jConfigurationFileProperty);
7882
if (!Files.exists(log4jConfigurationFile)) {
79-
Files.copy(Main.class.getResourceAsStream("/log4j2.xml"), log4jConfigurationFile);
83+
try (InputStream fallbackLog4j2ConfigStream = Main.class.getResourceAsStream("/log4j2.xml")) {
84+
Files.copy(fallbackLog4j2ConfigStream, log4jConfigurationFile);
85+
}
8086
}
8187
}
8288

src/main/java/org/javacord/bot/commands/DocsCommand.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import org.javacord.bot.util.javadoc.parser.JavadocMethod;
1212
import org.javacord.bot.util.javadoc.parser.JavadocParser;
1313

14+
import java.io.IOException;
15+
import java.io.InputStream;
1416
import java.util.Arrays;
1517
import java.util.Comparator;
1618
import java.util.List;
@@ -27,23 +29,20 @@ public class DocsCommand implements CommandExecutor {
2729
*
2830
* @param channel The channel where the command was issued.
2931
* @param args The arguments given to the command.
32+
* @throws IOException If the Javacord icon stream cannot be closed properly.
3033
*/
3134
@Command(aliases = {"!docs"}, async = true)
32-
public void onCommand(TextChannel channel, String[] args) {
33-
try {
35+
public void onCommand(TextChannel channel, String[] args) throws IOException {
36+
try (InputStream javacord3Icon = getClass().getClassLoader().getResourceAsStream("javacord3_icon.png")) {
37+
EmbedBuilder embed = new EmbedBuilder()
38+
.setThumbnail(javacord3Icon, "png")
39+
.setColor(Constants.JAVACORD_ORANGE);
3440
if (args.length == 0) { // Just give an overview
35-
EmbedBuilder embed = new EmbedBuilder()
36-
.addField("Overview", "https://docs.javacord.org/")
41+
embed.addField("Overview", "https://docs.javacord.org/")
3742
.addField("Latest release version", "https://docs.javacord.org/api/v/latest")
3843
.addField("Latest snapshot", "https://docs.javacord.org/api/build/latest")
39-
.addField("Hint", "You can search the docs using `!docs [method|class] <search>`")
40-
.setThumbnail(getClass().getClassLoader().getResourceAsStream("javacord3_icon.png"), "png")
41-
.setColor(Constants.JAVACORD_ORANGE);
42-
channel.sendMessage(embed).join();
44+
.addField("Hint", "You can search the docs using `!docs [method|class] <search>`");
4345
} else { // Search
44-
EmbedBuilder embed = new EmbedBuilder()
45-
.setThumbnail(getClass().getClassLoader().getResourceAsStream("javacord3_icon.png"), "png")
46-
.setColor(Constants.JAVACORD_ORANGE);
4746
if (args[0].matches("(classes|class|c)")) { // Search for a class
4847
String searchString = String.join(" ", Arrays.copyOfRange(args, 1, args.length));
4948
populateClasses(channel.getApi(), embed, searchString);
@@ -54,8 +53,8 @@ public void onCommand(TextChannel channel, String[] args) {
5453
String searchString = String.join(" ", args);
5554
populateMethods(channel.getApi(), embed, searchString);
5655
}
57-
channel.sendMessage(embed).join();
5856
}
57+
channel.sendMessage(embed).join();
5958
} catch (Throwable t) {
6059
channel.sendMessage(
6160
"Something went wrong: ```" + ExceptionLogger.unwrapThrowable(t).getMessage() + "```").join();

src/main/java/org/javacord/bot/commands/ExampleCommand.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import org.javacord.api.entity.message.embed.EmbedBuilder;
77
import org.javacord.bot.Constants;
88

9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
912
/**
1013
* The !example command which is used to get an link to the example bot.
1114
*/
@@ -15,15 +18,18 @@ public class ExampleCommand implements CommandExecutor {
1518
* Executes the {@code !example} command.
1619
*
1720
* @param channel The channel where the command was issued.
21+
* @throws IOException If the Javacord icon stream cannot be closed properly.
1822
*/
1923
@Command(aliases = {"!example"}, async = true)
20-
public void onCommand(TextChannel channel) {
21-
EmbedBuilder embed = new EmbedBuilder()
22-
.setThumbnail(getClass().getClassLoader().getResourceAsStream("javacord3_icon.png"), "png")
23-
.setColor(Constants.JAVACORD_ORANGE)
24-
.addField("Example Bot", "https://github.com/Javacord/Example-Bot");
24+
public void onCommand(TextChannel channel) throws IOException {
25+
try (InputStream javacord3Icon = getClass().getClassLoader().getResourceAsStream("javacord3_icon.png")) {
26+
EmbedBuilder embed = new EmbedBuilder()
27+
.setThumbnail(javacord3Icon, "png")
28+
.setColor(Constants.JAVACORD_ORANGE)
29+
.addField("Example Bot", "https://github.com/Javacord/Example-Bot");
2530

26-
channel.sendMessage(embed).join();
31+
channel.sendMessage(embed).join();
32+
}
2733
}
2834

2935
}

src/main/java/org/javacord/bot/commands/GitHubCommand.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import org.javacord.api.entity.message.embed.EmbedBuilder;
77
import org.javacord.bot.Constants;
88

9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
912
/**
1013
* The !github command which is used to link to Javacord related GitHub repositories.
1114
*/
@@ -15,15 +18,18 @@ public class GitHubCommand implements CommandExecutor {
1518
* Executes the {@code !github} command.
1619
*
1720
* @param channel The channel where the command was issued.
21+
* @throws IOException If the Javacord icon stream cannot be closed properly.
1822
*/
1923
@Command(aliases = {"!github"}, async = true)
20-
public void onCommand(TextChannel channel) {
21-
EmbedBuilder embed = new EmbedBuilder()
22-
.addField("Javacord", "https://github.com/Javacord/Javacord")
23-
.addField("Example Bot", "https://github.com/Javacord/Example-Bot")
24-
.setThumbnail(getClass().getClassLoader().getResourceAsStream("javacord3_icon.png"), "png")
25-
.setColor(Constants.JAVACORD_ORANGE);
26-
channel.sendMessage(embed).join();
24+
public void onCommand(TextChannel channel) throws IOException {
25+
try (InputStream javacord3Icon = getClass().getClassLoader().getResourceAsStream("javacord3_icon.png")) {
26+
EmbedBuilder embed = new EmbedBuilder()
27+
.addField("Javacord", "https://github.com/Javacord/Javacord")
28+
.addField("Example Bot", "https://github.com/Javacord/Example-Bot")
29+
.setThumbnail(javacord3Icon, "png")
30+
.setColor(Constants.JAVACORD_ORANGE);
31+
channel.sendMessage(embed).join();
32+
}
2733
}
2834

2935
}

src/main/java/org/javacord/bot/commands/InviteCommand.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import org.javacord.api.entity.message.embed.EmbedBuilder;
77
import org.javacord.bot.Constants;
88

9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
912
/**
1013
* The !invite command which is used to get an invite link to the Javacord Discord server.
1114
*/
@@ -15,15 +18,18 @@ public class InviteCommand implements CommandExecutor {
1518
* Executes the {@code !invite} command.
1619
*
1720
* @param channel The channel where the command was issued.
21+
* @throws IOException If the Javacord icon stream cannot be closed properly.
1822
*/
1923
@Command(aliases = {"!invite"}, async = true)
20-
public void onCommand(TextChannel channel) {
21-
EmbedBuilder embed = new EmbedBuilder()
22-
.setThumbnail(getClass().getClassLoader().getResourceAsStream("javacord3_icon.png"), "png")
23-
.setColor(Constants.JAVACORD_ORANGE)
24-
.addField("Invite Link", "https://discordapp.com/invite/0qJ2jjyneLEgG7y3");
24+
public void onCommand(TextChannel channel) throws IOException {
25+
try (InputStream javacord3Icon = getClass().getClassLoader().getResourceAsStream("javacord3_icon.png")) {
26+
EmbedBuilder embed = new EmbedBuilder()
27+
.setThumbnail(javacord3Icon, "png")
28+
.setColor(Constants.JAVACORD_ORANGE)
29+
.addField("Invite Link", "https://discordapp.com/invite/0qJ2jjyneLEgG7y3");
2530

26-
channel.sendMessage(embed).join();
31+
channel.sendMessage(embed).join();
32+
}
2733
}
2834

2935
}

src/main/java/org/javacord/bot/commands/WikiCommand.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.javacord.bot.util.wiki.parser.WikiParser;
1212

1313
import java.io.IOException;
14+
import java.io.InputStream;
1415
import java.util.Arrays;
1516
import java.util.List;
1617
import java.util.function.Predicate;
@@ -34,20 +35,16 @@ public class WikiCommand implements CommandExecutor {
3435
*/
3536
@Command(aliases = {"!wiki"}, async = true)
3637
public void onCommand(DiscordApi api, TextChannel channel, String[] args) throws IOException {
37-
try {
38+
try (InputStream javacord3Icon = getClass().getClassLoader().getResourceAsStream("javacord3_icon.png")) {
39+
EmbedBuilder embed = new EmbedBuilder()
40+
.setThumbnail(getClass().getClassLoader().getResourceAsStream("javacord3_icon.png"), "png")
41+
.setColor(Constants.JAVACORD_ORANGE);
3842
if (args.length == 0) { // Just an overview
39-
EmbedBuilder embed = new EmbedBuilder()
40-
.setTitle("Javacord Wiki")
43+
embed.setTitle("Javacord Wiki")
4144
.setDescription("The [Javacord Wiki](" + WikiParser.BASE_URL + "/wiki) is an excellent "
4245
+ "resource to get you started with Javacord.\n")
43-
.addInlineField("Hint", "You can search the wiki using `!wiki [title|full] <search>")
44-
.setThumbnail(getClass().getClassLoader().getResourceAsStream("javacord3_icon.png"), "png")
45-
.setColor(Constants.JAVACORD_ORANGE);
46-
channel.sendMessage(embed).join();
46+
.addInlineField("Hint", "You can search the wiki using `!wiki [title|full] <search>");
4747
} else {
48-
EmbedBuilder embed = new EmbedBuilder()
49-
.setThumbnail(getClass().getClassLoader().getResourceAsStream("javacord3_icon.png"), "png")
50-
.setColor(Constants.JAVACORD_ORANGE);
5148
String searchString = String.join(" ", Arrays.copyOfRange(args, 1, args.length)).toLowerCase();
5249
switch (args[0]) {
5350
case "page":
@@ -66,8 +63,8 @@ public void onCommand(DiscordApi api, TextChannel channel, String[] args) throws
6663
searchString = String.join(" ", Arrays.copyOfRange(args, 0, args.length)).toLowerCase();
6764
populatePages(api, embed, defaultSearch(searchString));
6865
}
69-
channel.sendMessage(embed).join();
7066
}
67+
channel.sendMessage(embed).join();
7168
} catch (Throwable t) {
7269
channel.sendMessage("Something went wrong: ```" + ExceptionLogger.unwrapThrowable(t).getMessage() + "```")
7370
.join();

src/main/java/org/javacord/bot/util/javadoc/parser/JavadocParser.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ public static CompletableFuture<String> getLatestJavaDocs(DiscordApi api) {
4949
.url("https://docs.javacord.org/api/")
5050
.build();
5151

52-
Response response = client.newCall(request).execute();
53-
return response.request().url().toString();
52+
try (Response response = client.newCall(request).execute()) {
53+
return response.request().url().toString();
54+
}
5455
} catch (Exception e) {
5556
throw new CompletionException(e);
5657
}
@@ -99,15 +100,13 @@ private Set<JavadocMethod> getMethodsBlocking() throws IOException {
99100
.build();
100101

101102
Response response = client.newCall(request).execute();
102-
ResponseBody body = response.body();
103-
Set<JavadocMethod> methods = new HashSet<>();
104-
if (body == null) {
103+
try (ResponseBody body = response.body()) {
104+
Set<JavadocMethod> methods = new HashSet<>();
105+
for (JsonNode node : mapper.readTree(body.string().replaceFirst("memberSearchIndex = ", ""))) {
106+
methods.add(new JavadocMethod(url, node));
107+
}
105108
return methods;
106109
}
107-
for (JsonNode node : mapper.readTree(body.string().replaceFirst("memberSearchIndex = ", ""))) {
108-
methods.add(new JavadocMethod(url, node));
109-
}
110-
return methods;
111110
}
112111

113112
/**
@@ -122,15 +121,13 @@ private Set<JavadocClass> getClassesBlocking() throws IOException {
122121
.build();
123122

124123
Response response = client.newCall(request).execute();
125-
ResponseBody body = response.body();
126-
Set<JavadocClass> classes = new HashSet<>();
127-
if (body == null) {
124+
try (ResponseBody body = response.body()) {
125+
Set<JavadocClass> classes = new HashSet<>();
126+
for (JsonNode node : mapper.readTree(body.string().replaceFirst("typeSearchIndex = ", ""))) {
127+
classes.add(new JavadocClass(url, node));
128+
}
128129
return classes;
129130
}
130-
for (JsonNode node : mapper.readTree(body.string().replaceFirst("typeSearchIndex = ", ""))) {
131-
classes.add(new JavadocClass(url, node));
132-
}
133-
return classes;
134131
}
135132

136133
}

src/main/java/org/javacord/bot/util/wiki/parser/WikiParser.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -75,28 +75,26 @@ public Set<WikiPage> getPagesBlocking() throws IOException {
7575
.build();
7676

7777
Response response = client.newCall(request).execute();
78-
ResponseBody body = response.body();
79-
Set<WikiPage> pages = new HashSet<>();
80-
if (body == null) {
81-
return pages;
82-
}
83-
JsonNode array = mapper.readTree(body.charStream());
84-
if (!array.isArray()) {
85-
throw new AssertionError("Format of wiki page list not as expected");
86-
}
87-
for (JsonNode node : array) {
88-
if (node.has("title") && node.has("keywords") && node.has("url") && node.has("content")) {
89-
pages.add(new WikiPage(
90-
node.get("title").asText(),
91-
asStringArray(node.get("keywords")),
92-
node.get("url").asText(),
93-
node.get("content").asText()
94-
));
95-
} else {
78+
try (ResponseBody body = response.body()) {
79+
Set<WikiPage> pages = new HashSet<>();
80+
JsonNode array = mapper.readTree(body.charStream());
81+
if (!array.isArray()) {
9682
throw new AssertionError("Format of wiki page list not as expected");
9783
}
84+
for (JsonNode node : array) {
85+
if (node.has("title") && node.has("keywords") && node.has("url") && node.has("content")) {
86+
pages.add(new WikiPage(
87+
node.get("title").asText(),
88+
asStringArray(node.get("keywords")),
89+
node.get("url").asText(),
90+
node.get("content").asText()
91+
));
92+
} else {
93+
throw new AssertionError("Format of wiki page list not as expected");
94+
}
95+
}
96+
return pages;
9897
}
99-
return pages;
10098
}
10199

102100
private String[] asStringArray(JsonNode arrayNode) {

0 commit comments

Comments
 (0)