How to call Java static method without arguments in Clojure?

I am trying to figure out how to call a method staticwith no arguments in Clojure. Two (bad) examples: (sun.misc.Unsafe/getUnsafe)and (Object/getClass), both of which generate CompilerException, called NoSuchFieldException.

Yes, I know that there is an easier way to call getClass, and I should not use sun.misc.Unsafeat all - just wondering how to call the static no-arg method in Clojure at all.

+4
source share
2 answers

Your examples do not work, but the following

(System/currentTimeMillis)
> 1398285925298

So, the way to call the static no-arg method.

Object/getClass not displayed as a static method. It should have been called on an object, not on a class.

+5

Unsafe . - ; . Java Magic. 4: sun.misc.Unsafe . Clojure, :

(let [f (.getDeclaredField sun.misc.Unsafe "theUnsafe")]
  (.setAccessible f true)
  (.get f nil))
;= #<Unsafe sun.misc.Unsafe@63124f52>

acomar WolfeFan, getClass - , Object , , :

(.getClass the-unsafe) ; the-unsafe obtained as above
;= sun.misc.Unsafe

, (Foo/meth) Clojure.

+3

Source: https://habr.com/ru/post/1537778/


All Articles