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:
- Define an interface in Java
- Implement it in a class in Clojure (Bonus: with a macro)
- 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
add a comment |
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:
- Define an interface in Java
- Implement it in a class in Clojure (Bonus: with a macro)
- 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
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 ofinstance?- 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
add a comment |
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:
- Define an interface in Java
- Implement it in a class in Clojure (Bonus: with a macro)
- 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
This seems to be a more difficult in Clojure than in Java and Scala. What I want to do is:
- Define an interface in Java
- Implement it in a class in Clojure (Bonus: with a macro)
- 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
java reflection interface clojure classpath
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 ofinstance?- 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
add a comment |
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 ofinstance?- 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
add a comment |
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))
Thank you.org.reflectionsgives 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 dolein run
– Trylks
Nov 9 at 20:32
@Trylks see my updated post
– OpenSauce
Nov 10 at 20:27
add a comment |
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.
add a comment |
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))
Thank you.org.reflectionsgives 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 dolein run
– Trylks
Nov 9 at 20:32
@Trylks see my updated post
– OpenSauce
Nov 10 at 20:27
add a comment |
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))
Thank you.org.reflectionsgives 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 dolein run
– Trylks
Nov 9 at 20:32
@Trylks see my updated post
– OpenSauce
Nov 10 at 20:27
add a comment |
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))
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))
edited Nov 10 at 20:26
answered Nov 8 at 19:25
OpenSauce
7,00311628
7,00311628
Thank you.org.reflectionsgives 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 dolein run
– Trylks
Nov 9 at 20:32
@Trylks see my updated post
– OpenSauce
Nov 10 at 20:27
add a comment |
Thank you.org.reflectionsgives 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 dolein 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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 10 at 1:03
l0st3d
1,84211728
1,84211728
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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