<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <id>tag:blog.danieljanus.pl,2019:category:leiningen</id>
  <title>Daniel Janus – Leiningen</title>
  <link href="http://blog.danieljanus.pl/category/leiningen/"/>
  <updated>2010-03-31T00:00:00Z</updated>
  <author>
    <name>Daniel Janus</name>
    <uri>http://danieljanus.pl</uri>
    <email>dj@danieljanus.pl</email>
  </author>
  <entry>
    <id>tag:blog.danieljanus.pl,2010-03-31:post:lein-swank</id>
    <title>The pitfalls of &lt;code&gt;lein swank&lt;/code&gt;</title>
    <link href="http://blog.danieljanus.pl/lein-swank/"/>
    <updated>2010-03-31T00:00:00Z</updated>
    <content type="html">&lt;div&gt;&lt;p&gt;A couple of weeks ago I finally got around to acquainting myself with [Leiningen][1], one of the most popular build tools for Clojure. The thing that stopped me the most was that Leiningen uses [Maven][2] under the hood, which seemed a scary beast at first sight — but once I’ve overcome the initial fear, it turned out to be a quite simple and useful tool.&lt;/p&gt;&lt;p&gt;One feature in particular is very useful for Emacs users like me: &lt;code&gt;lein swank&lt;/code&gt;. You define all dependencies in &lt;code&gt;project.clj&lt;/code&gt; as usual, add a magical line to &lt;code&gt;:dev-dependencies&lt;/code&gt;, then say&lt;/p&gt;&lt;pre&gt;&lt;code&gt;$ lein swank
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;and lo and behold, you can &lt;code&gt;M-x slime-connect&lt;/code&gt; from your Emacs and have all the code at your disposal.&lt;/p&gt;&lt;p&gt;There is, however, an issue that you must be aware of when using &lt;code&gt;lein swank&lt;/code&gt;: Leiningen uses a custom class loader — [AntClassLoader][3] to be more precise — to load the Java classes referenced by the code. Despite being a seemingly irrelevant thing — an implementation detail — this can bite you in a number of most surprising and obscure ways. Try evaluating the following code in a Leiningen REPL:&lt;/p&gt;&lt;pre&gt;&lt;code class="hljs clojure"&gt;(&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;str&lt;/span&gt;&lt;/span&gt; (&lt;span class="hljs-name"&gt;.decode&lt;/span&gt;
       (&lt;span class="hljs-name"&gt;java.nio.charset.Charset/forName&lt;/span&gt; &lt;span class="hljs-string"&gt;&amp;quot;ISO-8859-2&amp;quot;&lt;/span&gt;)
       (&lt;span class="hljs-name"&gt;java.nio.ByteBuffer/wrap&lt;/span&gt;
         (&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;into-array&lt;/span&gt;&lt;/span&gt; Byte/TYPE (&lt;span class="hljs-name"&gt;&lt;span class="hljs-built_in"&gt;map&lt;/span&gt;&lt;/span&gt; byte [&lt;span class="hljs-number"&gt;-79&lt;/span&gt; &lt;span class="hljs-number"&gt;-26&lt;/span&gt; &lt;span class="hljs-number"&gt;-22&lt;/span&gt;])))))
&lt;span class="hljs-comment"&gt;;=&amp;gt; &amp;quot;???&amp;quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The same code evaluated in a plain Clojure REPL will give you &lt;code&gt;"ąćę"&lt;/code&gt;, which is a string represented in ISO-8859-2 by the three bytes from the above snippet.&lt;/p&gt;&lt;p&gt;Whence the difference? Internally, each charset is represented as a unique instance of its specific class. These are loaded lazily as needed by the &lt;code&gt;Charset/forName&lt;/code&gt; method. Presumably, the system class loader is used for that, and somewhere along the way a &lt;code&gt;SecurityException&lt;/code&gt; gets thrown and caught.&lt;/p&gt;&lt;p&gt;Note also that there are parts of Java API which use the charset lookup under the hood and are thus vulnerable to the same problem, for example &lt;code&gt;Reader&lt;/code&gt; constructors taking charset names. If you use &lt;code&gt;clojure.contrib.duck-streams&lt;/code&gt;, then rebinding &lt;code&gt;*default-encoding*&lt;/code&gt; will not work from a Leiningen REPL. Jars and überjars produced by Leiningen should be fine, though.&lt;/p&gt;&lt;/div&gt;</content>
  </entry>
</feed>
