diff --git a/pom.xml b/pom.xml index 2936f24dc..573697cc2 100644 --- a/pom.xml +++ b/pom.xml @@ -242,7 +242,7 @@ rulesets/java/imports.xml - + rulesets/java/logging-jakarta-commons.xml rulesets/java/migrating.xml diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/Checker.java b/src/main/java/com/puppycrawl/tools/checkstyle/Checker.java index dafe706f3..69d4a395b 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/Checker.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/Checker.java @@ -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(), diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/PropertyCacheFile.java b/src/main/java/com/puppycrawl/tools/checkstyle/PropertyCacheFile.java index c1330db24..c67fe7b98 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/PropertyCacheFile.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/PropertyCacheFile.java @@ -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(); } } diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java b/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java index a4353662e..f0cabb332 100755 --- a/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java @@ -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 TreeWalker 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); diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/Utils.java b/src/main/java/com/puppycrawl/tools/checkstyle/Utils.java index c623a2826..ef1227113 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/Utils.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/Utils.java @@ -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 FileSetCheck - * work (debug() 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. diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java index 4e26856f4..aa891bd39 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java @@ -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 propertyFiles = Lists.newArrayList(); @@ -230,7 +235,7 @@ public class TranslationCheck final SortedSet messages = Sets.newTreeSet(); messages.add(message); getMessageDispatcher().fireErrors(file.getPath(), messages); - Utils.getExceptionLogger().debug("IOException occured.", ex); + LOG.debug("IOException occured.", ex); } diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/HandlerFactory.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/HandlerFactory.java index ec5d1c518..0f5e9ee9b 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/HandlerFactory.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/HandlerFactory.java @@ -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());