spotbugs cleanups.

This commit is contained in:
Erik C. Thauvin 2019-04-20 05:20:42 -07:00
parent e5398d73ec
commit 7ff30c1ce8
16 changed files with 53 additions and 29 deletions

View file

@ -12,13 +12,19 @@
<Or> <Or>
<Bug pattern="PATH_TRAVERSAL_IN"/> <Bug pattern="PATH_TRAVERSAL_IN"/>
<Bug pattern="PATH_TRAVERSAL_OUT"/> <Bug pattern="PATH_TRAVERSAL_OUT"/>
</Or> </Or>
<Confidence value="2"/> <Confidence value="2"/>
</Match> </Match>
<Match> <Match>
<Class name="net.thauvin.erik.mobibot.Mobibot"/> <Or>
<Method name="main"/> <Class name="net.thauvin.erik.mobibot.Mobibot"/>
<Bug pattern="PATH_TRAVERSAL_OUT"/> <Class name="net.thauvin.erik.mobibot.Pinboard"/>
<Confidence value="1"/> <Class name="net.thauvin.erik.mobibot.FeedReader"/>
<Class name="net.thauvin.erik.mobibot.tell.Tell"/>
<Package name="net.thauvin.erik.mobibot.modules.*"/>
<Package name="net.thauvin.erik.mobibot.entries.*"/>
</Or>
<Bug pattern="FCCD_FIND_CLASS_CIRCULAR_DEPENDENCY"/>
</Match> </Match>
</FindBugsFilter> </FindBugsFilter>

View file

@ -33,7 +33,6 @@
package net.thauvin.erik.mobibot; package net.thauvin.erik.mobibot;
import com.rometools.rome.feed.synd.SyndEntry; import com.rometools.rome.feed.synd.SyndEntry;
import com.rometools.rome.feed.synd.SyndEntryImpl;
import com.rometools.rome.feed.synd.SyndFeed; import com.rometools.rome.feed.synd.SyndFeed;
import com.rometools.rome.io.SyndFeedInput; import com.rometools.rome.io.SyndFeedInput;
import com.rometools.rome.io.XmlReader; import com.rometools.rome.io.XmlReader;
@ -87,10 +86,10 @@ class FeedReader implements Runnable {
final SyndFeed feed = input.build(new XmlReader(new URL(url))); final SyndFeed feed = input.build(new XmlReader(new URL(url)));
SyndEntry item; SyndEntry item;
final List items = feed.getEntries(); final List<SyndEntry> items = feed.getEntries();
for (int i = 0; (i < items.size()) && (i < MAX_ITEMS); i++) { for (int i = 0; (i < items.size()) && (i < MAX_ITEMS); i++) {
item = (SyndEntryImpl) items.get(i); item = items.get(i);
bot.send(sender, item.getTitle()); bot.send(sender, item.getTitle());
bot.send(sender, TAB_INDENT + Utils.green(item.getLink())); bot.send(sender, TAB_INDENT + Utils.green(item.getLink()));
} }

View file

@ -33,7 +33,7 @@ public final class TwitterOAuth {
* *
* @param args The consumerKey and consumerSecret should be passed as arguments. * @param args The consumerKey and consumerSecret should be passed as arguments.
*/ */
@SuppressFBWarnings(value = "DM_DEFAULT_ENCODING") @SuppressFBWarnings({"DM_DEFAULT_ENCODING", "IMC_IMMATURE_CLASS_PRINTSTACKTRACE"})
public static void main(final String[] args) public static void main(final String[] args)
throws Exception { throws Exception {
if (args.length == 2) { if (args.length == 2) {

View file

@ -95,9 +95,9 @@ public final class Utils {
* @return The colorized string. * @return The colorized string.
*/ */
static String colorize(final String s, final String color) { static String colorize(final String s, final String color) {
if (!Utils.isValidString(color) || color.equals(Colors.NORMAL)) { if (!Utils.isValidString(color) || Colors.NORMAL.equals(color)) {
return s; return s;
} else if (color.equals(Colors.BOLD) || color.equals(Colors.REVERSE)) { } else if (Colors.BOLD.equals(color) || Colors.REVERSE.equals(color)) {
return color + s + color; return color + s + color;
} }
@ -247,14 +247,12 @@ public final class Utils {
* @return The unescaped string. * @return The unescaped string.
*/ */
public static String unescapeXml(final String str) { public static String unescapeXml(final String str) {
String s = str.replaceAll("&amp;", "&"); return str.replaceAll("&amp;", "&")
s = s.replaceAll("&lt;", "<"); .replaceAll("&lt;", "<")
s = s.replaceAll("&gt;", ">"); .replaceAll("&gt;", ">")
s = s.replaceAll("&quot;", "\""); .replaceAll("&quot;", "\"")
s = s.replaceAll("&apos;", "'"); .replaceAll("&apos;", "'")
s = s.replaceAll("&#39;", "'"); .replaceAll("&#39;", "'");
return s;
} }
/** /**

View file

@ -32,6 +32,7 @@
package net.thauvin.erik.mobibot.entries; package net.thauvin.erik.mobibot.entries;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import net.thauvin.erik.mobibot.Commands; import net.thauvin.erik.mobibot.Commands;
import net.thauvin.erik.mobibot.Constants; import net.thauvin.erik.mobibot.Constants;
import net.thauvin.erik.mobibot.Utils; import net.thauvin.erik.mobibot.Utils;
@ -84,6 +85,7 @@ public final class EntriesUtils {
* @param isView Set to true to display the number of comments. * @param isView Set to true to display the number of comments.
* @return The entry's link. * @return The entry's link.
*/ */
@SuppressFBWarnings(value = "CE_CLASS_ENVY", justification = "Yes, it does.")
public static String buildLink(final int index, final EntryLink entry, final boolean isView) { public static String buildLink(final int index, final EntryLink entry, final boolean isView) {
final StringBuilder buff = new StringBuilder(Commands.LINK_CMD + (index + 1) + ": "); final StringBuilder buff = new StringBuilder(Commands.LINK_CMD + (index + 1) + ": ");

View file

@ -34,6 +34,7 @@ package net.thauvin.erik.mobibot.entries;
import com.rometools.rome.feed.synd.SyndCategory; import com.rometools.rome.feed.synd.SyndCategory;
import com.rometools.rome.feed.synd.SyndCategoryImpl; import com.rometools.rome.feed.synd.SyndCategoryImpl;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.Serializable; import java.io.Serializable;
import java.util.Calendar; import java.util.Calendar;
@ -291,6 +292,7 @@ public class EntryLink implements Serializable {
* *
* @param tags The space-delimited tags. * @param tags The space-delimited tags.
*/ */
@SuppressFBWarnings(value = "STT_STRING_PARSING_A_FIELD")
public final void setTags(final String tags) { public final void setTags(final String tags) {
if (tags != null) { if (tags != null) {
final String[] parts = tags.replaceAll(", ", " ").replaceAll(",", " ").split(" "); final String[] parts = tags.replaceAll(", ", " ").replaceAll(",", " ").split(" ");
@ -310,7 +312,7 @@ public class EntryLink implements Serializable {
if (mod == '-') { if (mod == '-') {
// Don't remove the channel tag, if any. // Don't remove the channel tag, if any.
if (!tag.getName().equals(channel.substring(1))) { if (!channel.substring(1).equals(tag.getName())) {
this.tags.remove(tag); this.tags.remove(tag);
} }
} else if (mod == '+') { } else if (mod == '+') {
@ -334,7 +336,7 @@ public class EntryLink implements Serializable {
* *
* @param tags The tags. * @param tags The tags.
*/ */
private void setTags(final List<SyndCategory> tags) { final void setTags(final List<SyndCategory> tags) {
this.tags.addAll(tags); this.tags.addAll(tags);
} }

View file

@ -130,7 +130,7 @@ public final class CurrencyConverter extends ThreadedModule {
final String[] cmds = query.split(" "); final String[] cmds = query.split(" ");
if (cmds.length == 4) { if (cmds.length == 4) {
if (cmds[3].equals(cmds[1]) || cmds[0].equals("0")) { if (cmds[3].equals(cmds[1]) || "0".equals(cmds[0])) {
return new ErrorMessage("You're kidding, right?"); return new ErrorMessage("You're kidding, right?");
} else { } else {
try { try {
@ -153,7 +153,7 @@ public final class CurrencyConverter extends ThreadedModule {
"The supported currencies are: " + EXCHANGE_RATES.keySet().toString(), e); "The supported currencies are: " + EXCHANGE_RATES.keySet().toString(), e);
} }
} }
} else if (query.equals(CURRENCY_RATES_KEYWORD)) { } else if (CURRENCY_RATES_KEYWORD.equals(query)) {
final StringBuilder buff = new StringBuilder('[' + pubDate + "]: "); final StringBuilder buff = new StringBuilder('[' + pubDate + "]: ");

View file

@ -107,7 +107,7 @@ public final class Joke extends ThreadedModule {
/** /**
* Returns a random joke from <a href="http://www.icndb.com/">The Internet Chuck Norris Database</a>. * Returns a random joke from <a href="http://www.icndb.com/">The Internet Chuck Norris Database</a>.
*/ */
void run(final Mobibot bot, final String sender, String arg) { void run(final Mobibot bot, final String sender, final String arg) {
try { try {
bot.send(Utils.cyan(randomJoke().getMessage())); bot.send(Utils.cyan(randomJoke().getMessage()));
} catch (ModuleException e) { } catch (ModuleException e) {

View file

@ -178,7 +178,7 @@ public class Message {
* *
* @param isPrivate The private flag. * @param isPrivate The private flag.
*/ */
public void setPrivate(boolean isPrivate) { public void setPrivate(final boolean isPrivate) {
this.isPrivate = isPrivate; this.isPrivate = isPrivate;
} }
} }

View file

@ -32,6 +32,7 @@
package net.thauvin.erik.mobibot.tell; package net.thauvin.erik.mobibot.tell;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import net.thauvin.erik.mobibot.Commands; import net.thauvin.erik.mobibot.Commands;
import net.thauvin.erik.mobibot.Mobibot; import net.thauvin.erik.mobibot.Mobibot;
import net.thauvin.erik.mobibot.Utils; import net.thauvin.erik.mobibot.Utils;
@ -100,7 +101,8 @@ public class Tell {
* *
* @return <code>true</code> if the queue was cleaned. * @return <code>true</code> if the queue was cleaned.
*/ */
private boolean clean() { @SuppressWarnings("WeakerAccess")
final boolean clean() {
if (bot.getLogger().isDebugEnabled()) { if (bot.getLogger().isDebugEnabled()) {
bot.getLogger().debug("Cleaning the messages."); bot.getLogger().debug("Cleaning the messages.");
} }
@ -138,12 +140,13 @@ public class Tell {
* @param sender The sender's nick. * @param sender The sender's nick.
* @param cmds The commands string. * @param cmds The commands string.
*/ */
@SuppressFBWarnings(value = "CC_CYCLOMATIC_COMPLEXITY", justification = "Working on it.")
public void response(final String sender, final String cmds) { public void response(final String sender, final String cmds) {
final String arrow = " --> "; final String arrow = " --> ";
if (!Utils.isValidString(cmds)) { if (!Utils.isValidString(cmds)) {
helpResponse(sender); helpResponse(sender);
} else if (cmds.startsWith(Commands.VIEW_CMD)) { } else if (cmds.startsWith(Commands.VIEW_CMD)) {
if (bot.isOp(sender) && cmds.equals(Commands.VIEW_CMD + ' ' + TELL_ALL_KEYWORD)) { if (bot.isOp(sender) && (Commands.VIEW_CMD + ' ' + TELL_ALL_KEYWORD).equals(cmds)) {
if (!messages.isEmpty()) { if (!messages.isEmpty()) {
for (final TellMessage message : messages) { for (final TellMessage message : messages) {
bot.send(sender, Utils.bold(message.getSender()) + arrow + Utils.bold(message.getRecipient()) bot.send(sender, Utils.bold(message.getSender()) + arrow + Utils.bold(message.getRecipient())
@ -201,7 +204,7 @@ public class Tell {
final String id = split[1]; final String id = split[1];
boolean deleted = false; boolean deleted = false;
if (id.equalsIgnoreCase(TELL_ALL_KEYWORD)) { if (TELL_ALL_KEYWORD.equalsIgnoreCase(id)) {
for (final TellMessage message : messages) { for (final TellMessage message : messages) {
if (message.getSender().equalsIgnoreCase(sender) && message.isReceived()) { if (message.getSender().equalsIgnoreCase(sender) && message.isReceived()) {
messages.remove(message); messages.remove(message);
@ -272,7 +275,8 @@ public class Tell {
/** /**
* Saves the messages queue. * Saves the messages queue.
*/ */
private void save() { @SuppressWarnings("WeakerAccess")
final void save() {
TellMessagesMgr.save(serializedObject, messages, bot.getLogger()); TellMessagesMgr.save(serializedObject, messages, bot.getLogger());
} }

View file

@ -84,7 +84,7 @@ public class EntryLinkTest {
final List<SyndCategory> tags = entryLink.getTags(); final List<SyndCategory> tags = entryLink.getTags();
int i = 0; int i = 0;
for (SyndCategory tag : tags) { for (final SyndCategory tag : tags) {
assertThat(tag.getName()).as("tag.getName(" + i + ')').isEqualTo("tag" + (i + 1)); assertThat(tag.getName()).as("tag.getName(" + i + ')').isEqualTo("tag" + (i + 1));
i++; i++;
} }

View file

@ -32,6 +32,8 @@
package net.thauvin.erik.mobibot.modules; package net.thauvin.erik.mobibot.modules;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
@ -41,7 +43,9 @@ import static org.assertj.core.api.Assertions.assertThat;
* @created 2019-04-07 * @created 2019-04-07
* @since 1.0 * @since 1.0
*/ */
final class AbstractModuleTest { final class AbstractModuleTest {
@SuppressFBWarnings("CE_CLASS_ENVY")
static void testAbstractModule(final AbstractModule module) { static void testAbstractModule(final AbstractModule module) {
final String name = module.getClass().getName(); final String name = module.getClass().getName();

View file

@ -32,6 +32,7 @@
package net.thauvin.erik.mobibot.modules; package net.thauvin.erik.mobibot.modules;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import net.thauvin.erik.mobibot.msg.Message; import net.thauvin.erik.mobibot.msg.Message;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -52,6 +53,7 @@ public class GoogleSearchTest extends LocalProperties {
AbstractModuleTest.testAbstractModule(new GoogleSearch()); AbstractModuleTest.testAbstractModule(new GoogleSearch());
} }
@SuppressFBWarnings("LEST_LOST_EXCEPTION_STACK_TRACE")
@Test @Test
public void testSearchGoogle() throws ModuleException { public void testSearchGoogle() throws ModuleException {
final String apiKey = LocalProperties.getProperty(GoogleSearch.GOOGLE_API_KEY_PROP); final String apiKey = LocalProperties.getProperty(GoogleSearch.GOOGLE_API_KEY_PROP);

View file

@ -32,6 +32,7 @@
package net.thauvin.erik.mobibot.modules; package net.thauvin.erik.mobibot.modules;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import net.thauvin.erik.mobibot.msg.Message; import net.thauvin.erik.mobibot.msg.Message;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -46,7 +47,9 @@ import static org.assertj.core.api.Assertions.assertThat;
* @created 2019-04-07 * @created 2019-04-07
* @since 1.0 * @since 1.0
*/ */
public class StockQuoteTest extends LocalProperties { public class StockQuoteTest extends LocalProperties {
@SuppressFBWarnings("LEST_LOST_EXCEPTION_STACK_TRACE")
@Test @Test
public void testGetQuote() throws ModuleException { public void testGetQuote() throws ModuleException {
final String apiKey = LocalProperties.getProperty(StockQuote.ALPHAVANTAGE_API_KEY_PROP); final String apiKey = LocalProperties.getProperty(StockQuote.ALPHAVANTAGE_API_KEY_PROP);

View file

@ -32,6 +32,7 @@
package net.thauvin.erik.mobibot.modules; package net.thauvin.erik.mobibot.modules;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import net.thauvin.erik.mobibot.Constants; import net.thauvin.erik.mobibot.Constants;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -48,6 +49,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @since 1.0 * @since 1.0
*/ */
public class TwitterTest { public class TwitterTest {
@SuppressFBWarnings("MDM")
private String getCi() { private String getCi() {
if ("true".equals(System.getenv("CIRCLECI"))) { if ("true".equals(System.getenv("CIRCLECI"))) {
return "CircleCI"; return "CircleCI";

View file

@ -32,6 +32,7 @@
package net.thauvin.erik.mobibot.modules; package net.thauvin.erik.mobibot.modules;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import net.thauvin.erik.mobibot.msg.Message; import net.thauvin.erik.mobibot.msg.Message;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -47,6 +48,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @since 1.0 * @since 1.0
*/ */
public class Weather2Test extends LocalProperties { public class Weather2Test extends LocalProperties {
@SuppressFBWarnings("PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS")
@Test @Test
public void testWeather() throws ModuleException { public void testWeather() throws ModuleException {
ArrayList<Message> messages = Weather2.getWeather("98204", ArrayList<Message> messages = Weather2.getWeather("98204",