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 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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,9 @@

import net.dv8tion.jda.api.Permission;

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

Expand Down Expand Up @@ -50,8 +51,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 List<String> aliases = new ArrayList<>();
private final List<Command> children = new ArrayList<>();
private BiConsumer<CommandEvent, Command> helpBiConsumer = null;
private boolean usesTopicTags = true;
private CooldownScope cooldownScope = CooldownScope.USER;
Expand Down Expand Up @@ -215,7 +216,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 +252,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 +491,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 +503,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 @@ -593,7 +593,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,7 @@

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

Expand Down Expand Up @@ -58,9 +58,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 List<Command> commands = new ArrayList<>();
private final List<SlashCommand> slashCommands = new ArrayList<>();
private final List<ContextMenu> contextMenus = new ArrayList<>();
private String forcedGuildId = null;
private boolean manualUpsert = false;
private CommandListener listener;
Expand All @@ -71,7 +71,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 +707,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 @@ -142,7 +142,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 @@ -151,7 +151,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 @@ -602,7 +602,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 @@ -37,7 +37,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 @@ -293,7 +292,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
50 changes: 24 additions & 26 deletions menu/src/main/java/com/jagrosh/jdautilities/menu/ButtonMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import net.dv8tion.jda.internal.utils.Checks;

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 @@ -115,34 +115,32 @@ 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().getEmoji().getName();
// If the reaction is an Emote we get the Snowflake,
// otherwise we get the unicode value.
String re = event.getReaction().getEmoji().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().getEmoji());
finalAction.accept(m);
}, timeout, unit, () -> finalAction.accept(m));
});
// Preform the specified action with the ReactionEmote
action.accept(event.getReaction().getEmoji());
finalAction.accept(m);
}, timeout, unit, () -> finalAction.accept(m)));
}
}
});
Expand Down Expand Up @@ -170,7 +168,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<Emoji> action;
private Consumer<Message> finalAction = (m) -> {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,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
26 changes: 12 additions & 14 deletions menu/src/main/java/com/jagrosh/jdautilities/menu/OrderedMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
*/
package com.jagrosh.jdautilities.menu;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import com.jagrosh.jdautilities.commons.waiter.EventWaiter;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.Permission;
Expand All @@ -35,15 +44,6 @@
import net.dv8tion.jda.api.utils.messages.MessageEditData;
import net.dv8tion.jda.internal.utils.Checks;

import java.awt.Color;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
* A {@link com.jagrosh.jdautilities.menu.Menu Menu} of ordered buttons signified
* by numbers or letters, each with a reaction linked to it for users to click.
Expand Down Expand Up @@ -251,9 +251,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().getEmoji().getName().equals(CANCEL))
cancel.accept(m);
Expand All @@ -275,7 +273,7 @@ private MessageEditData 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 @@ -351,7 +349,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
26 changes: 13 additions & 13 deletions menu/src/main/java/com/jagrosh/jdautilities/menu/Paginator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
*/
package com.jagrosh.jdautilities.menu;

import java.awt.Color;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.function.Consumer;

import com.jagrosh.jdautilities.commons.waiter.EventWaiter;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Message;
Expand All @@ -33,15 +42,6 @@
import net.dv8tion.jda.api.utils.messages.MessageEditData;
import net.dv8tion.jda.internal.utils.Checks;

import java.awt.Color;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.function.Consumer;

/**
* A {@link com.jagrosh.jdautilities.menu.Menu Menu} implementation that paginates a
* set of one or more text items across one or more pages.
Expand Down Expand Up @@ -291,9 +291,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 @@ -448,7 +448,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
Loading