diff --git a/src/main/clojure/hawk/core.clj b/src/main/clojure/hawk/core.clj index 5d2b6d3..f472ed5 100644 --- a/src/main/clojure/hawk/core.clj +++ b/src/main/clojure/hawk/core.clj @@ -1,6 +1,10 @@ (ns hawk.core (:require [clojure.java.io :as io] - [hawk.watcher :refer [new-watcher] :as watcher])) + [hawk.watcher :refer [new-watcher] :as watcher]) + (:import [java.nio.file Path] + [java.io File])) + +(set! *warn-on-reflection* true) (defn catch-errors [f] (fn [& args] @@ -13,13 +17,13 @@ (merge spec (->> paths (map #(-> % io/file .getCanonicalFile)) - (group-by #(if (.isFile %) :files :directories))))) + (group-by #(if (.isFile ^File %) :files :directories))))) (defn process-paths [{:keys [files directories] :as spec}] (assoc spec :paths (concat - (map #(-> % .toPath) directories) - (map #(-> % .getParentFile .toPath) files)))) + (map #(.toPath ^File %) directories) + (map #(-> ^File % .getParentFile .toPath) files)))) (defn process-context [spec] (assoc spec :context @@ -29,10 +33,10 @@ (defn process-handler [{:keys [files paths] :as spec}] (assoc spec :handler (catch-errors - (fn [ctx {:keys [file kind] :as e}] - (let [path (-> file .toPath)] + (fn [ctx {:keys [^File file kind] :as e}] + (let [path ^Path (.toPath file)] (if (and - (some #(.startsWith path %) paths) + (some #(.startsWith path ^Path %) paths) (or (empty? files) (some (partial = file) files)) (or (not (:filter spec)) @@ -44,7 +48,7 @@ (defn remove-duplicate-paths [paths] (seq (apply - sorted-set-by #(.compareTo %1 %2) paths))) + sorted-set-by #(.compareTo ^Path %1 ^Path %2) paths))) (defn remove-children-paths [paths] (loop [a [] @@ -52,7 +56,7 @@ (cond (seq ps) (recur (conj a p) - (remove #(.startsWith % p) ps)) + (remove #(.startsWith ^Path % ^Path p) ps)) p (conj a p) :else a))) @@ -93,14 +97,14 @@ (defn stop! [watch] (doto - (:thread watch) .interrupt .join) + ^Thread (:thread watch) .interrupt .join) (-> watch :watcher watcher/stop!)) (defn file? [_ {:keys [file]}] - (.isFile file)) + (.isFile ^File file)) (defn directory? [_ {:keys [file]}] - (.isDirectory file)) + (.isDirectory ^File file)) (defn created? [_ {:keys [kind]}] (= kind :create)) diff --git a/src/main/clojure/hawk/watcher.clj b/src/main/clojure/hawk/watcher.clj index accce3e..fde9e4d 100644 --- a/src/main/clojure/hawk/watcher.clj +++ b/src/main/clojure/hawk/watcher.clj @@ -6,6 +6,8 @@ (java.nio.file FileSystems Path Paths StandardWatchEventKinds WatchEvent WatchEvent$Modifier WatchKey WatchService) (com.barbarysoftware.watchservice StandardWatchEventKind WatchableFile))) +(set! *warn-on-reflection* true) + (def barbary-watch-event-kinds {:create StandardWatchEventKind/ENTRY_CREATE :modify StandardWatchEventKind/ENTRY_MODIFY @@ -48,10 +50,7 @@ :context (.context e)}) (defn- handle-watch-event! [this ^Path path {:keys [kind context]}] - (let [file (.. path - (resolve context) - toFile - getCanonicalFile)] + (let [file (.getCanonicalFile (.toFile ^Path (resolve path (cast Path context))))] (if (and (= kind :create) (.isDirectory file)) (do (register! this (.toPath file) [:create :modify :delete]) @@ -64,7 +63,7 @@ file-seq (map (fn [f] {:kind :create - :file (.getCanonicalFile f)})))) + :file (.getCanonicalFile ^File f)})))) [{:kind kind :file file}]))) @@ -110,7 +109,7 @@ context (.context event)] {:file (-> context str io/file .getCanonicalFile) :kind ((map-invert barbary-watch-event-kinds) kind)})) - #(.reset ^com.barbarysoftware.watchservice.WatchService %)) + #(.reset ^com.barbarysoftware.watchservice.WatchKey %)) (catch com.barbarysoftware.watchservice.ClosedWatchServiceException _ _ nil))) :stop! (fn [^com.barbarysoftware.watchservice.WatchService this] (.close this))})