Skip to content

Commit

Permalink
Issue checkstyle#2992: ThreadLocal usage in single-threaded checkstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladlis committed Nov 27, 2016
1 parent f84e26a commit 44a5851
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 33 deletions.
6 changes: 6 additions & 0 deletions config/findbugs-exclude.xml
Expand Up @@ -112,4 +112,10 @@
<Method name="makeCodeSelection"/>
<Bug pattern="RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT"/>
</Match>
<Match>
<!-- Until https://github.com/checkstyle/checkstyle/issues/3573 -->
<Class name="com.puppycrawl.tools.checkstyle.checks.FileContentsHolder" />
<Method name="beginTree"/>
<Bug pattern="ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD"/>
</Match>
</FindBugsFilter>
Expand Up @@ -31,21 +31,20 @@
* @author Mike McMahon
* @author Rick Giles
*/
public class FileContentsHolder
extends AbstractCheck {
public class FileContentsHolder extends AbstractCheck {
/** The current file contents. */
private static final ThreadLocal<FileContents> FILE_CONTENTS = new ThreadLocal<>();
private static FileContents currentFileContents;

/**
* @return the current file contents.
*/
public static FileContents getContents() {
return FILE_CONTENTS.get();
public static FileContents getCurrentFileContents() {
return currentFileContents;
}

@Override
public int[] getDefaultTokens() {
return getAcceptableTokens();
return CommonUtils.EMPTY_INT_ARRAY;
}

@Override
Expand All @@ -60,14 +59,6 @@ public int[] getRequiredTokens() {

@Override
public void beginTree(DetailAST rootAST) {
FILE_CONTENTS.set(getFileContents());
}

@Override
public void destroy() {
// This needs to be called in destroy, rather than finishTree()
// as finishTree() is called before the messages are passed to the
// filters. Without calling remove, there is a memory leak.
FILE_CONTENTS.remove();
currentFileContents = getFileContents();
}
}
Expand Up @@ -191,16 +191,14 @@ public boolean accept(AuditEvent event) {
if (event.getLocalizedMessage() != null) {
// Lazy update. If the first event for the current file, update file
// contents and tag suppressions
final FileContents currentContents = FileContentsHolder.getContents();
final FileContents currentContents = FileContentsHolder.getCurrentFileContents();

if (currentContents != null) {
if (getFileContents() != currentContents) {
setFileContents(currentContents);
tagSuppressions();
}
if (matchesTag(event)) {
accepted = false;
}
if (getFileContents() != currentContents) {
setFileContents(currentContents);
tagSuppressions();
}
if (matchesTag(event)) {
accepted = false;
}
}
return accepted;
Expand Down
Expand Up @@ -179,16 +179,14 @@ public boolean accept(AuditEvent event) {
if (event.getLocalizedMessage() != null) {
// Lazy update. If the first event for the current file, update file
// contents and tag suppressions
final FileContents currentContents = FileContentsHolder.getContents();
final FileContents currentContents = FileContentsHolder.getCurrentFileContents();

if (currentContents != null) {
if (getFileContents() != currentContents) {
setFileContents(currentContents);
tagSuppressions();
}
final Tag matchTag = findNearestMatch(event);
accepted = matchTag == null || matchTag.isReportingOn();
if (getFileContents() != currentContents) {
setFileContents(currentContents);
tagSuppressions();
}
final Tag matchTag = findNearestMatch(event);
accepted = matchTag == null || matchTag.isReportingOn();
}
return accepted;
}
Expand Down
Expand Up @@ -121,7 +121,7 @@ protected void processFiltered(File file, List<String> lines) {

@Override
public void finishProcessing() {
fileContentAvailable = FileContentsHolder.getContents() != null;
fileContentAvailable = FileContentsHolder.getCurrentFileContents() != null;
}
}
}

0 comments on commit 44a5851

Please sign in to comment.