zcaudate
zcaudate

Reputation: 14258

how to use the bean function in clojure

I don't really understand how the bean function works when I am using it on beans. This code here throws an exception:

(import java.lang.management.ManagementFactory)
(def runtime (bean (ManagementFactory/getRuntimeMXBean))
(:name runtime)


;; =>
Class clojure.core$bean$fn__5177$fn__5178 can not access a member of class sun.management.RuntimeImpl with modifiers "public"
  [Thrown class java.lang.IllegalAccessException]

but the class does have a method called getName(). http://docs.oracle.com/javase/6/docs/api/java/lang/management/RuntimeMXBean.html

Upvotes: 1

Views: 366

Answers (2)

gtrak
gtrak

Reputation: 5676

I think this is captured by a bug right here: http://dev.clojure.org/jira/browse/CLJ-978?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel

Upvote it!

You can roll-your-own bean function using the available patch.

Upvotes: 0

Ivan Koblik
Ivan Koblik

Reputation: 4325

You must have missed it, it's right there.

getName() Returns the name representing the runn (import java.lang.management.ManagementFactory) (def runtime (bean (ManagementFactory/getRuntimeMXBean)) (:name runtime)

;; => Class clojure.core$bean$fn_5177$fn_5178 can not access a member of class sun.management.RuntimeImpl with modifiers "public" [Thrown class java.lang.IllegalAccessException]

but the class does have a method called getName(). http://docs.oracle.com/javase/6/docs/api/java/lang/management/RuntimeMXBean.html

ing Java virtual machine.

Edit1:

This issue is very similar to one here and response to it is here.

Class RuntimeImpl returned by ManagementFactory.getRuntimeMXBean is only package visible but it implements public visible interface RuntimeMXBean. So what happens is that function bean tries to call a method on the class RuntimImpl but it can't as class is only package visible. I think if it tried to call getMethod on interface RuntimeMXBean it would have worked.


Yeap this works:

(def mx-bean (ManagementFactory/getRuntimeMXBean))
(def interface-method 
  (.getMethod RuntimeMXBean "getName" (into-array java.lang.Class [])))
(.invoke interface-method mx-bean (into-array []))

I'm not sure if it's a bug or feature. I would recommend asking on Clojure mailing list.

Upvotes: 2

Related Questions