I do occasionally drop stuff at https://weakreference.medium.com. See you there!
The Weak Reference
A blog on software development, and other extremely interesting things.
Kurser i Domain-Driven Design - VĂ¥ren 2012
Thursday, April 21, 2022
Sunday, October 21, 2012
Reflection in Clojure
I recently got a question about dynamically loading and instantiating Java classes in Clojure. The goal was to check if a certain library was available on the classpath. If it was available it should be loaded and used, if not, a simpler default version would be used instead.
Given Clojure's dynamic nature, it would be reasonable to believe that this is a simple thing. Perhaps it is but, I didn't find an obvious solution to this problem in pure Clojure. Luckily, since Clojure runs on the JVM and interops nicely with Java, there is a pretty straight-forward way of solving this; finding and instantiating the classes needed aren't too hard to accomplish using Java and some good old reflection.
In the code example below the class we are looking for has a useful default constructor. If that's not the case, instantiating the class requires a little bit more work. But once you have a reference to that instance, using it from Clojure is just like using any other Java class from Clojure!
In the example below we prefer to call on Joda Time to provide us with the current date and time, but if the library is not in our classpath, we fall back to using java.util.Date.
Update 2012-10-22:
My colleague Mattias came up with this much nicer version of the exists? function using clojure.reflect/resolve-class:
Given Clojure's dynamic nature, it would be reasonable to believe that this is a simple thing. Perhaps it is but, I didn't find an obvious solution to this problem in pure Clojure. Luckily, since Clojure runs on the JVM and interops nicely with Java, there is a pretty straight-forward way of solving this; finding and instantiating the classes needed aren't too hard to accomplish using Java and some good old reflection.
In the code example below the class we are looking for has a useful default constructor. If that's not the case, instantiating the class requires a little bit more work. But once you have a reference to that instance, using it from Clojure is just like using any other Java class from Clojure!
In the example below we prefer to call on Joda Time to provide us with the current date and time, but if the library is not in our classpath, we fall back to using java.util.Date.
(defn exists? [c] (let [loader (.getContextClassLoader (Thread/currentThread))] (try (Class/forName c false loader) true (catch ClassNotFoundException cnfe false)))) (defn print-java-date-now [] (println (java.util.Date.))) (defn print-joda-date-now [] (let [clazz (Class/forName "org.joda.time.DateTime")] (println (.newInstance clazz)))) (if (exists? "org.joda.time.DateTime") (print-joda-date-now) (print-java-date-now))
Update 2012-10-22:
My colleague Mattias came up with this much nicer version of the exists? function using clojure.reflect/resolve-class:
(defn exists? [c] (resolve-class (.getContextClassLoader (Thread/currentThread)) c))
Tuesday, March 27, 2012
Spring Break!
Last week the Citerus team spent a few fantastic days in Morocco. We try to take these trips somewhat regularly and over the years we have been to Italy, Czech Republic, and France among other places, but this trip was the first time we left Europe.
Citerus has been bringing quite a few new people on board over the last months, so this was a great opportunity to hang out with all our people for an extended time. Some for the first time as members of the Citerus team.
I must say I am amazed with the talents that have joined us for quite some time now. In fact, it blows me away. Spending time with my colleagues makes me feel old, slow and a bit stupid, and when building a great business, this is not a bad thing. I am very proud of being a part of this awesome group of people, you constantly learn new things, you are constantly challenged, and you always have fun.
Even though we bring our entire team together one day per month to share ideas and spend time together, doing this in new environments for an extended period really opens up a lot of great opportunities. The amazing views of the Atlas mountains as well as pool side work in Marrakech turned out to be perfect for creativity and focus. Look out for the new products we will be launching and exciting articles we will be publishing during the spring as a direct result of these last few days.
Feel like joining us? We’re hiring!
Citerus has been bringing quite a few new people on board over the last months, so this was a great opportunity to hang out with all our people for an extended time. Some for the first time as members of the Citerus team.
I must say I am amazed with the talents that have joined us for quite some time now. In fact, it blows me away. Spending time with my colleagues makes me feel old, slow and a bit stupid, and when building a great business, this is not a bad thing. I am very proud of being a part of this awesome group of people, you constantly learn new things, you are constantly challenged, and you always have fun.
Even though we bring our entire team together one day per month to share ideas and spend time together, doing this in new environments for an extended period really opens up a lot of great opportunities. The amazing views of the Atlas mountains as well as pool side work in Marrakech turned out to be perfect for creativity and focus. Look out for the new products we will be launching and exciting articles we will be publishing during the spring as a direct result of these last few days.
Feel like joining us? We’re hiring!
Monday, February 20, 2012
Sunday, January 16, 2011
Jfokus 2011
Jfokus 2011 is getting closer and this year looks fantastic! New venue, an additional day, and, as always, many interesting speakers. Tuesday February 15 has a DDD track with lots of great speakers, including Eric Evans.
Citerus is of course represented as a Jfokus partner also this year. We will be in the exhibition both conference days, come and see us!
Citerus is of course represented as a Jfokus partner also this year. We will be in the exhibition both conference days, come and see us!
Sunday, August 15, 2010
DDD classes survey
Eric Evans is doing a survey to guide future developments of the DDD classes that Citerus and other partners around the world offer together with Domain Language. If you are interested in DDD training, please take a moment to complete the survey. Thanks!
http://domainlanguage.com/survey/2010-08/
http://domainlanguage.com/survey/2010-08/
Friday, May 28, 2010
Meltingspot #4
Meltingspot #4 is now available. This time we start off with Daft Punk and end with Coldplay, while visiting both The Knife and Kraftwerk on our way.
Meltingspot #4 - This was meant to be, enjoy!
Follow us on Twitter @meltingspot, and stay tuned for our upcoming lists with music for the summer!
Meltingspot #4 - This was meant to be, enjoy!
Follow us on Twitter @meltingspot, and stay tuned for our upcoming lists with music for the summer!
Subscribe to:
Posts (Atom)