Skip to content

Commit

Permalink
minor: reorganized Checker class (#3128)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-zuy authored and romani committed Apr 26, 2016
1 parent 3defd23 commit 6273f20
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 135 deletions.
4 changes: 2 additions & 2 deletions config/suppressions.xml
Expand Up @@ -13,10 +13,10 @@
See https://github.com/checkstyle/checkstyle/issues/2285-->
<suppress checks="IllegalCatch"
files="Checker.java"
lines="315"/>
lines="247"/>
<suppress checks="IllegalCatch"
files="Checker.java"
lines="320"/>
lines="252"/>
<!--Test to reproduce error catching in Checker and satisfy coverage rate. -->
<suppress checks="IllegalCatch"
files="CheckerTest.java"
Expand Down
266 changes: 133 additions & 133 deletions src/main/java/com/puppycrawl/tools/checkstyle/Checker.java
Expand Up @@ -133,90 +133,6 @@ public void setCacheFile(String fileName) throws IOException {
cache.load();
}

@Override
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<String> 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.
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -289,6 +205,22 @@ public int process(List<File> 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.
Expand All @@ -315,7 +247,7 @@ private void processFiles(List<File> 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
Expand All @@ -341,36 +273,12 @@ private SortedSet<LocalizedMessage> 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.
*
Expand All @@ -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<LocalizedMessage> 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.
*
Expand All @@ -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<LocalizedMessage> 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<String> 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);
}

/**
Expand Down

0 comments on commit 6273f20

Please sign in to comment.