Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Reflection warnings #123

Merged
merged 1 commit into from

2 participants

@niclasmeier

Fixed a couple of reflection warnings and added a comment on the warnings on builder-map to clarify where the warnings are coming from (Fixes to #113).

@seancorfield seancorfield merged commit f921f28 into aboekhoff:master
@seancorfield
Collaborator

Thanx. Always good to knock those reflection warnings out (even in code that isn't executed a lot, IMO).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 18, 2013
  1. @niclasmeier
This page is out of date. Refresh to see the latest.
View
32 src/somnium/congomongo.clj
@@ -56,13 +56,14 @@
(def ^:private builder-map
"A map from keywords to builder invocation functions."
- (let [is-builder-method? (fn [f]
+ ;; Be aware that using refelction directly here will also issue Clojure reflection warnings.
+ (let [is-builder-method? (fn [^java.lang.reflect.Method f]
(let [m (.getModifiers f)]
(and (java.lang.reflect.Modifier/isPublic m)
(not (java.lang.reflect.Modifier/isStatic m))
(= MongoClientOptions$Builder (.getReturnType f)))))
- method-name (fn [f] (.getName f))
- builder-call (fn [m]
+ method-name (fn [^java.lang.reflect.Method f] (.getName f))
+ builder-call (fn [^MongoClientOptions$Builder m]
(eval (list 'fn '[o v]
(list (symbol (str "." m)) 'o 'v))))
kw-fn-pair (fn [m] [(field->kw m) (builder-call m)])
@@ -82,7 +83,7 @@
(f b v)
(throw (IllegalArgumentException.
(str k " is not a valid MongoClientOptions$Builder argument")))))]
- (.build (reduce builder-call (MongoClientOptions$Builder.) option-map))))
+ (.build ^MongoClientOptions$Builder (reduce builder-call (MongoClientOptions$Builder.) option-map))))
(defn- make-server-address
"Convenience to make a ServerAddress without reflection warnings."
@@ -112,10 +113,11 @@
^MongoClient client (MongoClient. mongouri)
^String db (.getDatabase mongouri)
conn {:mongo client :db (.getDB client db)}
- username (.getUsername mongouri)
- password (.getPassword mongouri)]
+ ^DB db (conn :db)
+ ^String username (.getUsername mongouri)
+ ^chars password (.getPassword mongouri)]
(when (and username password)
- (.authenticate (conn :db) username password))
+ (.authenticate db username password))
conn))
(defn make-connection
@@ -132,7 +134,7 @@ a map containing values for :host and/or :port.
([db]
(make-connection db {}))
([db & args]
- (let [dbname (named db)]
+ (let [^String dbname (named db)]
(if (.startsWith dbname "mongodb://")
(make-connection-uri dbname)
(make-connection-args dbname args)))))
@@ -176,7 +178,7 @@ a map containing values for :host and/or :port.
When with-db and set-database! interact, last one wins."
[dbname & body]
- `(let [db# (.getDB (:mongo *mongo-config*) (name ~dbname))]
+ `(let [^DB db# (.getDB ^MongoClient (:mongo *mongo-config*) (name ~dbname))]
(binding [*mongo-config* (assoc *mongo-config* :db db#)]
~@body)))
@@ -471,10 +473,10 @@ You should use fetch with :limit 1 instead."))); one? and sort should NEVER be c
:as -> results format (:clojure, :json, or :mongo). Defaults to :clojure."
{:arglists
'([collection key :where :from :as])}
- [coll k & {:keys [where from as]
+ [coll ^String k & {:keys [where from as]
:or {where {} from :clojure as :clojure}}]
- (let [query (coerce where [from :mongo])]
- (coerce (.distinct (get-coll coll) k query)
+ (let [^DBObject query (coerce where [from :mongo])]
+ (coerce (.distinct ^DBCollection (get-coll coll) k query)
[:mongo as])))
(defn insert!
@@ -687,7 +689,7 @@ You should use fetch with :limit 1 instead."))); one? and sort should NEVER be c
{:arglists '([fs data {:filename nil :contentType nil :metadata nil}])}
[fs data & {:keys [^String filename ^String contentType ^DBObject metadata]
:or {filename nil contentType nil metadata nil}}]
- (let [^com.mongodb.gridfs.GridFSInputFile f (.createFile (get-gridfs fs) data)]
+ (let [^com.mongodb.gridfs.GridFSInputFile f (.createFile ^GridFS (get-gridfs fs) data)]
(if filename (.setFilename f ^String filename))
(if contentType (.setContentType f contentType))
(if metadata (.setMetaData f (coerce metadata [:clojure :mongo])))
@@ -731,7 +733,7 @@ You should use fetch with :limit 1 instead."))); one? and sort should NEVER be c
should be either an OutputStream, File, or the String path for a file."
[fs file out]
;; since .findOne is overloaded and coerce returns different types, we cannot remove the reflection warning:
- (if-let [^com.mongodb.gridfs.GridFSDBFile f (.findOne (get-gridfs fs) (coerce file [:clojure :mongo]))]
+ (if-let [^com.mongodb.gridfs.GridFSDBFile f (.findOne ^GridFS (get-gridfs fs) (coerce file [:clojure :mongo]))]
;; since .writeTo is overloaded and we can pass different types, we cannot remove the reflection warning:
(.writeTo f out)))
@@ -739,7 +741,7 @@ You should use fetch with :limit 1 instead."))); one? and sort should NEVER be c
"Returns an InputStream from the GridFS file specified"
[fs file]
;; since .findOne is overloaded and coerce returns different types, we cannot remove the reflection warning:
- (if-let [^com.mongodb.gridfs.GridFSDBFile f (.findOne (get-gridfs fs) (coerce file [:clojure :mongo]))]
+ (if-let [^com.mongodb.gridfs.GridFSDBFile f (.findOne ^GridFS (get-gridfs fs) (coerce file [:clojure :mongo]))]
(.getInputStream f)))
(defn server-eval
View
4 test/somnium/congomongo/test/coerce.clj
@@ -47,8 +47,8 @@
(.put "a" -1))))
(deftest test-dbobject
- (are [fields bdo] (is (= (.toString (apply dbobject fields))
- (.toString bdo)))
+ (are [fields bdo] (let [^BasicDBObject exo (apply dbobject fields)]
+ (is (= (.toString exo ) (.toString bdo))))
[:a 1 :b 1 :c 1] (doto (BasicDBObject.)
(.put "a" 1)
(.put "b" 1)
View
23 test/somnium/test/congomongo.clj
@@ -5,7 +5,8 @@
somnium.congomongo.coerce
clojure.pprint)
(:use [clojure.data.json :only (read-str write-str)])
- (:import [com.mongodb DBObject BasicDBObject BasicDBObjectBuilder MongoException$DuplicateKey ReadPreference
+ (:import [com.mongodb MongoClient DB DBObject BasicDBObject BasicDBObjectBuilder MongoException$DuplicateKey
+ ReadPreference
WriteConcern]))
(deftest coercions
@@ -73,7 +74,7 @@
(let [a (make-connection "congomongotest-db-a" :host test-db-host :port test-db-port
(mongo-options :auto-connect-retry true
:write-concern (:acknowledged write-concern-map)))
- m (:mongo a)
+ ^MongoClient m (:mongo a)
opts (.getMongoOptions m)]
;; check non-default options attached to Mongo object
(is (.isAutoConnectRetry opts))
@@ -86,14 +87,14 @@
(let [userpass (if (and test-db-user test-db-pass) (str test-db-user ":" test-db-pass "@") "")
uri (str "mongodb://" userpass test-db-host ":" test-db-port "/congomongotest-db-a?maxpoolsize=123&w=1&safe=true")
a (make-connection uri)
- m (:mongo a)
+ ^MongoClient m (:mongo a)
opts (.getMongoOptions m)]
(testing "make-connection parses options from URI"
(is (= 123 (.getConnectionsPerHost opts)))
(is (= WriteConcern/ACKNOWLEDGED (.getWriteConcern opts))))
(with-mongo a
(testing "make-connection accepts Mongo URI"
- (is (= "congomongotest-db-a" (.getName (*mongo-config* :db)))))))))
+ (is (= "congomongotest-db-a" (.getName ^DB (*mongo-config* :db)))))))))
(deftest with-mongo-database
(with-test-mongo
@@ -101,9 +102,9 @@
(with-mongo a
(with-db "congomongotest-db-b"
(testing "with-mongo uses new database"
- (is (= "congomongotest-db-b" (.getName (*mongo-config* :db))))))
+ (is (= "congomongotest-db-b" (.getName ^DB (*mongo-config* :db))))))
(testing "with-mongo uses connection db "
- (is (= "congomongotest-db-a" (.getName (*mongo-config* :db)))))))))
+ (is (= "congomongotest-db-a" (.getName ^DB (*mongo-config* :db)))))))))
(deftest with-mongo-interactions
(with-test-mongo
@@ -111,12 +112,12 @@
b (make-connection :congomongotest-db-b :host test-db-host :port test-db-port)]
(with-mongo a
(testing "with-mongo sets the mongo-config"
- (is (= "congomongotest-db-a" (.getName (*mongo-config* :db)))))
+ (is (= "congomongotest-db-a" (.getName ^DB (*mongo-config* :db)))))
(testing "mongo! inside with-mongo stomps on current config"
(mongo! :db "congomongotest-db-b" :host test-db-host :port test-db-port)
- (is (= "congomongotest-db-b" (.getName (*mongo-config* :db))))))
+ (is (= "congomongotest-db-b" (.getName ^DB (*mongo-config* :db))))))
(testing "and previous mongo! inside with-mongo is visible afterwards"
- (is (= "congomongotest-db-b" (.getName (*mongo-config* :db))))))))
+ (is (= "congomongotest-db-b" (.getName ^DB (*mongo-config* :db))))))))
(deftest closing-with-mongo
(with-test-mongo
@@ -535,12 +536,12 @@
(.deleteOnExit))]
(with-open [w (java.io.FileOutputStream. tmp)]
(doseq [i (range 2048)]
- (.write w (rem i 255))))
+ (.write w (int (rem i 255)))))
tmp))
(deftest gridfs-test-insert-different-data-types
(with-test-mongo
- (let [file (gen-tempfile)]
+ (let [^java.io.File file (gen-tempfile)]
(insert-file! :filefs file)
(insert-file! :filefs (java.io.FileInputStream. file))
(insert-file! :filefs (.getBytes "data"))
Something went wrong with that request. Please try again.