PMD fixes.

This commit is contained in:
Erik C. Thauvin 2020-02-10 23:46:37 -08:00
parent 12a1655d3f
commit 4877c7ab12
9 changed files with 73 additions and 57 deletions

View file

@ -5,22 +5,27 @@
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>Erik's Ruleset</description>
<!-- BEST PRACTICES -->
<rule ref="category/java/bestpractices.xml/AvoidStringBufferField"/>
<!-- <rule ref="category/java/bestpractices.xml/AvoidStringBufferField"/>-->
<rule ref="category/java/bestpractices.xml/AvoidUsingHardCodedIP"/>
<rule ref="category/java/bestpractices.xml/CheckResultSet"/>
<rule ref="category/java/bestpractices.xml/ConstantsInInterface"/>
<rule ref="category/java/bestpractices.xml/DefaultLabelNotLastInSwitchStmt"/>
<rule ref="category/java/bestpractices.xml/ForLoopCanBeForeach"/>
<rule ref="category/java/bestpractices.xml/GuardLogStatement"/>
<rule ref="category/java/bestpractices.xml/LooseCoupling"/>
<!-- <rule ref="category/java/bestpractices.xml/LooseCoupling"/> -->
<rule ref="category/java/bestpractices.xml/MethodReturnsInternalArray"/>
<rule ref="category/java/bestpractices.xml/MissingOverride"/>
<rule ref="category/java/bestpractices.xml/MissingOverride">
<properties>
<property name="violationSuppressXPath"
value="./MethodDeclarator[@Image='hashCode' or @Image='equals' or @Image='toString']"/>
</properties>
</rule>
<rule ref="category/java/bestpractices.xml/PositionLiteralsFirstInCaseInsensitiveComparisons"/>
<rule ref="category/java/bestpractices.xml/PositionLiteralsFirstInComparisons"/>
<rule ref="category/java/bestpractices.xml/PreserveStackTrace"/>
<rule ref="category/java/bestpractices.xml/ReplaceEnumerationWithIterator"/>
<rule ref="category/java/bestpractices.xml/ReplaceHashtableWithMap"/>
<rule ref="category/java/bestpractices.xml/ReplaceVectorWithList"/>
<!-- <rule ref="category/java/bestpractices.xml/ReplaceHashtableWithMap"/>
<rule ref="category/java/bestpractices.xml/ReplaceVectorWithList"/> -->
<rule ref="category/java/bestpractices.xml/SwitchStmtsShouldHaveDefault"/>
<rule ref="category/java/bestpractices.xml/SystemPrintln"/>
<rule ref="category/java/bestpractices.xml/UnusedFormalParameter"/>
@ -41,7 +46,7 @@
<rule ref="category/java/codestyle.xml/GenericsNaming"/>
<rule ref="category/java/codestyle.xml/LocalVariableNamingConventions"/>
<rule ref="category/java/codestyle.xml/MethodNamingConventions"/>
<rule ref="category/java/codestyle.xml/PackageCase"/>
<!-- <rule ref="category/java/codestyle.xml/PackageCase"/>-->
<!-- OTHER -->
@ -84,7 +89,7 @@
<rule ref="category/java/design.xml/DataClass"/>
<rule ref="category/java/design.xml/DoNotExtendJavaLangError"/>
<rule ref="category/java/design.xml/ExceptionAsFlowControl"/>
<rule ref="category/java/design.xml/ExcessivePublicCount"/>
<!-- <rule ref="category/java/design.xml/ExcessivePublicCount"/>-->
<rule ref="category/java/design.xml/FinalFieldCouldBeStatic"/>
<rule ref="category/java/design.xml/ImmutableField"/>
<rule ref="category/java/design.xml/LogicInversion"/>
@ -108,6 +113,8 @@
<rule ref="category/java/errorprone.xml/AssignmentInOperand">
<properties>
<property name="allowWhile" value="true"/>
<property name="allowFor" value="true"/>
<property name="allowIf" value="true"/>
</properties>
</rule>
<rule ref="category/java/errorprone.xml/AssignmentToNonFinalStatic"/>
@ -117,7 +124,11 @@
<rule ref="category/java/errorprone.xml/AvoidCallingFinalize"/>
<rule ref="category/java/errorprone.xml/AvoidCatchingThrowable"/>
<rule ref="category/java/errorprone.xml/AvoidDecimalLiteralsInBigDecimalConstructor"/>
<rule ref="category/java/errorprone.xml/AvoidDuplicateLiterals"/>
<rule ref="category/java/errorprone.xml/AvoidDuplicateLiterals">
<properties>
<property name="skipAnnotations" value="true"/>
</properties>
</rule>
<rule ref="category/java/errorprone.xml/AvoidEnumAsIdentifier"/>
<rule ref="category/java/errorprone.xml/AvoidInstanceofChecksInCatchClause"/>
<rule ref="category/java/errorprone.xml/AvoidLosingExceptionInformation"/>
@ -150,7 +161,7 @@
<rule ref="category/java/errorprone.xml/IdempotentOperations"/>
<rule ref="category/java/errorprone.xml/ImportFromSamePackage"/>
<rule ref="category/java/errorprone.xml/InstantiationToGetClass"/>
<rule ref="category/java/errorprone.xml/InvalidSlf4jMessageFormat"/>
<rule ref="category/java/errorprone.xml/InvalidLogMessageFormat"/>
<rule ref="category/java/errorprone.xml/JumbledIncrementer"/>
<rule ref="category/java/errorprone.xml/JUnitSpelling"/>
<rule ref="category/java/errorprone.xml/JUnitStaticSuite"/>
@ -162,7 +173,7 @@
<rule ref="category/java/errorprone.xml/MoreThanOneLogger"/>
<rule ref="category/java/errorprone.xml/NonCaseLabelInSwitchStatement"/>
<rule ref="category/java/errorprone.xml/NonStaticInitializer"/>
<rule ref="category/java/errorprone.xml/NullAssignment"/>
<!-- <rule ref="category/java/errorprone.xml/NullAssignment"/>-->
<rule ref="category/java/errorprone.xml/OverrideBothEqualsAndHashcode"/>
<rule ref="category/java/errorprone.xml/ProperCloneImplementation"/>
<rule ref="category/java/errorprone.xml/ProperLogger"/>
@ -240,7 +251,7 @@
<rule ref="category/java/performance.xml/StringToString"/>
<rule ref="category/java/performance.xml/TooFewBranchesForASwitchStatement"/>
<rule ref="category/java/performance.xml/UnnecessaryWrapperObjectCreation"/>
<rule ref="category/java/performance.xml/UseArrayListInsteadOfVector"/>
<!-- <rule ref="category/java/performance.xml/UseArrayListInsteadOfVector"/> -->
<rule ref="category/java/performance.xml/UseArraysAsList"/>
<rule ref="category/java/performance.xml/UseIndexOfChar"/>
<rule ref="category/java/performance.xml/UselessStringValueOf"/>

View file

@ -710,11 +710,10 @@ public class Mobibot extends PircBot {
}
}
final StringBuilder info = new StringBuilder(28).append("Uptime: ");
final StringBuilder info = new StringBuilder(28);
info.append(Utils.uptime(ManagementFactory.getRuntimeMXBean().getUptime()));
info.append("[Entries: ").append(entries.size());
info.append("Uptime: ").append(Utils.uptime(ManagementFactory.getRuntimeMXBean().getUptime())).append(
" [Entries: ").append(entries.size());
if (tell.isEnabled() && isOp(sender)) {
info.append(", Messages: ").append(tell.size());
@ -769,7 +768,7 @@ public class Mobibot extends PircBot {
*/
@SuppressFBWarnings(
{"INFORMATION_EXPOSURE_THROUGH_AN_ERROR_MESSAGE", "DM_DEFAULT_ENCODING", "IOI_USE_OF_FILE_STREAM_CONSTRUCTORS"})
@SuppressWarnings({"PMD.SystemPrintln", "PMD.AvoidFileStream"})
@SuppressWarnings({"PMD.SystemPrintln", "PMD.AvoidFileStream", "PMD.CloseResource"})
public static void main(final String[] args) {
// Setup the command line options
final Options options = new Options();

View file

@ -45,7 +45,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
/**
* The class to handle posts to pinbard.in.
* The class to handle posts to pinboard.in.
*
* @author <a href="https://erik.thauvin.net" target="_blank">Erik C. Thauvin</a>
* @created 2017-05-17
@ -81,7 +81,7 @@ class Pinboard {
* @param entry The entry to add.
*/
final void addPost(final EntryLink entry) {
final SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() {
final SwingWorker<Boolean, Void> worker = new SwingWorker<>() {
@Override
protected Boolean doInBackground() {
return pinboard.addPin(entry.getLink(),
@ -103,7 +103,7 @@ class Pinboard {
final void deletePost(final EntryLink entry) {
final String link = entry.getLink();
final SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() {
final SwingWorker<Boolean, Void> worker = new SwingWorker<>() {
@Override
protected Boolean doInBackground() {
return pinboard.deletePin(link);
@ -140,7 +140,7 @@ class Pinboard {
* @param entry The entry to add.
*/
final void updatePost(final String oldUrl, final EntryLink entry) {
final SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() {
final SwingWorker<Boolean, Void> worker = new SwingWorker<>() {
@Override
protected Boolean doInBackground() {
if (!oldUrl.equals(entry.getLink())) {

View file

@ -34,6 +34,8 @@ public final class TwitterOAuth {
* Twitter OAuth Client Registration.
*
* @param args The consumerKey and consumerSecret should be passed as arguments.
* @throws TwitterException If an error occurs.
* @throws IOException If an IO error occurs.
*/
@SuppressFBWarnings({"DM_DEFAULT_ENCODING", "IMC_IMMATURE_CLASS_PRINTSTACKTRACE"})
@SuppressWarnings({"PMD.AvoidPrintStackTrace", "PMD.SystemPrintln"})
@ -43,7 +45,7 @@ public final class TwitterOAuth {
twitter.setOAuthConsumer(args[0], args[1]);
final RequestToken requestToken = twitter.getOAuthRequestToken();
AccessToken accessToken = null;
final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try (final BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
while (null == accessToken) {
System.out.println("Open the following URL and grant access to your account:");
System.out.println(requestToken.getAuthorizationURL());
@ -59,8 +61,7 @@ public final class TwitterOAuth {
System.out.println(
"Please add the following to the bot's property file:" + "\n\n" + "twitter-consumerKey="
+ args[0] + '\n' + "twitter-consumerSecret=" + args[1] + '\n' + "twitter-token="
+ accessToken.getToken() + '\n' + "twitter-tokenSecret=" + accessToken
.getTokenSecret());
+ accessToken.getToken() + '\n' + "twitter-tokenSecret=" + accessToken.getTokenSecret());
} catch (TwitterException te) {
if (401 == te.getStatusCode()) {
System.out.println("Unable to get the access token.");
@ -69,6 +70,7 @@ public final class TwitterOAuth {
}
}
}
}
} else {
System.out.println("Usage: " + TwitterOAuth.class.getName() + " <consumerKey> <consumerSecret>");
}

View file

@ -42,6 +42,7 @@ import java.time.LocalDateTime;
* @created Jan 31, 2004
* @since 1.0
*/
@SuppressWarnings({"PMD.DataClass"})
public class EntryComment implements Serializable {
// The serial version UID.
static final long serialVersionUID = 1L;

View file

@ -87,7 +87,6 @@ public final class Joke extends ThreadedModule {
final JSONObject json = new JSONObject(sb.toString());
//noinspection RegExpRedundantEscape
return new PublicMessage(
json.getJSONObject("value").get("joke").toString().replace("\\'", "'")
.replace("\\\"", "\""));

View file

@ -132,6 +132,7 @@ public final class StockQuote extends ThreadedModule {
* @return The {@link Message} array containing the stock quote.
* @throws ModuleException If an errors occurs.
*/
@SuppressWarnings({"PMD.CloseResource"})
static List<Message> getQuote(final String symbol, final String apiKey) throws ModuleException {
if (StringUtils.isBlank(apiKey)) {
throw new ModuleException(StringUtils.capitalize(STOCK_CMD) + " is disabled. The API key is missing.");

View file

@ -63,6 +63,7 @@ public class Message {
* @param isError The error flag.
* @param isPrivate The Private message
*/
@SuppressWarnings("unused")
public Message(final String message, final boolean isNotice, final boolean isError, final boolean isPrivate) {
msg = message;
this.isNotice = isNotice;
@ -80,6 +81,7 @@ public class Message {
* @param isPrivate The Private message
* @param color The color.
*/
@SuppressWarnings("unused")
public Message(final String message,
final boolean isNotice,
final boolean isError,
@ -178,6 +180,7 @@ public class Message {
*
* @param isPrivate The private flag.
*/
@SuppressWarnings("unused")
public void setPrivate(final boolean isPrivate) {
this.isPrivate = isPrivate;
}

View file

@ -59,7 +59,7 @@ public class Weather2Test extends LocalProperties {
messages = Weather2.getWeather("London, UK", LocalProperties.getProperty(Weather2.OWM_API_KEY_PROP));
assertThat(messages.get(0).getMessage()).as("is UK").contains("London").contains("UK");
assertThat(messages.get(messages.size() - 1).getMessage()).as("is City Code").endsWith("4298960");
assertThat(messages.get(messages.size() - 1).getMessage()).as("is City Code").endsWith("4517009");
assertThatThrownBy(
() -> Weather2.getWeather("Montpellier, FR", LocalProperties.getProperty(Weather2.OWM_API_KEY_PROP))).as(