Skip to content
Browse files

Add counters to File#{exists, isFile, isDirectory}.

Due to limitations in the Statistics machinery, these are only
reported if this patch is applied.

        --- a/src/reflect/scala/reflect/internal/util/Statistics.scala
        +++ b/src/reflect/scala/reflect/internal/util/Statistics.scala
        @@ -109,7 +109,7 @@ quant)
            *  Quantities with non-empty prefix are printed in the statistics info.
            */
           trait Quantity {
        -    if (enabled && prefix.nonEmpty) {
        +    if (prefix.nonEmpty) {
               val key = s"${if (underlying != this) underlying.prefix else ""}/$prefix"
               qs(key) = this
             }
        @@ -243,7 +243,7 @@ quant)
            *
            *  to remove all Statistics code from build
            */
        -  final val canEnable = _enabled
        +  final val canEnable = true // _enabled
  • Loading branch information...
1 parent 2efc6d5 commit 0cb632440e07ce370a3b97f81a81ee920eadb282 @retronym retronym committed
View
6 src/reflect/scala/reflect/io/AbstractFile.scala
@@ -11,6 +11,7 @@ import java.io.{ FileOutputStream, IOException, InputStream, OutputStream, Buffe
import java.io.{ File => JFile }
import java.net.URL
import scala.collection.mutable.ArrayBuffer
+import scala.reflect.internal.util.Statistics
/**
* An abstraction over files for use in the reflection/compiler libraries.
@@ -112,7 +113,10 @@ abstract class AbstractFile extends Iterable[AbstractFile] {
def underlyingSource: Option[AbstractFile] = None
/** Does this abstract file denote an existing file? */
- def exists: Boolean = (file eq null) || file.exists
+ def exists: Boolean = {
+ if (Statistics.canEnable) Statistics.incCounter(IOStats.fileExistsCount)
+ (file eq null) || file.exists
+ }
/** Does this abstract file represent something which can contain classfiles? */
def isClassContainer = isDirectory || (file != null && (extension == "jar" || extension == "zip"))
View
31 src/reflect/scala/reflect/io/IOStats.scala
@@ -0,0 +1,31 @@
+package scala.reflect.io
+
+import scala.reflect.internal.util.Statistics
+
+// Due to limitations in the Statistics machinery, these are only
+// reported if this patch is applied.
+//
+// --- a/src/reflect/scala/reflect/internal/util/Statistics.scala
+// +++ b/src/reflect/scala/reflect/internal/util/Statistics.scala
+// @@ -109,7 +109,7 @@ quant)
+// * Quantities with non-empty prefix are printed in the statistics info.
+// */
+// trait Quantity {
+// - if (enabled && prefix.nonEmpty) {
+// + if (prefix.nonEmpty) {
+// val key = s"${if (underlying != this) underlying.prefix else ""}/$prefix"
+// qs(key) = this
+// }
+// @@ -243,7 +243,7 @@ quant)
+// *
+// * to remove all Statistics code from build
+// */
+// - final val canEnable = _enabled
+// + final val canEnable = true // _enabled
+//
+// We can commit this change as the first diff reverts a fix for an IDE memory leak.
+private[io] object IOStats {
+ val fileExistsCount = Statistics.newCounter("# File.exists calls")
+ val fileIsDirectoryCount = Statistics.newCounter("# File.isDirectory calls")
+ val fileIsFileCount = Statistics.newCounter("# File.isFile calls")
+}
View
30 src/reflect/scala/reflect/io/Path.scala
@@ -13,6 +13,7 @@ import java.io.{ File => JFile }
import java.net.{ URI, URL }
import scala.util.Random.alphanumeric
import scala.language.implicitConversions
+import scala.reflect.internal.util.Statistics
/** An abstraction for filesystem paths. The differences between
* Path, File, and Directory are primarily to communicate intent.
@@ -57,8 +58,18 @@ object Path {
def apply(path: String): Path = apply(new JFile(path))
def apply(jfile: JFile): Path = try {
- if (jfile.isFile) new File(jfile)
- else if (jfile.isDirectory) new Directory(jfile)
+ def isFile = {
+ if (Statistics.canEnable) Statistics.incCounter(IOStats.fileIsFileCount)
+ jfile.isFile
+ }
+
+ def isDirectory = {
+ if (Statistics.canEnable) Statistics.incCounter(IOStats.fileIsDirectoryCount)
+ jfile.isDirectory
+ }
+
+ if (isFile) new File(jfile)
+ else if (isDirectory) new Directory(jfile)
else new Path(jfile)
} catch { case ex: SecurityException => new Path(jfile) }
@@ -187,10 +198,19 @@ class Path private[io] (val jfile: JFile) {
// Boolean tests
def canRead = jfile.canRead()
def canWrite = jfile.canWrite()
- def exists = try jfile.exists() catch { case ex: SecurityException => false }
+ def exists = {
+ if (Statistics.canEnable) Statistics.incCounter(IOStats.fileExistsCount)
+ try jfile.exists() catch { case ex: SecurityException => false }
+ }
- def isFile = try jfile.isFile() catch { case ex: SecurityException => false }
- def isDirectory = try jfile.isDirectory() catch { case ex: SecurityException => false }
+ def isFile = {
+ if (Statistics.canEnable) Statistics.incCounter(IOStats.fileIsFileCount)
+ try jfile.isFile() catch { case ex: SecurityException => false }
+ }
+ def isDirectory = {
+ if (Statistics.canEnable) Statistics.incCounter(IOStats.fileIsDirectoryCount)
+ try jfile.isDirectory() catch { case ex: SecurityException => false }
+ }
def isAbsolute = jfile.isAbsolute()
def isEmpty = path.length == 0

0 comments on commit 0cb6324

Please sign in to comment.
Something went wrong with that request. Please try again.