diff --git a/config/suppressions.xml b/config/suppressions.xml index 895057bf387..b0aaec12e16 100644 --- a/config/suppressions.xml +++ b/config/suppressions.xml @@ -13,10 +13,10 @@ See https://github.com/checkstyle/checkstyle/issues/2285--> + lines="247"/> + lines="252"/> packageNames = PackageNamesLoader - .getPackageNames(moduleClassLoader); - moduleFactory = new PackageObjectFactory(packageNames, - moduleClassLoader); - } - - final DefaultContext context = new DefaultContext(); - context.add("charset", charset); - context.add("classLoader", classLoader); - context.add("moduleFactory", moduleFactory); - context.add("severity", severityLevel.getName()); - context.add("basedir", basedir); - childContext = context; - } - - @Override - protected void setupChild(Configuration childConf) - throws CheckstyleException { - final String name = childConf.getName(); - final Object child; - - try { - child = moduleFactory.createModule(name); - - if (child instanceof AutomaticBean) { - final AutomaticBean bean = (AutomaticBean) child; - bean.contextualize(childContext); - bean.configure(childConf); - } - } - catch (final CheckstyleException ex) { - throw new CheckstyleException("cannot initialize module " + name - + " - " + ex.getMessage(), ex); - } - if (child instanceof FileSetCheck) { - final FileSetCheck fsc = (FileSetCheck) child; - fsc.init(); - addFileSetCheck(fsc); - } - else if (child instanceof Filter) { - final Filter filter = (Filter) child; - addFilter(filter); - } - else if (child instanceof AuditListener) { - final AuditListener listener = (AuditListener) child; - addListener(listener); - } - else { - throw new CheckstyleException(name - + " is not allowed as a child in Checker"); - } - } - - /** - * Adds a FileSetCheck to the list of FileSetChecks - * that is executed in process(). - * @param fileSetCheck the additional FileSetCheck - */ - public void addFileSetCheck(FileSetCheck fileSetCheck) { - fileSetCheck.setMessageDispatcher(this); - fileSetChecks.add(fileSetCheck); - } - - /** - * Adds a filter to the end of the audit event filter chain. - * @param filter the additional filter - */ - public void addFilter(Filter filter) { - filters.addFilter(filter); - } - /** * Removes filter. * @param filter filter to remove. @@ -239,14 +155,6 @@ public void destroy() { } } - /** - * Add the listener that will be used to receive events from the audit. - * @param listener the nosy thing - */ - public final void addListener(AuditListener listener) { - listeners.add(listener); - } - /** * Removes a given listener. * @param listener a listener to remove @@ -255,6 +163,14 @@ public void removeListener(AuditListener listener) { listeners.remove(listener); } + /** + * Sets base directory. + * @param basedir the base directory to strip off in file names + */ + public void setBasedir(String basedir) { + this.basedir = basedir; + } + /** * Processes a set of files with all FileSetChecks. * Once this is done, it is highly recommended to call for @@ -289,6 +205,22 @@ public int process(List files) throws CheckstyleException { return errorCount; } + /** Notify all listeners about the audit start. */ + private void fireAuditStarted() { + final AuditEvent event = new AuditEvent(this); + for (final AuditListener listener : listeners) { + listener.auditStarted(event); + } + } + + /** Notify all listeners about the audit end. */ + private void fireAuditFinished() { + final AuditEvent event = new AuditEvent(this); + for (final AuditListener listener : listeners) { + listener.auditFinished(event); + } + } + /** * Processes a list of files with all FileSetChecks. * @param files a list of files to process. @@ -315,7 +247,7 @@ private void processFiles(List files) throws CheckstyleException { catch (Exception ex) { // We need to catch all exceptions to put a reason failure (file name) in exception throw new CheckstyleException("Exception was thrown while processing " - + file.getPath(), ex); + + file.getPath(), ex); } catch (Error error) { // We need to catch all errors to put a reason failure (file name) in error @@ -341,36 +273,12 @@ private SortedSet processFile(File file) throws CheckstyleExce catch (final IOException ioe) { LOG.debug("IOException occurred.", ioe); fileMessages.add(new LocalizedMessage(0, - Definitions.CHECKSTYLE_BUNDLE, "general.exception", - new String[] {ioe.getMessage()}, null, getClass(), null)); + Definitions.CHECKSTYLE_BUNDLE, "general.exception", + new String[] {ioe.getMessage()}, null, getClass(), null)); } return fileMessages; } - /** - * Sets base directory. - * @param basedir the base directory to strip off in file names - */ - public void setBasedir(String basedir) { - this.basedir = basedir; - } - - /** Notify all listeners about the audit start. */ - private void fireAuditStarted() { - final AuditEvent event = new AuditEvent(this); - for (final AuditListener listener : listeners) { - listener.auditStarted(event); - } - } - - /** Notify all listeners about the audit end. */ - private void fireAuditFinished() { - final AuditEvent event = new AuditEvent(this); - for (final AuditListener listener : listeners) { - listener.auditFinished(event); - } - } - /** * Notify all listeners about the beginning of a file audit. * @@ -386,6 +294,25 @@ public void fireFileStarted(String fileName) { } } + /** + * Notify all listeners about the errors in a file. + * + * @param fileName the audited file + * @param errors the audit errors from the file + */ + @Override + public void fireErrors(String fileName, SortedSet errors) { + final String stripped = CommonUtils.relativizeAndNormalizePath(basedir, fileName); + for (final LocalizedMessage element : errors) { + final AuditEvent event = new AuditEvent(this, stripped, element); + if (filters.accept(event)) { + for (final AuditListener listener : listeners) { + listener.addError(event); + } + } + } + } + /** * Notify all listeners about the end of a file audit. * @@ -401,23 +328,96 @@ public void fireFileFinished(String fileName) { } } - /** - * Notify all listeners about the errors in a file. - * - * @param fileName the audited file - * @param errors the audit errors from the file - */ @Override - public void fireErrors(String fileName, SortedSet errors) { - final String stripped = CommonUtils.relativizeAndNormalizePath(basedir, fileName); - for (final LocalizedMessage element : errors) { - final AuditEvent event = new AuditEvent(this, stripped, element); - if (filters.accept(event)) { - for (final AuditListener listener : listeners) { - listener.addError(event); - } + public void finishLocalSetup() throws CheckstyleException { + final Locale locale = new Locale(localeLanguage, localeCountry); + LocalizedMessage.setLocale(locale); + + if (moduleFactory == null) { + + if (moduleClassLoader == null) { + throw new CheckstyleException( + "if no custom moduleFactory is set, " + + "moduleClassLoader must be specified"); } + + final Set packageNames = PackageNamesLoader + .getPackageNames(moduleClassLoader); + moduleFactory = new PackageObjectFactory(packageNames, + moduleClassLoader); } + + final DefaultContext context = new DefaultContext(); + context.add("charset", charset); + context.add("classLoader", classLoader); + context.add("moduleFactory", moduleFactory); + context.add("severity", severityLevel.getName()); + context.add("basedir", basedir); + childContext = context; + } + + @Override + protected void setupChild(Configuration childConf) + throws CheckstyleException { + final String name = childConf.getName(); + final Object child; + + try { + child = moduleFactory.createModule(name); + + if (child instanceof AutomaticBean) { + final AutomaticBean bean = (AutomaticBean) child; + bean.contextualize(childContext); + bean.configure(childConf); + } + } + catch (final CheckstyleException ex) { + throw new CheckstyleException("cannot initialize module " + name + + " - " + ex.getMessage(), ex); + } + if (child instanceof FileSetCheck) { + final FileSetCheck fsc = (FileSetCheck) child; + fsc.init(); + addFileSetCheck(fsc); + } + else if (child instanceof Filter) { + final Filter filter = (Filter) child; + addFilter(filter); + } + else if (child instanceof AuditListener) { + final AuditListener listener = (AuditListener) child; + addListener(listener); + } + else { + throw new CheckstyleException(name + + " is not allowed as a child in Checker"); + } + } + + /** + * Adds a FileSetCheck to the list of FileSetChecks + * that is executed in process(). + * @param fileSetCheck the additional FileSetCheck + */ + public void addFileSetCheck(FileSetCheck fileSetCheck) { + fileSetCheck.setMessageDispatcher(this); + fileSetChecks.add(fileSetCheck); + } + + /** + * Adds a filter to the end of the audit event filter chain. + * @param filter the additional filter + */ + public void addFilter(Filter filter) { + filters.addFilter(filter); + } + + /** + * Add the listener that will be used to receive events from the audit. + * @param listener the nosy thing + */ + public final void addListener(AuditListener listener) { + listeners.add(listener); } /**