Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 814013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:27:13+00:00 2026-05-15T01:27:13+00:00

I’m a few days into learning Clojure and are having some teething problems, so

  • 0

I’m a few days into learning Clojure and are having some teething problems, so I’m asking for advice.

I’m trying to store a Java class in a Clojure var and call its static methods, but it doesn’t work.

Example:

user=> (. java.lang.reflect.Modifier isPrivate 1)
false
user=> (def jmod java.lang.reflect.Modifier)
#'user/jmod
user=> (. jmod isPrivate 1)
java.lang.IllegalArgumentException: No matching method found: isPrivate for class java.lang.Class (NO_SOURCE_FILE:0)
    at clojure.lang.Compiler.eval(Compiler.java:4543)

From the exception it looks like the runtime expects a var to hold an object, so it calls .getClass() to get the class and looks up the method using reflection. In this case the var already holds a class, so .getClass() returns java.lang.Class and the method lookup obviously fails.

Is there some way around this, other than writing my own macro?

In the general case I’d like to have either an object or a class in a varible and call the appropriate methods on it – duck typing for static methods as well as for instance methods.

In this specific case I’d just like a shorter name for java.lang.reflect.Modifier, an alias if you wish. I know about import, but looking for something more general, like the Clojure namespace alias but for Java classes. Are there other mechanisms for doing this?

Edit:

Maybe I’m just confused about the calling conventions here. I thought the Lisp (and by extension Clojure) model was to evaluate all arguments and call the first element in the list as a function.

In this case (= jmod java.lang.reflect.Modifier) returns true, and (.getName jmod) and (.getName java.lang.reflect.Modifier) both return the same string.

So the variable and the class name clearly evaluate to the same thing, but they still cannot be called in the same fashion. What’s going on here?

Edit 2

Answering my second question (what is happening here), the Clojure doc says that

If the first operand is a symbol that
resolves to a class name, the access
is considered to be to a static member
of the named class… Otherwise it is
presumed to be an instance member

http://clojure.org/java_interop under “The Dot special form”

“Resolving to a class name” is apparently not the same as “evaluating to something that resolves to a class name”, so what I am trying to do here is not supported by the dot special form.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T01:27:14+00:00Added an answer on May 15, 2026 at 1:27 am

    (Update: I’ve prepared something which might be acceptable as a solution… The original answer remains below a horizontal rule towards the end of the post.)


    I’ve just written a macro to enable this:

    (adapter-ns java.lang.reflect.Modifier jmod)
    ; => nil
    (jmod/isStatic 1)
    ; => false
    (jmod/isStatic 8)
    ; => true
    

    The idea is to create a single-purpose namespace, import the statics of a given class as Vars into that namespace, then alias the namespace to some useful name. Convoluted, but it works! 🙂

    The code is looks like this:

    (defmacro import-all-statics
      "code stolen from clojure.contrib.import-static/import-static"
      [c]
      (let [the-class (. Class forName (str c))
            static? (fn [x]
                      (. java.lang.reflect.Modifier
                         (isStatic (. x (getModifiers)))))
            statics (fn [array]
                      (set (map (memfn getName)
                                (filter static? array))))
            all-fields (statics (. the-class (getFields)))
            all-methods (statics (. the-class (getMethods)))
            import-field (fn [name]
                           (list 'def (symbol name)
                                 (list '. c (symbol name))))
            import-method (fn [name]
                            (list 'defmacro (symbol name)
                                  '[& args]
                                  (list 'list ''. (list 'quote c)
                                        (list 'apply 'list
                                              (list 'quote (symbol name))
                                              'args))))]
        `(do ~@(map import-field all-fields)
             ~@(map import-method all-methods))))
    
    (defmacro adapter-ns [c n]
      (let [ias (symbol (-> (resolve 'import-all-statics) .ns .name name)
                        "import-all-statics")]
        `(let [ns-sym# (gensym (str "adapter_" ~n))]
           (create-ns 'ns-sym#)
           (with-ns 'ns-sym#
             (clojure.core/refer-clojure)
             (~ias ~c))
           (alias '~n 'ns-sym#))))
    

    The above looks up the Var holding the import-all-statics macro in a somewhat convoluted way (which is, however, guaranteed to work if the macro is visible from the current namespace). If you know which namespace it’s going to be found in, the original version I’ve written is a simpler replacement:

    (defmacro adapter-ns [c n]
      `(let [ns-sym# (gensym (str "adapter_" ~n))]
         (create-ns 'ns-sym#)
         (with-ns 'ns-sym#
           (clojure.core/refer-clojure)
           ;; NB. the "user" namespace is mentioned below;
           ;; change as appropriate
           (user/import-all-statics ~c))
         (alias '~n 'ns-sym#)))
    

    (Original answer below.)

    I realise that this is not really what you’re asking for, but perhaps clojure.contrib.import-static/import-static will be useful to you:

    (use 'clojure.contrib.import-static)
    
    (import-static clojure.lang.reflect.Modifier isPrivate)
    
    (isPrivate 1)
    ; => false
    (isPrivate 2)
    ; => true
    

    Note that import-static imports static methods as macros.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 475k
  • Answers 475k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Replace Scenario: with Scenario Outline: May 16, 2026 at 4:43 am
  • Editorial Team
    Editorial Team added an answer Unfortunately this can only work if the web server in… May 16, 2026 at 4:43 am
  • Editorial Team
    Editorial Team added an answer In a comment on your previous question you mentioned http://www.gallot.be/?p=85,… May 16, 2026 at 4:43 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.