Permalink
Browse files

reflection wip

  • Loading branch information...
1 parent 587af46 commit 1f1836ce37ae78f0dc6503b7ac56ed536f99d893 @stuarthalloway stuarthalloway committed Sep 21, 2010
Showing with 66 additions and 19 deletions.
  1. +8 −1 src/mycroft/class.clj
  2. +35 −10 src/mycroft/data.clj
  3. +23 −8 src/mycroft/reflect.clj
View
9 src/mycroft/class.clj
@@ -3,12 +3,19 @@
(:require [mycroft.reflect :as reflect]
[mycroft.breadcrumb :as breadcrumb]))
+(defn member-selector?
+ [selector]
+ (contains? #{[:fields] [:methods] [:constructors]} selector))
+
(defn render
[classname options]
(let [cls (Class/forName classname)
obj (reflect/reflect cls)
selector (:selector options)
- selection (select obj selector)]
+ selection (select obj selector)
+ options (if (member-selector? selector)
+ (assoc options :headers [:name :modifiers])
+ options)]
[:div
[:div {:id "breadcrumb"}
(breadcrumb/top-link)
View
45 src/mycroft/data.clj
@@ -136,11 +136,21 @@
(defn render-row
[row options]
- (if (second row)
- `[:tr
- ~(render-cell (first row) {:href (breadcrumb/url (add-selector options (first row)))})
- ~@(map render-cell (rest row))]
- [:tr (render-cell (first row))]))
+ {:pre (= 2 (count row))}
+ `[:tr
+ ~(render-cell (first row) {:href (breadcrumb/url (add-selector options (first row)))})
+ ~@(map render-cell (rest row))])
+
+(defn render-row-matching-headers
+ [[key obj :as row] {:keys [headers] :as options}]
+ {:pre [(= 2 (count row))
+ (associative? obj)]}
+ `[:tr
+ ~(render-cell key {:href (breadcrumb/url (add-selector options key))})
+ ~@(let [explicit-columns (map obj headers)]
+ (map render-cell explicit-columns))
+ ~(let [rest-of-object (apply dissoc obj headers)]
+ (render-cell rest-of-object))])
(defn composite?
[x]
@@ -169,13 +179,28 @@
"next"]
[:span.disabled-button "next"])]))
+(defn render-table-with-headers
+ [content {:keys [headers] :as options}]
+ `[:table.data
+ [:thead
+ [:tr
+ ~@(map #(vector :td %) (concat [" "] headers))]]
+ [:tbody
+ ~@(map
+ (fn [o]
+ (println o)
+ (render-row-matching-headers o options))
+ content)]])
+
(defn render-table
- [content {:keys [start] :as options}]
+ [content {:keys [headers] :as options}]
(if (seq content)
- [:table.data
- (map
- #(render-row % options)
- content)]
+ (if headers
+ (render-table-with-headers content options)
+ [:table.data
+ (map
+ #(render-row % options)
+ content)])
[:table.data
[:th "Collection is empty."]]))
View
31 src/mycroft/reflect.clj
@@ -1,5 +1,5 @@
(ns mycroft.reflect
- (:import java.lang.reflect.Modifier))
+ (:import [java.lang.reflect Field Constructor Method Modifier]))
(defn modifiers->set
[mod]
@@ -17,8 +17,22 @@
(when (Modifier/isTransient mod) :transient)
(when (Modifier/isVolatile mod) :volatile)])))
+(defn constructor->map
+ [^Constructor constructor]
+ {:name (.getName constructor)
+ :declaring-class (.getDeclaringClass constructor)
+ :parameter-types (vec (.getParameterTypes constructor))
+ :exception-types (vec (.getExceptionTypes constructor))
+ :modifiers (modifiers->set (.getModifiers constructor))})
+
+(defn constructors-set
+ [^Class cls]
+ (set (map
+ constructor->map
+ (.getDeclaredConstructors cls))))
+
(defn method->map
- [method]
+ [^Method method]
{:name (.getName method)
:return-type (.getReturnType method)
:declaring-class (.getDeclaringClass method)
@@ -27,30 +41,31 @@
:modifiers (modifiers->set (.getModifiers method))})
(defn methods-set
- [cls]
+ [^Class cls]
(set (map
method->map
- (.getMethods cls))))
+ (.getDeclaredMethods cls))))
(defn field->map
- [field]
+ [^Field field]
{:name (.getName field)
:type (.getType field)
:declaring-class (.getDeclaringClass field)
:modifiers (modifiers->set (.getModifiers field))})
(defn fields-set
- [cls]
+ [^Class cls]
(set (map
field->map
- (.getFields cls))))
+ (.getDeclaredFields cls))))
(defn reflect
[cls]
(when cls
(if (class? cls)
{:fields (fields-set cls)
- :methods (methods-set cls)}
+ :methods (methods-set cls)
+ :constructors (constructors-set cls)}
(reflect (class cls)))))

0 comments on commit 1f1836c

Please sign in to comment.