0

With reference to the following link, I have created the classes at run time, http://blog.javaforge.net/post/31913732423/howto-create-java-pojo-at-runtime-with-javassist. Now I need to view the created class that means Where will it create the class file? Is it possible to save it on disk/work space?

Vinod
  • 2,263
  • 9
  • 55
  • 104
  • 1
    Just call [`cc.writeFile()`](http://www.csg.ci.i.u-tokyo.ac.jp/~chiba/javassist/html/javassist/CtClass.html#writeFile--) right before or right after you call `cc.toClass()`; sometimes it really helps to read the documentation… – Holger Jan 21 '15 at 13:38
  • Great. It is working. But Is there any way to generate a file with human readable format ? – Vinod Jan 21 '15 at 13:49

3 Answers3

2

The process of taking a java object and writing it to a text-like file is called serialization. The language has good built in support for this.

Oracle's documentation for these features can be found here and a tutorial here.

In general it's pretty easy to use and well understood and provides some clever features including the ability to detect if one version of a program saved the record but an incompatible version is trying to load it.

Also this stack overflow question will be useful to you.

Community
  • 1
  • 1
Elemental
  • 7,365
  • 2
  • 28
  • 33
2

You can call cc.writeFile() right before or right after you call cc.toClass() to store a class file containing the bytecode of the generated class.

I don’t know of an equivalent operation to get a source file, however, you may consider the fact that you are actually generating the source code already (at least for the methods) and passing it to Javassist’s CtClass in order to be compiled.

So it’s not that hard to use the same code to generate the source code for an entire class as you only need to concatenate these methods, add field declarations and enframe it with a class body. After all, generating a source file means just writing a text file in a format that you already know very well…

Holger
  • 285,553
  • 42
  • 434
  • 765
0

Why would you want do do that? It's possible to create files from Pojos, then youll have to follow this tutorial:

http://www.mkyong.com/java/how-to-write-an-object-to-file-in-java/. But then you'll only write the contents of the fields to a file.

Kornelito Benito
  • 1,067
  • 1
  • 17
  • 38
  • There are lots of everyday contexts where writing a record to file is useful. Seems strange to question the OP's reasons. (excellent link btw - mkyong site is really useful) – Elemental Jan 21 '15 at 12:22
  • I am using some Rapid app dev tool in my app, So based on POJO it will generate UI and model. So I wanted to save the generated POJO in the workspace – Vinod Jan 21 '15 at 12:59
  • I was a little confused about what the author meant. Maybe I felt over the word 'class-file'. Should'nt have doubted the question, as Serialization is a very common practice in programming. Hope OP is helped :) – Kornelito Benito Jan 21 '15 at 13:00