Permalink
Browse files

Created project for beanhelpers

  • Loading branch information...
0 parents commit 43fe6c1c6e8209862f7de27389310f8ef38fcea7 @cosmin committed with Sep 8, 2010
@@ -0,0 +1,7 @@
+target
+*~
+*.iml
+/.ipr
+*.iws
+/.idea
+/ignore*
1 README
@@ -0,0 +1 @@
+Helpers for dealing with Java beans in Clojure
135 pom.xml
@@ -0,0 +1,135 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>com.offbytwo</groupId>
+ <artifactId>beanhelpers</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <packaging>jar</packaging>
+
+ <name>Bean Helpers</name>
+ <url>http://github.com/offbytwo/beanhelpers</url>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <clojure.version>1.2.0</clojure.version>
+ </properties>
+
+ <build>
+ <resources>
+ <resource>
+ <filtering>true</filtering>
+ <directory>src/main/resources/</directory>
+ </resource>
+ </resources>
+
+ <plugins>
+ <plugin>
+ <groupId>com.theoryinpractise</groupId>
+ <artifactId>clojure-maven-plugin</artifactId>
+ <version>1.3.2</version>
+ <configuration>
+ <sourceDirectories>
+ <sourceDirectory>src/main/clojure</sourceDirectory>
+ </sourceDirectories>
+ <testSourceDirectories>
+ <testSourceDirectory>src/test/clojure</testSourceDirectory>
+ </testSourceDirectories>
+ <script>src/main/clojure/console.clj</script>
+ <replScript>src/main/clojure/console.clj</replScript>
+ </configuration>
+ <executions>
+ <execution>
+ <id>compile</id>
+ <phase>compile</phase>
+ <goals>
+ <goal>compile</goal>
+ </goals>
+ </execution>
+ <execution>
+ <id>test</id>
+ <phase>test</phase>
+ <goals>
+ <goal>test</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.clojure</groupId>
+ <artifactId>clojure</artifactId>
+ <version>${clojure.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.clojure</groupId>
+ <artifactId>clojure-contrib</artifactId>
+ <version>${clojure.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>swank-clojure</groupId>
+ <artifactId>swank-clojure</artifactId>
+ <version>1.2.1</version>
+ </dependency>
+ <dependency>
+ <groupId>jline</groupId>
+ <artifactId>jline</artifactId>
+ <version>0.9.94</version>
+ </dependency>
+
+ <dependency>
+ <groupId>joda-time</groupId>
+ <artifactId>joda-time</artifactId>
+ <version>1.6</version>
+ </dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>3.8.1</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ <version>1.2.15</version>
+ <exclusions>
+ <exclusion>
+ <groupId>javax.jms</groupId>
+ <artifactId>jms</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>com.sun.jmx</groupId>
+ <artifactId>jmxri</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>com.sun.jdmk</groupId>
+ <artifactId>jmxtools</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>javax.mail</groupId>
+ <artifactId>mail</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ </dependencies>
+ <repositories>
+ <repository>
+ <id>clojure-releases</id>
+ <url>http://build.clojure.org/releases</url>
+ <releases>
+ <enabled>true</enabled>
+ </releases>
+ <snapshots>
+ <enabled>false</enabled>
+ </snapshots>
+ </repository>
+ <repository>
+ <id>clojars</id>
+ <url>http://clojars.org/repo</url>
+ </repository>
+ </repositories>
+</project>
@@ -0,0 +1,4 @@
+(in-ns 'user)
+(use 'clojure.repl)
+(use ['clojure.contrib.repl-utils :only ['show]])
+;; add your repl initialization code here
@@ -0,0 +1,67 @@
+(ns offbytwo.beanhelpers)
+
+(defmulti to-java (fn [destination-type value] [destination-type (class value)]))
+(defmulti from-java class)
+
+;(derive Boolean :primitive)
+;(derive Integer :primitive)
+;(derive String :primitive)
+
+
+(defn- get-property-descriptors [clazz]
+ (.getPropertyDescriptors (java.beans.Introspector/getBeanInfo clazz)))
+
+(defn- is-getter [method]
+ (and method (= 0 (alength (. method (getParameterTypes))))))
+
+(defn- is-setter [method]
+ (and method (= 1 (alength (. method (getParameterTypes))))))
+
+(defn- get-setter-type [method]
+ (get (.getParameterTypes method) 0))
+
+(defn- make-getter-fn [method]
+ (fn [instance]
+ (.invoke method instance nil)))
+
+(defn- make-setter-fn [method]
+ (fn [instance value]
+ (.invoke method instance (into-array [(to-java value (get-setter-type method))]))))
+
+(defn- add-getter-fn [the-map prop-descriptor]
+ (let [name (.getName prop-descriptor)
+ method (.getReadMethod prop-descriptor)]
+ (if (is-getter method)
+ (assoc the-map (keyword name) (make-getter-fn method)))))
+
+
+(defn- add-setter-fn [the-map prop-descriptor]
+ (let [name (.getName prop-descriptor)
+ method (.getWriteMethod prop-descriptor)]
+ (if (is-setter method)
+ (assoc the-map (keyword name) (make-setter-fn method))
+ the-map)))
+
+
+(defmethod to-java [Enum String] [enum value]
+ (.invoke (.getDeclaredMethod enum "valueOf" (into-array [String])) nil (into-array [value])))
+
+(defmethod to-java [Object clojure.lang.APersistentMap] [clazz props]
+ (let [instance (.newInstance clazz)
+ setter-map (reduce add-setter-fn {} (get-property-descriptors clazz))]
+ (doseq [[key value] props]
+ (let [setter (get setter-map (keyword key))]
+ (if (nil? setter)
+ (println "WARNING: Cannot set value for " key " because there is no setter.")
+ (apply setter [instance value]))))
+ instance))
+
+(defmethod to-java :default [_ value] value)
+
+
+;(defmethod from-java )
+
+(defmethod from-java Object [instance]
+ (let [clazz (.getClass instance)
+ getter-map (reduce add-getter-fn {} (get-property-descriptors clazz))]
+ (reduce add-getter-fn {} (get-property-descriptors clazz))))
No changes.
No changes.
No changes.
No changes.
No changes.

0 comments on commit 43fe6c1

Please sign in to comment.