diff --git a/pom.xml b/pom.xml
index cd8304247..936791cce 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1082,7 +1082,6 @@
PropertyCacheFile instance.
*
- * @param currentConfig the current configuration, not null
+ * @param config the current configuration, not null
* @param fileName the cache file
*/
- PropertyCacheFile(Configuration currentConfig, String fileName) {
- boolean setInActive = true;
- if (fileName != null) {
- FileInputStream inStream = null;
- // get the current config so if the file isn't found
- // the first time the hash will be added to output file
- final String currentConfigHash = getConfigHashCode(currentConfig);
+ PropertyCacheFile(Configuration config, String fileName) {
+ if (config == null) {
+ throw new IllegalArgumentException("config can not be null");
+ }
+ if (fileName == null) {
+ throw new IllegalArgumentException("fileName can not be null");
+ }
+ this.config = config;
+ this.fileName = fileName;
+ }
+
+ /**
+ * load cached values from file
+ * @throws IOException when there is a problems with file read
+ */
+ void load() throws IOException {
+ FileInputStream inStream = null;
+ // get the current config so if the file isn't found
+ // the first time the hash will be added to output file
+ final String currentConfigHash = getConfigHashCode(config);
+ if (new File(fileName).exists()) {
try {
inStream = new FileInputStream(fileName);
details.load(inStream);
- final String cachedConfigHash =
- details.getProperty(CONFIG_HASH_KEY);
- setInActive = false;
- if (cachedConfigHash == null
- || !cachedConfigHash.equals(currentConfigHash)) {
+ final String cachedConfigHash = details.getProperty(CONFIG_HASH_KEY);
+ if (!currentConfigHash.equals(cachedConfigHash)) {
// Detected configuration change - clear cache
details.clear();
details.put(CONFIG_HASH_KEY, currentConfigHash);
}
}
- catch (final FileNotFoundException e) {
- // Ignore, the cache does not exist
- setInActive = false;
- // put the hash in the file if the file is going to be created
- details.put(CONFIG_HASH_KEY, currentConfigHash);
- }
- catch (final IOException e) {
- LOG.debug("Unable to open cache file, ignoring.", e);
- }
finally {
Closeables.closeQuietly(inStream);
}
}
- detailsFile = setInActive ? null : fileName;
+ else {
+ // put the hash in the file if the file is going to be created
+ details.put(CONFIG_HASH_KEY, currentConfigHash);
+ }
}
- /** Cleans up the object and updates the cache file. **/
- void destroy() {
- if (detailsFile != null) {
- FileOutputStream out = null;
- try {
- out = new FileOutputStream(detailsFile);
- details.store(out, null);
- }
- catch (final IOException e) {
- LOG.debug("Unable to save cache file.", e);
- }
- finally {
- if (out != null) {
- flushAndCloseOutStream(out);
- }
- }
+ /**
+ * Cleans up the object and updates the cache file.
+ * @throws IOException when there is a problems with file save
+ */
+ void persist() throws IOException {
+ FileOutputStream out = null;
+ try {
+ out = new FileOutputStream(fileName);
+ details.store(out, null);
+ }
+ finally {
+ flushAndCloseOutStream(out);
}
}
/**
* Flushes and closes output stream.
* @param stream the output stream
+ * @throws IOException when there is a problems with file flush and close
*/
- private static void flushAndCloseOutStream(OutputStream stream) {
- try {
+ private static void flushAndCloseOutStream(OutputStream stream) throws IOException {
+ if (stream != null) {
Flushables.flush(stream, false);
- Closeables.close(stream, false);
- }
- catch (final IOException ex) {
- LOG.debug("Unable to flush and close output stream.", ex);
}
+ Closeables.close(stream, false);
}
/**
@@ -159,7 +157,7 @@ final class PropertyCacheFile {
* @param timestamp the timestamp of the file to check
* @return whether the specified file has already been checked ok
*/
- boolean alreadyChecked(String fileName, long timestamp) {
+ boolean inCache(String fileName, long timestamp) {
final String lastChecked = details.getProperty(fileName);
return lastChecked != null
&& lastChecked.equals(Long.toString(timestamp));
@@ -170,17 +168,17 @@ final class PropertyCacheFile {
* @param fileName name of the file that checked ok
* @param timestamp the timestamp of the file
*/
- void checkedOk(String fileName, long timestamp) {
+ void put(String fileName, long timestamp) {
details.put(fileName, Long.toString(timestamp));
}
/**
* Calculates the hashcode for a GlobalProperties.
*
- * @param configuration the GlobalProperties
- * @return the hashcode for configuration
+ * @param object the GlobalProperties
+ * @return the hashcode for object
*/
- private static String getConfigHashCode(Serializable configuration) {
+ private static String getConfigHashCode(Serializable object) {
try {
// im-memory serialization of Configuration
@@ -188,7 +186,7 @@ final class PropertyCacheFile {
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(baos);
- oos.writeObject(configuration);
+ oos.writeObject(object);
}
finally {
flushAndCloseOutStream(oos);
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java b/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java
index 05a581fbf..fb7c418ef 100755
--- a/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java
@@ -20,6 +20,7 @@
package com.puppycrawl.tools.checkstyle;
import java.io.File;
+import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.AbstractMap.SimpleEntry;
@@ -103,7 +104,7 @@ public final class TreeWalker
private int tabWidth = DEFAULT_TAB_WIDTH;
/** cache file **/
- private PropertyCacheFile cache = new PropertyCacheFile(null, null);
+ private PropertyCacheFile cache;
/** class loader to resolve classes with. **/
private ClassLoader classLoader;
@@ -130,6 +131,13 @@ public final class TreeWalker
public void setCacheFile(String fileName) {
final Configuration configuration = getConfiguration();
cache = new PropertyCacheFile(configuration, fileName);
+
+ try {
+ cache.load();
+ }
+ catch (IOException e) {
+ throw new IllegalStateException("cache file load is failed", e);
+ }
}
/** @param classLoader class loader to resolve classes with. */
@@ -178,8 +186,9 @@ public final class TreeWalker
// check if already checked and passed the file
final String fileName = file.getPath();
final long timestamp = file.lastModified();
- if (cache.alreadyChecked(fileName, timestamp)
- || !Utils.fileExtensionMatches(file, getFileExtensions())) {
+ if (cache != null
+ && (cache.inCache(fileName, timestamp)
+ || !Utils.fileExtensionMatches(file, getFileExtensions()))) {
return;
}
@@ -216,8 +225,8 @@ public final class TreeWalker
getMessageCollector().add(createLocalizedMessage(ex.getMessage()));
}
- if (getMessageCollector().size() == 0) {
- cache.checkedOk(fileName, timestamp);
+ if (cache != null && getMessageCollector().size() == 0) {
+ cache.put(fileName, timestamp);
}
}
@@ -466,7 +475,14 @@ public final class TreeWalker
for (Check c : commentChecks) {
c.destroy();
}
- cache.destroy();
+ if (cache != null) {
+ try {
+ cache.persist();
+ }
+ catch (IOException e) {
+ throw new IllegalStateException("Unable to persist cache file", e);
+ }
+ }
super.destroy();
}
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/CustomImportOrderCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/CustomImportOrderCheck.java
index 6e1359946..312fb4598 100644
--- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/CustomImportOrderCheck.java
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/CustomImportOrderCheck.java
@@ -117,7 +117,7 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
* For example:
*
*
To configure the check so that it matches default Eclipse formatter configuration - * (tested on Kepler and Luna releases):
+ * (tested on Kepler, Luna and Mars): *Notes:
- *
*
* <module name="CustomImportOrder">
@@ -145,29 +137,6 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
*
*
*
- * To configure the check so that it matches default Eclipse formatter - * configuration (tested on Mars release):
- *
- *
- *<module name="CustomImportOrder">
- * <property name="customImportOrderRules"
- * value="STATIC###STANDARD_JAVA_PACKAGE###SPECIAL_IMPORTS
- * ###THIRD_PARTY_PACKAGE"/>
- * <property name="specialImportsRegExp" value="org"/>
- * <property name="thirdPartyPackageRegExp" value="com"/>
- * <property name="sortImportsInGroupAlphabetically" value="true"/>
- * <property name="separateLineBetweenGroups" value="true"/>
- *</module>
- *
- *
* To configure the check so that it matches default IntelliJ IDEA formatter * configuration (tested on v14):
*To configure the check so that it matches default Eclipse formatter configuration - * (tested on Kepler and Luna releases):
+ * (tested on Kepler, Luna and Mars): *Notes:
- ** <module name="ImportOrder"> @@ -93,26 +86,6 @@ import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck; * </module> ** - *
To configure the check so that it matches default Eclipse formatter - * configuration (tested on Mars release):
- *- *<module name="ImportOrder"> - * <property name="groups" value="/^javax?\./,org,com"/> - * <property name="ordered" value="true"/> - * <property name="separated" value="true"/> - * <property name="option" value="above"/> - * <property name="sortStaticImportsAlphabetically" value="true"/> - *</module> - *- * *
To configure the check so that it matches default IntelliJ IDEA formatter configuration * (tested on v14):
*