Last active
August 29, 2015 14:16
-
-
Save athos/4b53ae4446fcf76ee40f to your computer and use it in GitHub Desktop.
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
(ns redef-as-serializable.core) | |
(defmacro redef-as-serializable [& ts] | |
`(do ~@(for [t ts | |
:let [c (resolve t) | |
fields (mapv #(symbol (.getName %)) (.getDeclaredFields c))]] | |
`(deftype ~(symbol (str t \')) ~fields | |
java.io.Serializable)))) |
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
(ns redef-as-serializable.example | |
(:require [redef-as-serializable.core :refer [redef-as-serializable]]) | |
(:import [redef_as_serializable.types T1 T2])) | |
(redef-as-serializable T1 T2) | |
(comment | |
user=> (require 'redef-as-serializable.example) | |
nil | |
user=> (import '[redef_as_serializable.example T1' T2']) | |
redef_as_serializable.example.T2' | |
user=> (def fos (java.io.FileOutputStream. "t.tmp")) | |
#'user/fos | |
user=> (def oos (java.io.ObjectOutputStream. fos)) | |
#'user/oos | |
user=> (.writeObject oos (T1'. 0)) | |
nil | |
user=> (.close oos) | |
nil | |
user=> (def fis (java.io.FileInputStream. "t.tmp")) | |
#'user/fis | |
user=> (def ois (java.io.ObjectInputStream. fis)) | |
#'user/ois | |
user=> (.readObject ois) | |
#<T1' redef_as_serializable.example.T1'@41508995> | |
user=> (.x *1) | |
0 | |
user=> | |
) |
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
(defproject redef-as-serializable "0.1.0-SNAPSHOT" | |
:description "FIXME: write description" | |
:url "http://example.com/FIXME" | |
:license {:name "Eclipse Public License" | |
:url "http://www.eclipse.org/legal/epl-v10.html"} | |
:dependencies [[org.clojure/clojure "1.6.0"]] | |
:aot [redef-as-serializable.types | |
redef-as-serializable.example]) |
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
(ns redef-as-serializable.types) | |
(deftype T1 [x]) | |
(deftype T2 [x y]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment