forked from greglook/puget
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add :class-lookup option for local extensibility
Based off of the fressian API.
- Loading branch information
1 parent
5491c2c
commit d87687c
Showing
2 changed files
with
57 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
(ns puget.class-lookup | ||
"Class-based lookup, algorithm based on | ||
https://github.com/Datomic/fressian/blob/32a833ba5adce9cd14365ff64b8b42a8569b7916/src/org/fressian/impl/InheritanceLookup.java") | ||
|
||
(defn check-base-classes | ||
[m ^Class the-class] | ||
(loop [c (.getSuperclass the-class)] | ||
(when-not (= Object c) | ||
(or (get m c) | ||
(recur (.getSuperclass c)))))) | ||
|
||
(defn check-base-interfaces | ||
[m ^Class the-class] | ||
(let [possibles (java.util.HashMap.)] | ||
(loop [c the-class] | ||
(when-not (= Object c) | ||
(doseq [itf (.getInterfaces c)] | ||
(when-let [impl (get m itf)] | ||
(.put possibles itf impl))) | ||
(recur (.getSuperclass c)))) | ||
(case (.size possibles) | ||
0 nil | ||
1 (first (vals possibles)) | ||
(throw (ex-info "More than one interface match for class" | ||
{:class the-class}))))) | ||
|
||
(defn lookup-impl | ||
[m the-class] | ||
(or (get m the-class) | ||
(check-base-classes m the-class) | ||
(check-base-interfaces m the-class) | ||
(get m Object))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters