Clojure, reflection: Find classes that implement an interface









up vote
0
down vote

favorite












This seems to be a more difficult in Clojure than in Java and Scala. What I want to do is:



  1. Define an interface in Java

  2. Implement it in a class in Clojure (Bonus: with a macro)

  3. Find it using the classloader and reflection

This is what I have so far:



The Java interface



package hello.interfaces;
public interface Test
String doSomething(String input);



The Clojure definition



(ns hello.impl.test
(:gen-class
:implements [hello.interfaces.Test]))

(defn doSomething [v] (str "hello " v))


Searching for the classes:



(ns hello.core
(:import
(org.reflections Reflections)
(org.reflections.util ConfigurationBuilder))
(:gen-class))

(defn -instances
(let [paths (into-array Object ["hello"])]
(.getSubTypesOf (Reflections. paths) hello.interfaces.Test)))

(defn -main [& args]
(println (-instances)))


I am using org.reflections. This works if I search for classes that are in the classpath (e.g. in the org.reflections jar) but it does not work for my previously defined class, therefore I think that the problem is not in the last snippet of code but in the previous one, or maybe in the usage, e.g. it requires pre-compiling it.



How can I define classes in Clojure that I can find later with reflection?










share|improve this question





















  • I must say that I am very new to Clojure, this is in fact the first piece of code that I am trying to write, so sorry for the noob question. My main interest in Clojure is in metaprogramming, so I hope that the implementation of interfaces using macros is compatible with this usage. In Scala (previous version) I am using implicit conversions instead of macros, but macros seem better to avoid boilerplate.
    – Trylks
    Nov 8 at 14:22










  • never used Java interfaces in such a way, just making sure that you are aware of instance? - stackoverflow.com/a/12170685/1327651
    – nha
    Nov 8 at 14:56










  • @nha I am not aware of most of Clojure things yet. In this case, instance? is not a suitable option, as it works with objects to check if they are instances of a class. I am searching for classes, and once found the first thing to do is call their constructors to build their corresponding objects. What I am trying to do may not be conventional.
    – Trylks
    Nov 8 at 18:12














up vote
0
down vote

favorite












This seems to be a more difficult in Clojure than in Java and Scala. What I want to do is:



  1. Define an interface in Java

  2. Implement it in a class in Clojure (Bonus: with a macro)

  3. Find it using the classloader and reflection

This is what I have so far:



The Java interface



package hello.interfaces;
public interface Test
String doSomething(String input);



The Clojure definition



(ns hello.impl.test
(:gen-class
:implements [hello.interfaces.Test]))

(defn doSomething [v] (str "hello " v))


Searching for the classes:



(ns hello.core
(:import
(org.reflections Reflections)
(org.reflections.util ConfigurationBuilder))
(:gen-class))

(defn -instances
(let [paths (into-array Object ["hello"])]
(.getSubTypesOf (Reflections. paths) hello.interfaces.Test)))

(defn -main [& args]
(println (-instances)))


I am using org.reflections. This works if I search for classes that are in the classpath (e.g. in the org.reflections jar) but it does not work for my previously defined class, therefore I think that the problem is not in the last snippet of code but in the previous one, or maybe in the usage, e.g. it requires pre-compiling it.



How can I define classes in Clojure that I can find later with reflection?










share|improve this question





















  • I must say that I am very new to Clojure, this is in fact the first piece of code that I am trying to write, so sorry for the noob question. My main interest in Clojure is in metaprogramming, so I hope that the implementation of interfaces using macros is compatible with this usage. In Scala (previous version) I am using implicit conversions instead of macros, but macros seem better to avoid boilerplate.
    – Trylks
    Nov 8 at 14:22










  • never used Java interfaces in such a way, just making sure that you are aware of instance? - stackoverflow.com/a/12170685/1327651
    – nha
    Nov 8 at 14:56










  • @nha I am not aware of most of Clojure things yet. In this case, instance? is not a suitable option, as it works with objects to check if they are instances of a class. I am searching for classes, and once found the first thing to do is call their constructors to build their corresponding objects. What I am trying to do may not be conventional.
    – Trylks
    Nov 8 at 18:12












up vote
0
down vote

favorite









up vote
0
down vote

favorite











This seems to be a more difficult in Clojure than in Java and Scala. What I want to do is:



  1. Define an interface in Java

  2. Implement it in a class in Clojure (Bonus: with a macro)

  3. Find it using the classloader and reflection

This is what I have so far:



The Java interface



package hello.interfaces;
public interface Test
String doSomething(String input);



The Clojure definition



(ns hello.impl.test
(:gen-class
:implements [hello.interfaces.Test]))

(defn doSomething [v] (str "hello " v))


Searching for the classes:



(ns hello.core
(:import
(org.reflections Reflections)
(org.reflections.util ConfigurationBuilder))
(:gen-class))

(defn -instances
(let [paths (into-array Object ["hello"])]
(.getSubTypesOf (Reflections. paths) hello.interfaces.Test)))

(defn -main [& args]
(println (-instances)))


I am using org.reflections. This works if I search for classes that are in the classpath (e.g. in the org.reflections jar) but it does not work for my previously defined class, therefore I think that the problem is not in the last snippet of code but in the previous one, or maybe in the usage, e.g. it requires pre-compiling it.



How can I define classes in Clojure that I can find later with reflection?










share|improve this question













This seems to be a more difficult in Clojure than in Java and Scala. What I want to do is:



  1. Define an interface in Java

  2. Implement it in a class in Clojure (Bonus: with a macro)

  3. Find it using the classloader and reflection

This is what I have so far:



The Java interface



package hello.interfaces;
public interface Test
String doSomething(String input);



The Clojure definition



(ns hello.impl.test
(:gen-class
:implements [hello.interfaces.Test]))

(defn doSomething [v] (str "hello " v))


Searching for the classes:



(ns hello.core
(:import
(org.reflections Reflections)
(org.reflections.util ConfigurationBuilder))
(:gen-class))

(defn -instances
(let [paths (into-array Object ["hello"])]
(.getSubTypesOf (Reflections. paths) hello.interfaces.Test)))

(defn -main [& args]
(println (-instances)))


I am using org.reflections. This works if I search for classes that are in the classpath (e.g. in the org.reflections jar) but it does not work for my previously defined class, therefore I think that the problem is not in the last snippet of code but in the previous one, or maybe in the usage, e.g. it requires pre-compiling it.



How can I define classes in Clojure that I can find later with reflection?







java reflection interface clojure classpath






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 14:18









Trylks

7101125




7101125











  • I must say that I am very new to Clojure, this is in fact the first piece of code that I am trying to write, so sorry for the noob question. My main interest in Clojure is in metaprogramming, so I hope that the implementation of interfaces using macros is compatible with this usage. In Scala (previous version) I am using implicit conversions instead of macros, but macros seem better to avoid boilerplate.
    – Trylks
    Nov 8 at 14:22










  • never used Java interfaces in such a way, just making sure that you are aware of instance? - stackoverflow.com/a/12170685/1327651
    – nha
    Nov 8 at 14:56










  • @nha I am not aware of most of Clojure things yet. In this case, instance? is not a suitable option, as it works with objects to check if they are instances of a class. I am searching for classes, and once found the first thing to do is call their constructors to build their corresponding objects. What I am trying to do may not be conventional.
    – Trylks
    Nov 8 at 18:12
















  • I must say that I am very new to Clojure, this is in fact the first piece of code that I am trying to write, so sorry for the noob question. My main interest in Clojure is in metaprogramming, so I hope that the implementation of interfaces using macros is compatible with this usage. In Scala (previous version) I am using implicit conversions instead of macros, but macros seem better to avoid boilerplate.
    – Trylks
    Nov 8 at 14:22










  • never used Java interfaces in such a way, just making sure that you are aware of instance? - stackoverflow.com/a/12170685/1327651
    – nha
    Nov 8 at 14:56










  • @nha I am not aware of most of Clojure things yet. In this case, instance? is not a suitable option, as it works with objects to check if they are instances of a class. I am searching for classes, and once found the first thing to do is call their constructors to build their corresponding objects. What I am trying to do may not be conventional.
    – Trylks
    Nov 8 at 18:12















I must say that I am very new to Clojure, this is in fact the first piece of code that I am trying to write, so sorry for the noob question. My main interest in Clojure is in metaprogramming, so I hope that the implementation of interfaces using macros is compatible with this usage. In Scala (previous version) I am using implicit conversions instead of macros, but macros seem better to avoid boilerplate.
– Trylks
Nov 8 at 14:22




I must say that I am very new to Clojure, this is in fact the first piece of code that I am trying to write, so sorry for the noob question. My main interest in Clojure is in metaprogramming, so I hope that the implementation of interfaces using macros is compatible with this usage. In Scala (previous version) I am using implicit conversions instead of macros, but macros seem better to avoid boilerplate.
– Trylks
Nov 8 at 14:22












never used Java interfaces in such a way, just making sure that you are aware of instance? - stackoverflow.com/a/12170685/1327651
– nha
Nov 8 at 14:56




never used Java interfaces in such a way, just making sure that you are aware of instance? - stackoverflow.com/a/12170685/1327651
– nha
Nov 8 at 14:56












@nha I am not aware of most of Clojure things yet. In this case, instance? is not a suitable option, as it works with objects to check if they are instances of a class. I am searching for classes, and once found the first thing to do is call their constructors to build their corresponding objects. What I am trying to do may not be conventional.
– Trylks
Nov 8 at 18:12




@nha I am not aware of most of Clojure things yet. In this case, instance? is not a suitable option, as it works with objects to check if they are instances of a class. I am searching for classes, and once found the first thing to do is call their constructors to build their corresponding objects. What I am trying to do may not be conventional.
– Trylks
Nov 8 at 18:12












2 Answers
2






active

oldest

votes

















up vote
1
down vote













I'm not familiar with org.reflections, but if you just want a list of loaded classes, you can get it with the below code:



(let [classloader (.getClassLoader clojure.main)
classes-field (.getDeclaredField java.lang.ClassLoader "classes")]
(.setAccessible classes-field true)
(let [class-list (.get classes-field classloader)
class-vec (reduce conj class-list)] ; copy everything into a new vector rather than working directly with the classloader's private field
class-vec))


It sounds like you're familiar with Java, so I guess you can see the above is basically just translated Java. It will only give you the classes which have been loaded with the same class loader as that for the class clojure.main, but if you haven't done any customisation with your class loaders, that should be enough.



Once you have that list, you can search/filter it however you want. Of course, the class you're looking for does have to have been loaded first. If that's not the case, you'll have to search the classpath instead.



=== UPDATE to respond to your comment ===



OK I see, you're asking how to create a class. The first thing to say is that you don't generally need to create named classes when you're writing Clojure, unless you specifically want to use some existing Java code which requires you to do so. If you're writing pure Clojure, you just write your functions and work with them directly.



However, you can of course do so. The first part of the doc for gen-class states:




=> (doc gen-class)



clojure.core/gen-class



([& options])



Macro



When compiling, generates compiled bytecode for a class with the
given package-qualified :name (which, as all names in these
parameters, can be a string or symbol), and writes the .class file
to the compile-path directory. When not compiling, does
nothing.




So, you need to compile your namespace. I don't normally do this so I don't know if there's a way to do it without creating .class files, and just creating the classes directly in memory, but the below does what you want, if I've understood you correctly:



(ns wip.test)

; looks for wip/himplement.clj on the classpath, and compiles it into .class files
; requires that ../bin is in the classpath
(binding [*compile-path* "../bin"]
(compile 'wip.himplement))

; loads the wip.himplement class from the .class files
(Class/forName "wip.himplement")


; create a list of all loaded classes (could presumably also be done with org.reflections)
(def classes-list (let [classloader (.getClassLoader clojure.main)
classes-field (.getDeclaredField java.lang.ClassLoader "classes")]
(.setAccessible classes-field true)
(java.util.ArrayList. (.get classes-field classloader))))

; Outputs all loaded classes which implement HInterface. Output is:
; (wip.hello.HInterface wip.himplement)
(println (filter #(isa? % wip.hello.HInterface) classes-list))





share|improve this answer






















  • Thank you.org.reflections gives me the classes that implement an interface, but I could not create a class implementing an interface in Clojure. Here is the corresponding code: github.com/trylks/wip you can just do lein run
    – Trylks
    Nov 9 at 20:32










  • @Trylks see my updated post
    – OpenSauce
    Nov 10 at 20:27

















up vote
0
down vote













I'm not totally sure what you're trying to achieve, but there are a few things that I should probably point you in the direction of. Firstly there's a namespace called clojure.reflect that contains useful functions for getting info about classes. It makes it easier to reflect the Java hierarchy at the REPL. Secondly there's things like clojure.tools.namespace that will help you traverse the code in your repo and find all the namespaces that implement interfaces. However, you tend to use deftype to implement classes rather than using the AOT compiler features like the :gen-class option in the ns macro, unless you're implementing something specific like a servelet or something.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53209619%2fclojure-reflection-find-classes-that-implement-an-interface%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    I'm not familiar with org.reflections, but if you just want a list of loaded classes, you can get it with the below code:



    (let [classloader (.getClassLoader clojure.main)
    classes-field (.getDeclaredField java.lang.ClassLoader "classes")]
    (.setAccessible classes-field true)
    (let [class-list (.get classes-field classloader)
    class-vec (reduce conj class-list)] ; copy everything into a new vector rather than working directly with the classloader's private field
    class-vec))


    It sounds like you're familiar with Java, so I guess you can see the above is basically just translated Java. It will only give you the classes which have been loaded with the same class loader as that for the class clojure.main, but if you haven't done any customisation with your class loaders, that should be enough.



    Once you have that list, you can search/filter it however you want. Of course, the class you're looking for does have to have been loaded first. If that's not the case, you'll have to search the classpath instead.



    === UPDATE to respond to your comment ===



    OK I see, you're asking how to create a class. The first thing to say is that you don't generally need to create named classes when you're writing Clojure, unless you specifically want to use some existing Java code which requires you to do so. If you're writing pure Clojure, you just write your functions and work with them directly.



    However, you can of course do so. The first part of the doc for gen-class states:




    => (doc gen-class)



    clojure.core/gen-class



    ([& options])



    Macro



    When compiling, generates compiled bytecode for a class with the
    given package-qualified :name (which, as all names in these
    parameters, can be a string or symbol), and writes the .class file
    to the compile-path directory. When not compiling, does
    nothing.




    So, you need to compile your namespace. I don't normally do this so I don't know if there's a way to do it without creating .class files, and just creating the classes directly in memory, but the below does what you want, if I've understood you correctly:



    (ns wip.test)

    ; looks for wip/himplement.clj on the classpath, and compiles it into .class files
    ; requires that ../bin is in the classpath
    (binding [*compile-path* "../bin"]
    (compile 'wip.himplement))

    ; loads the wip.himplement class from the .class files
    (Class/forName "wip.himplement")


    ; create a list of all loaded classes (could presumably also be done with org.reflections)
    (def classes-list (let [classloader (.getClassLoader clojure.main)
    classes-field (.getDeclaredField java.lang.ClassLoader "classes")]
    (.setAccessible classes-field true)
    (java.util.ArrayList. (.get classes-field classloader))))

    ; Outputs all loaded classes which implement HInterface. Output is:
    ; (wip.hello.HInterface wip.himplement)
    (println (filter #(isa? % wip.hello.HInterface) classes-list))





    share|improve this answer






















    • Thank you.org.reflections gives me the classes that implement an interface, but I could not create a class implementing an interface in Clojure. Here is the corresponding code: github.com/trylks/wip you can just do lein run
      – Trylks
      Nov 9 at 20:32










    • @Trylks see my updated post
      – OpenSauce
      Nov 10 at 20:27














    up vote
    1
    down vote













    I'm not familiar with org.reflections, but if you just want a list of loaded classes, you can get it with the below code:



    (let [classloader (.getClassLoader clojure.main)
    classes-field (.getDeclaredField java.lang.ClassLoader "classes")]
    (.setAccessible classes-field true)
    (let [class-list (.get classes-field classloader)
    class-vec (reduce conj class-list)] ; copy everything into a new vector rather than working directly with the classloader's private field
    class-vec))


    It sounds like you're familiar with Java, so I guess you can see the above is basically just translated Java. It will only give you the classes which have been loaded with the same class loader as that for the class clojure.main, but if you haven't done any customisation with your class loaders, that should be enough.



    Once you have that list, you can search/filter it however you want. Of course, the class you're looking for does have to have been loaded first. If that's not the case, you'll have to search the classpath instead.



    === UPDATE to respond to your comment ===



    OK I see, you're asking how to create a class. The first thing to say is that you don't generally need to create named classes when you're writing Clojure, unless you specifically want to use some existing Java code which requires you to do so. If you're writing pure Clojure, you just write your functions and work with them directly.



    However, you can of course do so. The first part of the doc for gen-class states:




    => (doc gen-class)



    clojure.core/gen-class



    ([& options])



    Macro



    When compiling, generates compiled bytecode for a class with the
    given package-qualified :name (which, as all names in these
    parameters, can be a string or symbol), and writes the .class file
    to the compile-path directory. When not compiling, does
    nothing.




    So, you need to compile your namespace. I don't normally do this so I don't know if there's a way to do it without creating .class files, and just creating the classes directly in memory, but the below does what you want, if I've understood you correctly:



    (ns wip.test)

    ; looks for wip/himplement.clj on the classpath, and compiles it into .class files
    ; requires that ../bin is in the classpath
    (binding [*compile-path* "../bin"]
    (compile 'wip.himplement))

    ; loads the wip.himplement class from the .class files
    (Class/forName "wip.himplement")


    ; create a list of all loaded classes (could presumably also be done with org.reflections)
    (def classes-list (let [classloader (.getClassLoader clojure.main)
    classes-field (.getDeclaredField java.lang.ClassLoader "classes")]
    (.setAccessible classes-field true)
    (java.util.ArrayList. (.get classes-field classloader))))

    ; Outputs all loaded classes which implement HInterface. Output is:
    ; (wip.hello.HInterface wip.himplement)
    (println (filter #(isa? % wip.hello.HInterface) classes-list))





    share|improve this answer






















    • Thank you.org.reflections gives me the classes that implement an interface, but I could not create a class implementing an interface in Clojure. Here is the corresponding code: github.com/trylks/wip you can just do lein run
      – Trylks
      Nov 9 at 20:32










    • @Trylks see my updated post
      – OpenSauce
      Nov 10 at 20:27












    up vote
    1
    down vote










    up vote
    1
    down vote









    I'm not familiar with org.reflections, but if you just want a list of loaded classes, you can get it with the below code:



    (let [classloader (.getClassLoader clojure.main)
    classes-field (.getDeclaredField java.lang.ClassLoader "classes")]
    (.setAccessible classes-field true)
    (let [class-list (.get classes-field classloader)
    class-vec (reduce conj class-list)] ; copy everything into a new vector rather than working directly with the classloader's private field
    class-vec))


    It sounds like you're familiar with Java, so I guess you can see the above is basically just translated Java. It will only give you the classes which have been loaded with the same class loader as that for the class clojure.main, but if you haven't done any customisation with your class loaders, that should be enough.



    Once you have that list, you can search/filter it however you want. Of course, the class you're looking for does have to have been loaded first. If that's not the case, you'll have to search the classpath instead.



    === UPDATE to respond to your comment ===



    OK I see, you're asking how to create a class. The first thing to say is that you don't generally need to create named classes when you're writing Clojure, unless you specifically want to use some existing Java code which requires you to do so. If you're writing pure Clojure, you just write your functions and work with them directly.



    However, you can of course do so. The first part of the doc for gen-class states:




    => (doc gen-class)



    clojure.core/gen-class



    ([& options])



    Macro



    When compiling, generates compiled bytecode for a class with the
    given package-qualified :name (which, as all names in these
    parameters, can be a string or symbol), and writes the .class file
    to the compile-path directory. When not compiling, does
    nothing.




    So, you need to compile your namespace. I don't normally do this so I don't know if there's a way to do it without creating .class files, and just creating the classes directly in memory, but the below does what you want, if I've understood you correctly:



    (ns wip.test)

    ; looks for wip/himplement.clj on the classpath, and compiles it into .class files
    ; requires that ../bin is in the classpath
    (binding [*compile-path* "../bin"]
    (compile 'wip.himplement))

    ; loads the wip.himplement class from the .class files
    (Class/forName "wip.himplement")


    ; create a list of all loaded classes (could presumably also be done with org.reflections)
    (def classes-list (let [classloader (.getClassLoader clojure.main)
    classes-field (.getDeclaredField java.lang.ClassLoader "classes")]
    (.setAccessible classes-field true)
    (java.util.ArrayList. (.get classes-field classloader))))

    ; Outputs all loaded classes which implement HInterface. Output is:
    ; (wip.hello.HInterface wip.himplement)
    (println (filter #(isa? % wip.hello.HInterface) classes-list))





    share|improve this answer














    I'm not familiar with org.reflections, but if you just want a list of loaded classes, you can get it with the below code:



    (let [classloader (.getClassLoader clojure.main)
    classes-field (.getDeclaredField java.lang.ClassLoader "classes")]
    (.setAccessible classes-field true)
    (let [class-list (.get classes-field classloader)
    class-vec (reduce conj class-list)] ; copy everything into a new vector rather than working directly with the classloader's private field
    class-vec))


    It sounds like you're familiar with Java, so I guess you can see the above is basically just translated Java. It will only give you the classes which have been loaded with the same class loader as that for the class clojure.main, but if you haven't done any customisation with your class loaders, that should be enough.



    Once you have that list, you can search/filter it however you want. Of course, the class you're looking for does have to have been loaded first. If that's not the case, you'll have to search the classpath instead.



    === UPDATE to respond to your comment ===



    OK I see, you're asking how to create a class. The first thing to say is that you don't generally need to create named classes when you're writing Clojure, unless you specifically want to use some existing Java code which requires you to do so. If you're writing pure Clojure, you just write your functions and work with them directly.



    However, you can of course do so. The first part of the doc for gen-class states:




    => (doc gen-class)



    clojure.core/gen-class



    ([& options])



    Macro



    When compiling, generates compiled bytecode for a class with the
    given package-qualified :name (which, as all names in these
    parameters, can be a string or symbol), and writes the .class file
    to the compile-path directory. When not compiling, does
    nothing.




    So, you need to compile your namespace. I don't normally do this so I don't know if there's a way to do it without creating .class files, and just creating the classes directly in memory, but the below does what you want, if I've understood you correctly:



    (ns wip.test)

    ; looks for wip/himplement.clj on the classpath, and compiles it into .class files
    ; requires that ../bin is in the classpath
    (binding [*compile-path* "../bin"]
    (compile 'wip.himplement))

    ; loads the wip.himplement class from the .class files
    (Class/forName "wip.himplement")


    ; create a list of all loaded classes (could presumably also be done with org.reflections)
    (def classes-list (let [classloader (.getClassLoader clojure.main)
    classes-field (.getDeclaredField java.lang.ClassLoader "classes")]
    (.setAccessible classes-field true)
    (java.util.ArrayList. (.get classes-field classloader))))

    ; Outputs all loaded classes which implement HInterface. Output is:
    ; (wip.hello.HInterface wip.himplement)
    (println (filter #(isa? % wip.hello.HInterface) classes-list))






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 10 at 20:26

























    answered Nov 8 at 19:25









    OpenSauce

    7,00311628




    7,00311628











    • Thank you.org.reflections gives me the classes that implement an interface, but I could not create a class implementing an interface in Clojure. Here is the corresponding code: github.com/trylks/wip you can just do lein run
      – Trylks
      Nov 9 at 20:32










    • @Trylks see my updated post
      – OpenSauce
      Nov 10 at 20:27
















    • Thank you.org.reflections gives me the classes that implement an interface, but I could not create a class implementing an interface in Clojure. Here is the corresponding code: github.com/trylks/wip you can just do lein run
      – Trylks
      Nov 9 at 20:32










    • @Trylks see my updated post
      – OpenSauce
      Nov 10 at 20:27















    Thank you.org.reflections gives me the classes that implement an interface, but I could not create a class implementing an interface in Clojure. Here is the corresponding code: github.com/trylks/wip you can just do lein run
    – Trylks
    Nov 9 at 20:32




    Thank you.org.reflections gives me the classes that implement an interface, but I could not create a class implementing an interface in Clojure. Here is the corresponding code: github.com/trylks/wip you can just do lein run
    – Trylks
    Nov 9 at 20:32












    @Trylks see my updated post
    – OpenSauce
    Nov 10 at 20:27




    @Trylks see my updated post
    – OpenSauce
    Nov 10 at 20:27












    up vote
    0
    down vote













    I'm not totally sure what you're trying to achieve, but there are a few things that I should probably point you in the direction of. Firstly there's a namespace called clojure.reflect that contains useful functions for getting info about classes. It makes it easier to reflect the Java hierarchy at the REPL. Secondly there's things like clojure.tools.namespace that will help you traverse the code in your repo and find all the namespaces that implement interfaces. However, you tend to use deftype to implement classes rather than using the AOT compiler features like the :gen-class option in the ns macro, unless you're implementing something specific like a servelet or something.






    share|improve this answer
























      up vote
      0
      down vote













      I'm not totally sure what you're trying to achieve, but there are a few things that I should probably point you in the direction of. Firstly there's a namespace called clojure.reflect that contains useful functions for getting info about classes. It makes it easier to reflect the Java hierarchy at the REPL. Secondly there's things like clojure.tools.namespace that will help you traverse the code in your repo and find all the namespaces that implement interfaces. However, you tend to use deftype to implement classes rather than using the AOT compiler features like the :gen-class option in the ns macro, unless you're implementing something specific like a servelet or something.






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        I'm not totally sure what you're trying to achieve, but there are a few things that I should probably point you in the direction of. Firstly there's a namespace called clojure.reflect that contains useful functions for getting info about classes. It makes it easier to reflect the Java hierarchy at the REPL. Secondly there's things like clojure.tools.namespace that will help you traverse the code in your repo and find all the namespaces that implement interfaces. However, you tend to use deftype to implement classes rather than using the AOT compiler features like the :gen-class option in the ns macro, unless you're implementing something specific like a servelet or something.






        share|improve this answer












        I'm not totally sure what you're trying to achieve, but there are a few things that I should probably point you in the direction of. Firstly there's a namespace called clojure.reflect that contains useful functions for getting info about classes. It makes it easier to reflect the Java hierarchy at the REPL. Secondly there's things like clojure.tools.namespace that will help you traverse the code in your repo and find all the namespaces that implement interfaces. However, you tend to use deftype to implement classes rather than using the AOT compiler features like the :gen-class option in the ns macro, unless you're implementing something specific like a servelet or something.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 1:03









        l0st3d

        1,84211728




        1,84211728



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53209619%2fclojure-reflection-find-classes-that-implement-an-interface%23new-answer', 'question_page');

            );

            Post as a guest














































































            Popular posts from this blog

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

            Node.js puppeteer - Use values from array in a loop to cycle through pages