Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs checked in SpotBugs #64

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ subprojects {
}

compileJava {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

source = sourcesForRelease.destinationDir
classpath = sourceSets.main.compileClasspath

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

import net.dv8tion.jda.api.Permission;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

Expand Down Expand Up @@ -50,8 +50,8 @@ public class CommandBuilder
private int cooldown = 0;
private Permission[] userPermissions = new Permission[0];
private Permission[] botPermissions = new Permission[0];
private final LinkedList<String> aliases = new LinkedList<>();
private final LinkedList<Command> children = new LinkedList<>();
private final ArrayList<String> aliases = new ArrayList<>();
private final ArrayList<Command> children = new ArrayList<>();
JellyBrick marked this conversation as resolved.
Show resolved Hide resolved
private BiConsumer<CommandEvent, Command> helpBiConsumer = null;
private boolean usesTopicTags = true;
private CooldownScope cooldownScope = CooldownScope.USER;
Expand Down Expand Up @@ -215,7 +215,7 @@ public CommandBuilder setUserPermissions(Collection<Permission> userPermissions)
if(userPermissions == null)
this.userPermissions = new Permission[0];
else
this.userPermissions = (Permission[]) userPermissions.toArray();
this.userPermissions = userPermissions.toArray(new Permission[0]);
return this;
}

Expand Down Expand Up @@ -251,7 +251,7 @@ public CommandBuilder setBotPermissions(Collection<Permission> botPermissions)
if(botPermissions == null)
this.botPermissions = new Permission[0];
else
this.botPermissions = (Permission[]) botPermissions.toArray();
this.botPermissions = botPermissions.toArray(new Permission[0]);
return this;
}

Expand Down Expand Up @@ -490,8 +490,8 @@ public Command build(BiConsumer<Command,CommandEvent> execution)
{
return new BlankCommand(name, help, category, arguments,
guildOnly, requiredRole, ownerCommand, cooldown,
userPermissions, botPermissions, aliases.toArray(new String[aliases.size()]),
children.toArray(new Command[children.size()]), helpBiConsumer, usesTopicTags,
userPermissions, botPermissions, aliases.toArray(new String[0]),
children.toArray(new Command[0]), helpBiConsumer, usesTopicTags,
cooldownScope, hidden)
{
@Override
Expand All @@ -502,7 +502,7 @@ protected void execute(CommandEvent event)
};
}

private abstract class BlankCommand extends Command
private abstract static class BlankCommand extends Command
{
BlankCommand(String name, String help, Category category,
String arguments, boolean guildOnly, String requiredRole,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ public interface CommandClient
*
* @return The GuildSettingsManager, or {@code null} if one was not provided when building this CommandClient.
*/
<M extends GuildSettingsManager> M getSettingsManager();
<M extends GuildSettingsManager<?>> M getSettingsManager();

/**
* Shuts down internals of the Command Client, such as the threadpool and guild settings manager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.function.BiFunction;
import java.util.function.Consumer;

Expand Down Expand Up @@ -58,9 +57,9 @@ public class CommandClientBuilder
private String error;
private String carbonKey;
private String botsKey;
private final LinkedList<Command> commands = new LinkedList<>();
private final LinkedList<SlashCommand> slashCommands = new LinkedList<>();
private final LinkedList<ContextMenu> contextMenus = new LinkedList<>();
private final ArrayList<Command> commands = new ArrayList<>();
private final ArrayList<SlashCommand> slashCommands = new ArrayList<>();
private final ArrayList<ContextMenu> contextMenus = new ArrayList<>();
JellyBrick marked this conversation as resolved.
Show resolved Hide resolved
private String forcedGuildId = null;
private boolean manualUpsert = false;
private CommandListener listener;
Expand All @@ -71,7 +70,7 @@ public class CommandClientBuilder
private ScheduledExecutorService executor;
private int linkedCacheSize = 0;
private AnnotatedModuleCompiler compiler = new AnnotatedModuleCompilerImpl();
private GuildSettingsManager manager = null;
private GuildSettingsManager<?> manager = null;

/**
* Builds a {@link com.jagrosh.jdautilities.command.impl.CommandClientImpl CommandClientImpl}
Expand Down Expand Up @@ -707,7 +706,7 @@ public CommandClientBuilder setLinkedCacheSize(int linkedCacheSize)
*
* @return This builder
*/
public CommandClientBuilder setGuildSettingsManager(GuildSettingsManager manager)
public CommandClientBuilder setGuildSettingsManager(GuildSettingsManager<?> manager)
{
this.manager = manager;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.jagrosh.jdautilities.command;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.ChannelType;
import net.dv8tion.jda.api.entities.GuildChannel;
import net.dv8tion.jda.api.entities.IMentionable;
Expand All @@ -26,7 +25,6 @@
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public class CommandClientImpl implements CommandClient, EventListener
private final String helpWord;
private final ScheduledExecutorService executor;
private final AnnotatedModuleCompiler compiler;
private final GuildSettingsManager manager;
private final GuildSettingsManager<?> manager;

private String textPrefix;
private CommandListener listener = null;
Expand All @@ -152,7 +152,7 @@ public class CommandClientImpl implements CommandClient, EventListener
public CommandClientImpl(String ownerId, String[] coOwnerIds, String prefix, String altprefix, String[] prefixes, Function<MessageReceivedEvent, String> prefixFunction, Function<MessageReceivedEvent, Boolean> commandPreProcessFunction, BiFunction<MessageReceivedEvent, Command, Boolean> commandPreProcessBiFunction, Activity activity, OnlineStatus status, String serverInvite,
String success, String warning, String error, String carbonKey, String botsKey, ArrayList<Command> commands, ArrayList<SlashCommand> slashCommands, ArrayList<ContextMenu> contextMenus, String forcedGuildId, boolean manualUpsert,
boolean useHelp, boolean shutdownAutomatically, Consumer<CommandEvent> helpConsumer, String helpWord, ScheduledExecutorService executor,
int linkedCacheSize, AnnotatedModuleCompiler compiler, GuildSettingsManager manager)
int linkedCacheSize, AnnotatedModuleCompiler compiler, GuildSettingsManager<?> manager)
{
Checks.check(ownerId != null, "Owner ID was set null or not set! Please provide an User ID to register as the owner!");

Expand Down Expand Up @@ -603,7 +603,7 @@ public <S> S getSettingsFor(Guild guild)

@SuppressWarnings("unchecked")
@Override
public <M extends GuildSettingsManager> M getSettingsManager()
public <M extends GuildSettingsManager<?>> M getSettingsManager()
{
return (M) manager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public <T extends Event> void waitForEvent(Class<T> classType, Predicate<T> cond
@SuppressWarnings("unchecked")
public final void onEvent(GenericEvent event)
{
Class c = event.getClass();
Class<?> c = event.getClass();

// Runs at least once for the fired Event, at most
// once for each superclass (excluding Object) because
Expand Down Expand Up @@ -275,7 +275,7 @@ public void shutdown()
threadpool.shutdown();
}

private class WaitingEvent<T extends GenericEvent>
private static class WaitingEvent<T extends GenericEvent>
{
final Predicate<T> condition;
final Consumer<T> action;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ else if(found.size()>1)

private static String listOfRoles(List<Role> list, String query)
{
String out = String.format("**Multiple roles found matching \"%s\":**", query);
StringBuilder out = new StringBuilder(String.format("**Multiple roles found matching \"%s\":**", query));
for(int i = 0; i < 6 && i < list.size(); i++)
out += "\n - " + list.get(i).getName() + " (ID:" + list.get(i).getId() + ")";
out.append("\n - ").append(list.get(i).getName()).append(" (ID:").append(list.get(i).getId()).append(")");
if(list.size() > 6)
out += "\n**And " + (list.size() - 6) + " more...**";
return out;
out.append("\n**And ").append(list.size() - 6).append(" more...**");
return out.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -234,9 +233,7 @@ private void handleButtonInteraction(ButtonInteractionEvent event, Message messa
}

int n = newPageNum;
event.deferEdit().queue(interactionHook -> {
message.editMessage(renderPage(n)).setActionRow(buildButtons()).queue(m -> pagination(m, n));
});
event.deferEdit().queue(interactionHook -> message.editMessage(renderPage(n)).setActionRow(buildButtons()).queue(m -> pagination(m, n)));
}

private Message renderPage(int pageNum) {
Expand All @@ -259,7 +256,7 @@ public static class Builder extends Menu.Builder<Builder, ButtonEmbedPaginator>
private boolean wrapPageEnds = false;
private ButtonStyle style = ButtonStyle.SECONDARY;

private final List<MessageEmbed> embeds = new LinkedList<>();
private final List<MessageEmbed> embeds = new ArrayList<>();

/**
* Builds the {@link ButtonEmbedPaginator} with this Builder.
Expand Down
54 changes: 26 additions & 28 deletions menu/src/main/java/com/jagrosh/jdautilities/menu/ButtonMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.jagrosh.jdautilities.menu;

import java.awt.Color;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -112,36 +112,34 @@ private void initialize(RestAction<Message> ra)
else
{
// This is the last reaction added.
r.queue(v -> {
waiter.waitForEvent(MessageReactionAddEvent.class, event -> {
// If the message is not the same as the ButtonMenu
// currently being displayed.
if(!event.getMessageId().equals(m.getId()))
return false;
r.queue(v -> waiter.waitForEvent(MessageReactionAddEvent.class, event -> {
// If the message is not the same as the ButtonMenu
// currently being displayed.
if(!event.getMessageId().equals(m.getId()))
return false;

// If the reaction is an Emote we get the Snowflake,
// otherwise we get the unicode value.
String re = event.getReaction().getReactionEmote().isEmote()
? event.getReaction().getReactionEmote().getId()
: event.getReaction().getReactionEmote().getName();
// If the reaction is an Emote we get the Snowflake,
// otherwise we get the unicode value.
String re = event.getReaction().getReactionEmote().isEmote()
? event.getReaction().getReactionEmote().getId()
: event.getReaction().getReactionEmote().getName();

// If the value we got is not registered as a button to
// the ButtonMenu being displayed we return false.
if(!choices.contains(re))
return false;
// If the value we got is not registered as a button to
// the ButtonMenu being displayed we return false.
if(!choices.contains(re))
return false;

// Last check is that the person who added the reaction
// is a valid user.
return isValidUser(event.getUser(), event.isFromGuild() ? event.getGuild() : null);
}, (MessageReactionAddEvent event) -> {
// What happens next is after a valid event
// is fired and processed above.
// Last check is that the person who added the reaction
// is a valid user.
return isValidUser(event.getUser(), event.isFromGuild() ? event.getGuild() : null);
}, (MessageReactionAddEvent event) -> {
// What happens next is after a valid event
// is fired and processed above.

// Preform the specified action with the ReactionEmote
action.accept(event.getReaction().getReactionEmote());
finalAction.accept(m);
}, timeout, unit, () -> finalAction.accept(m));
});
// Preform the specified action with the ReactionEmote
action.accept(event.getReaction().getReactionEmote());
finalAction.accept(m);
}, timeout, unit, () -> finalAction.accept(m)));
}
}
});
Expand Down Expand Up @@ -169,7 +167,7 @@ public static class Builder extends Menu.Builder<Builder, ButtonMenu>
private Color color;
private String text;
private String description;
private final List<String> choices = new LinkedList<>();
private final List<String> choices = new ArrayList<>();
private Consumer<ReactionEmote> action;
private Consumer<Message> finalAction = (m) -> {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ else if(event instanceof MessageReceivedEvent)
int pages = embeds.size();
final int targetPage;

if(leftText != null && rawContent.equalsIgnoreCase(leftText) && (1 < pageNum || wrapPageEnds))
targetPage = pageNum - 1 < 1 && wrapPageEnds ? pages : pageNum - 1;
else if(rightText != null && rawContent.equalsIgnoreCase(rightText) && (pageNum < pages || wrapPageEnds))
if(rawContent.equalsIgnoreCase(leftText) && (1 < pageNum || wrapPageEnds))
targetPage = pageNum - 1 < 1 ? pages : pageNum - 1;
else if(rawContent.equalsIgnoreCase(rightText) && (pageNum < pages || wrapPageEnds))
targetPage = pageNum + 1 > pages && wrapPageEnds ? 1 : pageNum + 1;
else
targetPage = Integer.parseInt(rawContent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.jagrosh.jdautilities.menu;

import java.awt.Color;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Set;
Expand Down Expand Up @@ -246,9 +246,7 @@ else if (e instanceof MessageReceivedEvent)
private void waitReactionOnly(Message m)
{
// This one is only for reactions
waiter.waitForEvent(MessageReactionAddEvent.class, e -> {
return isValidReaction(m, e);
}, e -> {
waiter.waitForEvent(MessageReactionAddEvent.class, e -> isValidReaction(m, e), e -> {
m.delete().queue();
if(e.getReaction().getReactionEmote().getName().equals(CANCEL))
cancel.accept(m);
Expand All @@ -270,7 +268,7 @@ private Message getMessage()
for(int i=0; i<choices.size(); i++)
sb.append("\n").append(getEmoji(i)).append(" ").append(choices.get(i));
mbuilder.setEmbeds(new EmbedBuilder().setColor(color)
.setDescription(description==null ? sb.toString() : description+sb.toString()).build());
.setDescription(description==null ? sb.toString() : description+ sb).build());
return mbuilder.build();
}

Expand Down Expand Up @@ -346,7 +344,7 @@ public static class Builder extends Menu.Builder<Builder, OrderedMenu>
private Color color;
private String text;
private String description;
private final List<String> choices = new LinkedList<>();
private final List<String> choices = new ArrayList<>();
private BiConsumer<Message, Integer> selection;
private Consumer<Message> cancel = (m) -> {};
private boolean useLetters = false;
Expand Down
10 changes: 5 additions & 5 deletions menu/src/main/java/com/jagrosh/jdautilities/menu/Paginator.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import java.awt.Color;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -256,9 +256,9 @@ else if(event instanceof MessageReceivedEvent)

final int targetPage;

if(leftText != null && rawContent.equalsIgnoreCase(leftText) && (1 < pageNum || wrapPageEnds))
targetPage = pageNum - 1 < 1 && wrapPageEnds? pages : pageNum - 1;
else if(rightText != null && rawContent.equalsIgnoreCase(rightText) && (pageNum < pages || wrapPageEnds))
if(rawContent.equalsIgnoreCase(leftText) && (1 < pageNum || wrapPageEnds))
targetPage = pageNum - 1 < 1 ? pages : pageNum - 1;
else if(rawContent.equalsIgnoreCase(rightText) && (pageNum < pages || wrapPageEnds))
targetPage = pageNum + 1 > pages && wrapPageEnds? 1 : pageNum + 1;
else
{
Expand Down Expand Up @@ -413,7 +413,7 @@ public static class Builder extends Menu.Builder<Builder, Paginator>
private String textToRight = null;
private boolean allowTextInput = false;

private final List<String> strings = new LinkedList<>();
private final List<String> strings = new ArrayList<>();

/**
* Builds the {@link com.jagrosh.jdautilities.menu.Paginator Paginator}
Expand Down
Loading