Skip to content

Commit

Permalink
Issue #4917: Remove thread-unsafe context from the AbstractFileSetCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
soon committed Aug 9, 2017
1 parent 70761aa commit 8671e59
Showing 1 changed file with 12 additions and 8 deletions.
Expand Up @@ -37,8 +37,12 @@ public abstract class AbstractFileSetCheck
extends AbstractViolationReporter
implements FileSetCheck {

/** Collects the error messages. */
private final SortedSet<LocalizedMessage> messageCollector = new TreeSet<>();
/**
* Collects the error messages.
* @noinspection ThreadLocalNotStaticFinal
*/
private final ThreadLocal<SortedSet<LocalizedMessage>> messageCollector =
ThreadLocal.withInitial(TreeSet::new);

/** The dispatcher errors are fired to. */
private MessageDispatcher messageDispatcher;
Expand Down Expand Up @@ -73,12 +77,12 @@ public void beginProcessing(String charset) {
@Override
public final SortedSet<LocalizedMessage> process(File file, FileText fileText)
throws CheckstyleException {
messageCollector.clear();
messageCollector.get().clear();
// Process only what interested in
if (CommonUtils.matchesFileExtension(file, fileExtensions)) {
processFiltered(file, fileText);
}
return new TreeSet<>(messageCollector);
return new TreeSet<>(messageCollector.get());
}

@Override
Expand Down Expand Up @@ -139,7 +143,7 @@ public final void setFileExtensions(String... extensions) {
* @param messages the sorted set of {@link LocalizedMessage}.
*/
protected final void addMessages(SortedSet<LocalizedMessage> messages) {
messageCollector.addAll(messages);
messageCollector.get().addAll(messages);
}

@Override
Expand All @@ -150,7 +154,7 @@ public final void log(int line, String key, Object... args) {
@Override
public final void log(int lineNo, int colNo, String key,
Object... args) {
messageCollector.add(
messageCollector.get().add(
new LocalizedMessage(lineNo,
colNo,
getMessageBundle(),
Expand All @@ -169,8 +173,8 @@ public final void log(int lineNo, int colNo, String key,
* @param fileName the audited file
*/
protected final void fireErrors(String fileName) {
final SortedSet<LocalizedMessage> errors = new TreeSet<>(messageCollector);
messageCollector.clear();
final SortedSet<LocalizedMessage> errors = new TreeSet<>(messageCollector.get());
messageCollector.get().clear();
messageDispatcher.fireErrors(fileName, errors);
}
}

0 comments on commit 8671e59

Please sign in to comment.