Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: use checkstyle core cachefile #488

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,24 @@ public void runAudit(IProject project, IProgressMonitor monitor)
// Cleanup listener and filter
if (checker != null) {
checker.removeListener(listener);
persistCacheFile(checker);
}
}
}

private void persistCacheFile(Checker checker) throws CheckstylePluginException {
try {
// TODO use new API in checkstyle core once it might be available
var field = checker.getClass().getDeclaredField("cacheFile");
field.setAccessible(true);
var cacheFile = field.get(checker);
var persistMethod = cacheFile.getClass().getDeclaredMethod("persist");
persistMethod.invoke(cacheFile);
} catch (ReflectiveOperationException | SecurityException | IllegalArgumentException ex) {
CheckstylePluginException.rethrow(ex);
}
}

private void handleCheckstyleFailure(IProject project, CheckstyleException error)
throws CheckstylePluginException {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.LocalizedMessage;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
Expand All @@ -59,6 +63,8 @@
*/
public final class CheckerFactory {

private static final String CHECKSTYLE_CACHEFILE_EXTENSION = ".checkstyle.cache";

/** Map containing the configured checkers. */
private static Cache<String, Checker> sCheckerMap;

Expand Down Expand Up @@ -103,11 +109,6 @@ public static Checker createChecker(ICheckConfiguration config, IProject project
CheckstyleConfigurationFile configFileData = config.getCheckstyleConfiguration();
Checker checker = tryCheckerCache(cacheKey, configFileData.getModificationStamp());

// clear Checkstyle internal caches upon checker reuse
if (checker != null) {
checker.clearCache();
}

// no cache hit
if (checker == null) {
PropertyResolver resolver = configFileData.getPropertyResolver();
Expand Down Expand Up @@ -143,6 +144,7 @@ public static Checker createChecker(ICheckConfiguration config, IProject project
public static void cleanup() {
sCheckerMap.invalidateAll();
sModifiedMap.clear();
deleteCacheFiles();
}

/**
Expand Down Expand Up @@ -243,6 +245,12 @@ private static Checker createCheckerInternal(InputSource input, PropertyResolver
// https://sourceforge.net/tracker/?func=detail&aid=2880044&group_id=80344&atid=559497
checker.setBasedir(null);

try {
checker.setCacheFile(project.getLocation().toFile() + File.separator + CHECKSTYLE_CACHEFILE_EXTENSION);
} catch (IOException ex) {
CheckstylePluginException.rethrow(ex);
}

return checker;
}

Expand Down Expand Up @@ -279,4 +287,21 @@ private static String getLocale() {
}
return lang;
}

public static String getCacheFileLocation(final IProject project) {
return CheckstylePlugin.getDefault().getStateLocation().append(project.getName()).append(CHECKSTYLE_CACHEFILE_EXTENSION).toOSString();
}

public static void deleteCacheFiles() {
File dir = CheckstylePlugin.getDefault().getStateLocation().toFile();
File [] files = dir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(CHECKSTYLE_CACHEFILE_EXTENSION);
}
});
if (files != null) {
Arrays.stream(files).forEach(File::delete);
}
}
}