diff --git a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/builder/Auditor.java b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/builder/Auditor.java index d992142c2..a6c7f18e1 100644 --- a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/builder/Auditor.java +++ b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/builder/Auditor.java @@ -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 { diff --git a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/builder/CheckerFactory.java b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/builder/CheckerFactory.java index 44e2d9e51..fdf11841f 100644 --- a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/builder/CheckerFactory.java +++ b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/builder/CheckerFactory.java @@ -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; @@ -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 sCheckerMap; @@ -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(); @@ -143,6 +144,7 @@ public static Checker createChecker(ICheckConfiguration config, IProject project public static void cleanup() { sCheckerMap.invalidateAll(); sModifiedMap.clear(); + deleteCacheFiles(); } /** @@ -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; } @@ -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); + } + } }