Skip to content
Browse files

ch 9 gen-class example tweaks

  • Loading branch information...
1 parent 28368f1 commit fb44c29062248f8657cdb14ceb04fe7ba358f54a @cemerick cemerick committed Apr 3, 2012
Showing with 76 additions and 2 deletions.
  1. +58 −0 ch09-gen-class/README.md
  2. BIN ch09-gen-class/java-resize.png
  3. +18 −2 ch09-gen-class/project.clj
View
58 ch09-gen-class/README.md
@@ -0,0 +1,58 @@
+## _Clojure Programming_, Chapter 9
+
+### `gen-class`
+
+This project contains examples related to using `gen-class` to generate
+Java-style classes from within Clojure.
+
+
+#### "Wrapping" a Clojure API with a Java-friendly class
+
+The
+[`com.clojurebook.imaging`](src/com/clojurebook/imaging.clj)
+namespace defines a Clojure API for loading and resizing images. It then uses `gen-class` to produce a Java class (`ResizeImage` in the default package) whose static utility methods delegate to the API's `resize-image` function.
+
+You can see this in action by first building an uberjar containing all
+of the code in the project, and its dependencies:
+
+```
+$ lein uberjar
+```
+
+Then you can choose to run either `ResizeImage` directly:
+
+```
+$ java -cp target/gen-class-1.0.0-standalone.jar ResizeImage clojure.png small.png 0.3
+```
+
+…or you can run a [Java shim class](src/ResizeClient.java) that calls
+into `ResizeImage` to demonstrate that Java->Clojure interop:
+
+```
+$ java -cp target/gen-class-1.0.0-standalone.jar ResizeClient clojure.png java-small.png 0.3
+```
+
+#### A custom `Exception` type
+
+The
+[`com.clojurebook.CustomException`](src/com/clojurebook/CustomException.clj)
+namespace defines a custom `Exception` subclass that has a couple of interesting properties:
+
+* a `java.util.Map` value can be provided when constructing an instance
+ of `CustomException`
+* `CustomException` implements Clojure's `IDeref` interface, so
+ instances can be dereferenced (i.e. `@e`) to easily obtain the
+aforementioned `java.util.Map`.
+
+The [`BatchJob`](src/BatchJob.java) class shows an example of using this
+custom `Exception` type from Java. Assuming you've built an uberjar
+(`lein uberjar`), you can run `BatchJob`'s `main` method, which echos
+the contents of the exception's info map:
+
+```
+$ java -cp target/gen-class-1.0.0-standalone.jar BatchJobError! Operation failed {"timestamp" 1333488510315, "customer-id" 89045, "priority" "critical", "jobId" "verify-billings"}
+```
+
+
+
+
View
BIN ch09-gen-class/java-resize.png
Deleted file not rendered
View
20 ch09-gen-class/project.clj
@@ -1,4 +1,20 @@
(defproject com.clojurebook/gen-class "1.0.0"
+ :description "A set of examples showing how you can use the
+`gen-class` form to produce full-featured Java classes from Clojure.
+From chapter 9 of 'Clojure Programming' by Emerick, Carper, and Grand."
+ :url "http://github.com/clojurebook/ClojureProgramming"
:dependencies [[org.clojure/clojure "1.3.0"]]
- :aot :all
- :warn-on-reflection true)
+ :aot :all)
+
+(require '(leiningen compile javac)
+ 'robert.hooke)
+
+(robert.hooke/add-hook #'leiningen.compile/compile
+ (fn [compile project & args]
+ (apply compile project args)
+ (leiningen.javac/javac (assoc project
+ ;; Leiningen 1 uses :java-source-path
+ :java-source-path "src"
+ ;; Lein 2 uses :java-source-paths
+ :java-source-paths ["src"]))))
+

0 comments on commit fb44c29

Please sign in to comment.
Something went wrong with that request. Please try again.