3

I've created JCodeModel that contains all classes I want to generate. The thing is that I want to generate bytecode (.class files) and a jar but not the sources. Is there an elegant way to do it without generating the .java files and later compiling thme into a .class files and a jar?

DiSol
  • 414
  • 2
  • 11
  • JCodeModel is a Java source code generation library, how would it generate bytecode without compiling the source it generates? – John Ericksen Jun 17 '15 at 15:57
  • @JohnEricksen If I knew I wouldn't ask it. By asking how it can elegantly generate the byte code I'm talking about the compiling process. The point is that I want to get an artifact (jar) in the shortes, simplest and elegant way and from my point of view creating hounderds of source code files that I'll later compile into a jar (and at the end delete the sources) is an extra step that I would like to avoid / skip. – DiSol Jun 17 '15 at 16:27
  • Would you like the bytecode generated via an anotation processor? – John Ericksen Jun 17 '15 at 16:46
  • @JohnEricksen I don't sure I understand the question. I'm currently using JCodeModel to generate java files from scratch and than taking the generated sources and compiling them into class files. What I'm trying to achive is to get the class files without first creating the java files. What does it mean " bytecode generated via an anotation processor"? How does annotations related to this? – DiSol Jun 17 '15 at 16:50
  • Annotation processing is a common use case for using JCodeModel to generate source code (and I guess the answer is no). Do you want to generate the class files at runtime or during development then? – John Ericksen Jun 17 '15 at 16:57
  • @JohnEricksen During a build process - I'm creating a tool that should connect to DB and create a class file for each record of some table and pack them into a jar. Another projects that will later be built have dependencies on this jar and are using this classes. – DiSol Jun 17 '15 at 17:02
  • The next question is, what are you using to build your project and (how) can you use that to generate code? – John Ericksen Jun 29 '15 at 15:21

1 Answers1

0

You can consider one of several java byte code generators: Any Java Bytecode Generation Guide?

But if you prefer to deal with JCodeModel lib you have an option to keep intermediate java code in memory as temporary stage. Here is sequence of hints:

(1) Here is an example how to get java source text in memory: Compile dynamically generated class at runtime w/o writing to File

(2) Then you can use similar thing to keep compiled byte code in memory again: https://github.com/trung/InMemoryJavaCompiler/blob/master/src/main/java/org/mdkt/compiler/InMemoryJavaCompiler.java

(3) Finally to create jar-file from byte code stored in memory just do thing similar to: How to use JarOutputStream to create a JAR file?

PS: Last stage could be done as stream writing directly to database (BLOB field or so).

Community
  • 1
  • 1
rsutormin
  • 1,629
  • 2
  • 17
  • 21