decoding clojure generated class names

84 views
Skip to first unread message

Dave Tenny

unread,
Aug 22, 2015, 7:07:22 PM8/22/15
to Clojure
I wanted to write a function like this:

(defn fn-name-info
  "Given a function, try to deduce the function name and clojure namespace by looking 
  at the function's java class name and decoding stuff.  Return a vector of name, namespace strings, 
  or nil if we can't find anything.
  
  For example, if in jdt.core we do (defn foo []), the resulting value foo is a thing whose class is
  jdt.core$foo, for which we can return [\"jdt.core\" \"foo\"].
  
  However, for anonymous functions like (fn []) typed into the REPL, 
  you're going to get something like [\"jdt.core\" \"eval3676$fn__3677\"]"
  [f]
  )

Surely someone out there has a bit of code that knows how to decode classnames accounting for
many of the weird edge cases, question marks (e.g. "QMARK"), and so on?

Can anybody help me out?

FYI, the next step I had planned for this was for me to reach take that fn information, look for the var that defines it, and yank out some metadata, when all I have is the function pointer, not the var.

Tips appreciated.

Yes, I know it's probably a bad idea for a couple of reasons, but feel free to point them out.


Colin Fleming

unread,
Aug 22, 2015, 7:13:22 PM8/22/15
to clo...@googlegroups.com
Hi Dave,

All the various stacktrace-prettifying libraries (clj-stacktrace, Aviso pretty etc) all do this - check their code out for some examples.

Cheers,
Colin

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alex Miller

unread,
Aug 22, 2015, 7:28:32 PM8/22/15
to Clojure
There is a demunge function in clojure.lang.Compiler. It's not foolproof as the munging is not unambiguously reversible.

Fluid Dynamics

unread,
Aug 22, 2015, 8:41:15 PM8/22/15
to Clojure
On Saturday, August 22, 2015 at 7:28:32 PM UTC-4, Alex Miller wrote:
There is a demunge function in clojure.lang.Compiler. It's not foolproof as the munging is not unambiguously reversible.

If the munging process loses information, isn't there a (theoretical, at least) risk of name collisions in the output?

Alex Miller

unread,
Aug 22, 2015, 9:51:12 PM8/22/15
to Clojure
Yes. Specifically, - and _ both map to _ so foo-bar and foo_bar generate the same class name. So don't do that.

Dave Tenny

unread,
Aug 23, 2015, 10:53:58 AM8/23/15
to clo...@googlegroups.com
Thanks Alex, clojure.lang.Compiler/demunge is just what I wanted.

On Sat, Aug 22, 2015 at 7:28 PM, Alex Miller <al...@puredanger.com> wrote:
There is a demunge function in clojure.lang.Compiler. It's not foolproof as the munging is not unambiguously reversible.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/5-l7bxFSOxk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages