Is there a better way to get to `Classname[].class` ?

97 views
Skip to first unread message

pericles macedo

unread,
Oct 9, 2017, 11:38:37 AM10/9/17
to Clojure
Hey Guys,
I wanted to know if there is a better way to get to the same result as `GitlabProject[].class`
after some digging around, I got to this array of class definition by doing something like `(class (make-array GitlabProject 1))`. But I don't know if there is a better way to get to this same result.
In Java, this array of class is sent as an attribute that is used to build the array of objects that is returned. like: `retrieve().getAll(tailUrl, GitlabProject[].class);`


Thanks for your time,
Pericles Dantas

Gary Verhaegen

unread,
Oct 9, 2017, 12:20:37 PM10/9/17
to clo...@googlegroups.com
You can generally use ^"[fully.qualified.ClassName" as the type hint, if that's what you're trying to do. I'm not quite sure of the benefits of typing an array when it's not a primitive one, though.

What are you trying to accomplish, if we zoom back a little bit? Maybe there's a better way to do whatever you're trying to do.
--
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,
Oct 9, 2017, 12:21:24 PM10/9/17
to Clojure
(Class/forName "[Lorg.gitlab.api.models.GitlabProject;")

pericles macedo

unread,
Oct 9, 2017, 1:37:34 PM10/9/17
to Clojure
Hey Gary, thanks for the help
I'm not sure if a type hint would work in this case, as the attribute expect a class literal, and not a variable of that specific class. Thanks for the example though, I didn't know that was a valid type hint.
About what I'm trying to do, I'm using a gitlab api gem (https://github.com/timols/java-gitlab-api), and I need to do some java interop with it.
I'm trying to get a list of projects filtered by name, but the call java does doesn't build the search attribute, so I just get all projects, not filtered by name.
I'm able to get on java by using something like `connection.retrieve().getAll(buildUrl(project), GitlabProject[].class);`. The getAll expects a string, and a class literal.
If the return from gitlab wasn't an array, I could just use GitlabProject on clojure, and that would give me a result. As it's an array, I get an error showing it can't build a GitlabProject out of ARRAY_START.

My solution to make that call was 
(def get-project                                                                                               
    (memoize                                                                      
      (fn [project]                                                               
            (-> connection                                                                                       
                .retrieve                                                                                        
                (.getAll (build-url project) (array-class-for GitlabProject)))))) 
                                                                                                                 
(defn array-class-for [class-name]                                                                             
  (class (make-array class-name 1)))


pericles macedo

unread,
Oct 9, 2017, 1:37:34 PM10/9/17
to Clojure
Thanks Alex :)
Yes, that works better

Gary Verhaegen

unread,
Oct 10, 2017, 5:16:16 AM10/10/17
to clo...@googlegroups.com
What Alex suggested is probably the best approach here. Note: there's a typo in my message, the name of the class needs to be as Alex says for the type hint too, i.e. the L and ; are essential for this to work.

Peter Hull

unread,
Oct 10, 2017, 6:22:38 AM10/10/17
to Clojure
I actually preferred your solution, pericles, because it doesn't require memorising the "[L...;" syntax, and works if you don't know the class at compile time. By the way you can use make-array to create a zero size array which might be (ever so slightly) more efficient.

For reference (apologies if you already knew this), the class name syntax is outlined here:
https://docs.oracle.com/javase/9/docs/api/java/lang/Class.html#getName--
and for the full details
https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-4.html#jvms-4.7.9.1

pericles macedo

unread,
Oct 11, 2017, 11:27:46 AM10/11/17
to Clojure
Thanks Peter,
Yeah, I didn't find that reference before. It would have saved me a couple hours. I found something similar in a few places, but it only had the primitive types. It missed the class specific element type.
Reply all
Reply to author
Forward
0 new messages