Fix PMD violations from 'logging-jakarta' ruleset, issue #871

This commit is contained in:
Michal Kordas 2015-04-09 22:45:19 +02:00 committed by Roman Ivanov
parent efc6142fb0
commit ee518dfe86
7 changed files with 42 additions and 61 deletions

View File

@ -242,7 +242,7 @@
<ruleset>rulesets/java/imports.xml</ruleset>
<!--<ruleset>rulesets/java/javabeans.xml</ruleset>-->
<!--<ruleset>rulesets/java/junit.xml</ruleset>-->
<!--<ruleset>rulesets/java/logging-jakarta-commons.xml</ruleset>-->
<ruleset>rulesets/java/logging-jakarta-commons.xml</ruleset>
<!--<ruleset>rulesets/java/logging-java.xml</ruleset>-->
<ruleset>rulesets/java/migrating.xml</ruleset>
<!--<ruleset>rulesets/java/naming.xml</ruleset>-->

View File

@ -34,6 +34,8 @@ import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
import com.puppycrawl.tools.checkstyle.api.MessageDispatcher;
import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
import com.puppycrawl.tools.checkstyle.api.SeverityLevelCounter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.File;
import java.io.FileNotFoundException;
@ -55,6 +57,9 @@ import static com.puppycrawl.tools.checkstyle.Utils.fileExtensionMatches;
*/
public class Checker extends AutomaticBean implements MessageDispatcher
{
/** Logger for Checker */
private static final Log LOG = LogFactory.getLog(Checker.class);
/** maintains error count */
private final SeverityLevelCounter counter = new SeverityLevelCounter(
SeverityLevel.ERROR);
@ -269,14 +274,13 @@ public class Checker extends AutomaticBean implements MessageDispatcher
}
}
catch (final FileNotFoundException fnfe) {
Utils.getExceptionLogger().debug(
"FileNotFoundException occured.", fnfe);
LOG.debug("FileNotFoundException occured.", fnfe);
fileMessages.add(new LocalizedMessage(0,
Defn.CHECKSTYLE_BUNDLE, "general.fileNotFound", null,
null, this.getClass(), null));
}
catch (final IOException ioe) {
Utils.getExceptionLogger().debug("IOException occured.", ioe);
LOG.debug("IOException occured.", ioe);
fileMessages.add(new LocalizedMessage(0,
Defn.CHECKSTYLE_BUNDLE, "general.exception",
new String[] {ioe.getMessage()}, null, this.getClass(),

View File

@ -33,6 +33,8 @@ import java.security.MessageDigest;
import com.google.common.io.Closeables;
import com.google.common.io.Flushables;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* This class maintains a persistent(on file-system) store of the files
@ -48,6 +50,9 @@ import com.puppycrawl.tools.checkstyle.api.Configuration;
*/
final class PropertyCacheFile
{
/** Logger for PropertyCacheFile */
private static final Log LOG = LogFactory.getLog(PropertyCacheFile.class);
/**
* The property key to use for storing the hashcode of the
* configuration. To avoid nameclashes with the files that are
@ -96,8 +101,7 @@ final class PropertyCacheFile
details.put(CONFIG_HASH_KEY, currentConfigHash);
}
catch (final IOException e) {
Utils.getExceptionLogger()
.debug("Unable to open cache file, ignoring.", e);
LOG.debug("Unable to open cache file, ignoring.", e);
}
finally {
Closeables.closeQuietly(inStream);
@ -116,8 +120,7 @@ final class PropertyCacheFile
details.store(out, null);
}
catch (final IOException e) {
Utils.getExceptionLogger()
.debug("Unable to save cache file.", e);
LOG.debug("Unable to save cache file.", e);
}
finally {
if (out != null) {
@ -138,8 +141,7 @@ final class PropertyCacheFile
Closeables.close(stream, false);
}
catch (final IOException ex) {
Utils.getExceptionLogger()
.debug("Unable to flush and close output stream.", ex);
LOG.debug("Unable to flush and close output stream.", ex);
}
}
@ -196,8 +198,7 @@ final class PropertyCacheFile
return hexEncode(md.digest());
}
catch (final Exception ex) { // IO, NoSuchAlgorithm
Utils.getExceptionLogger()
.debug("Unable to calculate hashcode.", ex);
LOG.debug("Unable to calculate hashcode.", ex);
return "ALWAYS FRESH: " + System.currentTimeMillis();
}
}

View File

@ -114,8 +114,7 @@ public final class TreeWalker
private ModuleFactory moduleFactory;
/** logger for debug purpose */
private static final Log LOG =
LogFactory.getLog("com.puppycrawl.tools.checkstyle.TreeWalker");
private static final Log LOG = LogFactory.getLog(TreeWalker.class);
/**
* Creates a new <code>TreeWalker</code> instance.
@ -217,7 +216,7 @@ public final class TreeWalker
catch (final TokenStreamRecognitionException tre) {
final String exceptionMsg = String.format(msg, "TokenStreamRecognitionException",
fileName);
Utils.getExceptionLogger().error(exceptionMsg);
LOG.error(exceptionMsg);
final RecognitionException re = tre.recog;
String message = "TokenStreamRecognitionException occured";
if (re != null) {
@ -228,7 +227,7 @@ public final class TreeWalker
// RecognitionException and any other (need to check if needed)
catch (Throwable ex) {
final String exceptionMsg = String.format(msg, ex.getClass().getSimpleName(), fileName);
Utils.getExceptionLogger().error(exceptionMsg);
LOG.error(exceptionMsg);
getMessageCollector().add(createLocalizedMessage(ex.getMessage()));
}
@ -317,12 +316,10 @@ public final class TreeWalker
tokenToCommentChecks.put(token, check);
}
else if (TokenTypes.isCommentType(token)) {
LOG.warn("Check '"
+ check.getClass().getName()
+ "' waits for comment type token ('"
+ token
+ "') and should override 'isCommentNodesRequred()'"
+ " method to return 'true'");
final String message = String.format("Check '%s' waits for comment type "
+ "token ('%s') and should override 'isCommentNodesRequred()' "
+ "method to return 'true'", check.getClass().getName(), token);
LOG.warn(message);
}
else {
tokenToOrdinaryChecks.put(token, check);

View File

@ -25,9 +25,6 @@ import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Contains utility methods.
@ -36,9 +33,6 @@ import org.apache.commons.logging.LogFactory;
*/
public final class Utils
{
/** Shared instance of logger for exception logging. */
private static final Log EXCEPTION_LOG =
LogFactory.getLog("com.puppycrawl.tools.checkstyle.ExceptionLog");
/** stop instances being created **/
private Utils()
@ -82,17 +76,6 @@ public final class Utils
return result;
}
/**
* Accessor for shared instance of logger which should be
* used to log all exceptions occurred during <code>FileSetCheck</code>
* work (<code>debug()</code> should be used).
* @return shared exception logger.
*/
public static Log getExceptionLogger()
{
return EXCEPTION_LOG;
}
/**
* Returns whether the specified string contains only whitespace up to the
* specified index.

View File

@ -26,7 +26,9 @@ import com.puppycrawl.tools.checkstyle.Defn;
import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
import com.puppycrawl.tools.checkstyle.api.MessageDispatcher;
import com.puppycrawl.tools.checkstyle.Utils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@ -78,6 +80,9 @@ public class TranslationCheck
*/
public static final String MSG_KEY = "translation.missingKey";
/** Logger for TranslationCheck */
private static final Log LOG = LogFactory.getLog(TranslationCheck.class);
/** The property files to process. */
private final List<File> propertyFiles = Lists.newArrayList();
@ -230,7 +235,7 @@ public class TranslationCheck
final SortedSet<LocalizedMessage> messages = Sets.newTreeSet();
messages.add(message);
getMessageDispatcher().fireErrors(file.getPath(), messages);
Utils.getExceptionLogger().debug("IOException occured.", ex);
LOG.debug("IOException occured.", ex);
}

View File

@ -62,14 +62,10 @@ public class HandlerFactory
});
typeHandlers.put(type, ctor);
}
catch (final NoSuchMethodException e) {
throw new RuntimeException("couldn't find ctor for "
+ handlerClass);
}
catch (final SecurityException e) {
LOG.debug("couldn't find ctor for " + handlerClass, e);
throw new RuntimeException("couldn't find ctor for "
+ handlerClass);
catch (final NoSuchMethodException | SecurityException e) {
final String message = "couldn't find ctor for " + handlerClass;
LOG.debug(message, e);
throw new RuntimeException(message);
}
}
@ -166,20 +162,15 @@ public class HandlerFactory
indentCheck, ast, parent);
}
}
catch (final InstantiationException e) {
LOG.debug("couldn't instantiate constructor for " + ast, e);
throw new RuntimeException("couldn't instantiate constructor for "
+ ast);
catch (final InstantiationException | InvocationTargetException e) {
final String message = "couldn't instantiate constructor for " + ast;
LOG.debug(message, e);
throw new RuntimeException(message);
}
catch (final IllegalAccessException e) {
LOG.debug("couldn't access constructor for " + ast, e);
throw new RuntimeException("couldn't access constructor for "
+ ast);
}
catch (final InvocationTargetException e) {
LOG.debug("couldn't instantiate constructor for " + ast, e);
throw new RuntimeException("couldn't instantiate constructor for "
+ ast);
final String message = "couldn't access constructor for " + ast;
LOG.debug(message, e);
throw new RuntimeException(message);
}
if (expHandler == null) {
throw new RuntimeException("no handler for type " + ast.getType());