diff --git a/examples/example-vfs/src/FileSystem.kt b/examples/example-vfs/src/FileSystem.kt index e9380227794..9c256147527 100644 --- a/examples/example-vfs/src/FileSystem.kt +++ b/examples/example-vfs/src/FileSystem.kt @@ -1,5 +1,6 @@ package org.jetbrains.jet.samples.vfs; +import std.concurrent.* import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; import org.jetbrains.jet.samples.vfs.utils.*; @@ -12,6 +13,7 @@ import java.util.Arrays import java.util.Map import std.util.* +import java.util.TimerTask /** * File system singleton. To work with virtual file system, read/write locks should be @@ -52,14 +54,14 @@ public object FileSystem { * Runs function with read lock. */ public inline fun read(task : () -> T) : T { - return locked(lock.readLock().sure(), task) + return lock.read(task) } /** * Runs function with write lock. */ public inline fun write(task : () -> T) : T { - return locked(lock.writeLock().sure(), task) + return lock.write(task) } /** @@ -102,6 +104,19 @@ public object FileSystem { listeners.add(listener) } + /** + * Adds file system listener which should be notified about changing of file system. + */ + public fun addVirtualFileListener(listener : VirtualFileListener.(VirtualFileEvent)->Unit) : VirtualFileListener { + val vfl = object: VirtualFileListener{ + override fun eventHappened(event: VirtualFileEvent) { + listener(event) + } + } + addVirtualFileListener(vfl) + return vfl + } + /** * Removes file system listener. */ diff --git a/examples/example-vfs/src/Main.kt b/examples/example-vfs/src/Main.kt index 7c288a228b2..a5c20a1b2d2 100644 --- a/examples/example-vfs/src/Main.kt +++ b/examples/example-vfs/src/Main.kt @@ -16,20 +16,18 @@ fun main(args : Array) { // add watched directory FileSystem.write { - for (val arg in args) { + for (arg in args) { FileSystem.addWatchedDirectory(FileSystem.getFileByPath(arg)) } } // add listener which prints out everything - FileSystem.addVirtualFileListener(object : VirtualFileListener { - override fun eventHappened(event: VirtualFileEvent) { + FileSystem.addVirtualFileListener{ event -> println(event) if (event is VirtualFileChangedEvent) { println("new file size is ${event.file.size()}") } - } - }) + } // wait for 1 minute Thread.sleep(60000) diff --git a/examples/example-vfs/src/RefreshQueue.kt b/examples/example-vfs/src/RefreshQueue.kt index 09e7f21713f..daee725a078 100644 --- a/examples/example-vfs/src/RefreshQueue.kt +++ b/examples/example-vfs/src/RefreshQueue.kt @@ -7,6 +7,7 @@ import java.util.concurrent.LinkedBlockingQueue import java.util.ArrayList import std.util.* +import std.concurrent.* import java.util.Timer import java.util.TimerTask @@ -16,18 +17,22 @@ import org.jetbrains.jet.samples.vfs.utils.* * Singleton which creates thread for periodically checking if there are changes in * file system and notifying file system listeners. */ -internal object RefreshQueue : Runnable { - private val taskQueue = LinkedBlockingQueue>() - private val workerThread: Thread = Thread(this) - private val fullRefreshScheduler = Timer(true); +internal object RefreshQueue { + private val taskQueue = LinkedBlockingQueue>(); { - workerThread.setDaemon(true) - workerThread.start() + thread(daemon=true, name="refresher thread") { + while (!currentThread.isInterrupted()) { + try { + takeAndRefreshFiles() + } catch (e : InterruptedException) { + } + } + } - fullRefreshScheduler.scheduleAtFixedRate(createTimerTask { + fixedRateTimer(daemon=true, name="refresher timer", period=5000.lng) { scheduleRefresh(FileSystem.watchedDirectories) - }, 0, 5000) + } } private fun takeAndRefreshFiles() { @@ -114,25 +119,4 @@ internal object RefreshQueue : Runnable { // taskQueue.put(ArrayList(files.map{ it })) // taskQueue.put(files.map{ it }.toList()) } - - // FIXME RefreshQueue implements Runnable because of error on accessing - // private fields and methods from anonymous class (KT-1157 & KT-1159) ( - override fun run() { - while (!workerThread.isInterrupted()) { - try { - takeAndRefreshFiles() - } catch (e : InterruptedException) { - } - } - } } - -// FIXME this method is used because of error on accessing -// private methods from anonymous class (KT-1157) ( -private fun createTimerTask(task : () -> Unit) : TimerTask { - return object : TimerTask() { - override fun run() { - task() - } - } -} \ No newline at end of file diff --git a/stdlib/ktSrc/JavaLang.kt b/stdlib/ktSrc/JavaLang.kt index 99da9daef6d..687208ae694 100644 --- a/stdlib/ktSrc/JavaLang.kt +++ b/stdlib/ktSrc/JavaLang.kt @@ -3,8 +3,8 @@ package std import java.lang.Class import java.lang.Object -val T.javaClass : Class +val T.javaClass : Class get() = jet.runtime.Intrinsics.getJavaClass(this) as Class -val TypeInfo.javaClassForType : Class +val TypeInfo.javaClassForType : Class get() = (this as org.jetbrains.jet.rt.TypeInfoImpl).getJavaClass().sure()