1

Is there a way of using reflection to create a class programmatically? For instance. Is there a private constructor we can use to create a class type that then can be used to create instances of that class?

I know that there are other tricks like generating source code and compiling it, or generating byte code and loading it using a classloader... But I want to see if it is possible to somehow create an instance of java.lang.Class directly.

I want to write the following code in clojure:

(def c (create-class "com.example.Dog" {:fields {"legs" 4}
                                        :methods {"bark" (... do something ...)}
                                        :constructors {.....}}))

(def d1 (.newInstance c))
(def d2 (.newInstance c))
zcaudate
  • 13,998
  • 7
  • 64
  • 124
  • 2
    It's really not clear what you're asking here. Could you give more details? – Jon Skeet Jan 28 '14 at 19:16
  • 1
    And where is the Clojure part in your question? – Chiron Jan 28 '14 at 19:17
  • To clarify, you're asking for a way to define a new `class` (not create a new object of an existing class) using reflection? – Gus Jan 28 '14 at 19:19
  • 1
    If you are talking about Clojure, `defrecord` and `deftype` emit bytecode for a new named class. – A. Webb Jan 28 '14 at 19:22
  • @gus. Yes, that is what I want to do. I know that clojure emits bytecode to create a class but I was hoping to be able to use reflection instead. I tagged the question with clojure because i am more partial to clojure and want to grab the attention of clojure users. – zcaudate Jan 28 '14 at 20:14
  • possible duplicate of [Dynamically class creating by using Java Reflection, java.lang.ClassNotFoundException](http://stackoverflow.com/questions/2566754/dynamically-class-creating-by-using-java-reflection-java-lang-classnotfoundexce) – claj Jan 28 '14 at 20:26
  • @claj... That is not even close – zcaudate Jan 28 '14 at 20:55
  • I've clarified the question with a usage example. – zcaudate Jan 29 '14 at 04:47

1 Answers1

4

Clojure has several ways to generate classes depending on your needs: They are described in the Clojure types page Though I found the chapter on types in Clojure Programming most helpful as a comparison of the various ways of generating classes dynamically.

  • defrecord
  • deftype
  • proxy
  • gen-class
  • protocols
  • reify

Under the hood these generate bytecode and load it using a classloader (except for gen-class) which writes class files. There is a lot more to the subject than I can cover here, it's well worth checking out the chapter in Clojure Programming (or your favorite Clojure Book)

sw1nn
  • 7,278
  • 1
  • 26
  • 36
Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
  • Is there a way to do it without going through the classloader? Ie. a hack? – zcaudate Jan 28 '14 at 21:57
  • the only way to get classes into a jvm is via a classloader. – sw1nn Jan 28 '14 at 22:34
  • The Classloader is used everytime you create a function, perhaps the confusion here is related to the fact that the clojure runtime uses classloaders internally? You don't need to know anything about them to use clojure to generate named java classes etc. – Arthur Ulfeldt Jan 28 '14 at 22:47