views:

373

answers:

5

Does anyone knows a tool for Java (something like codedom for C#) that provides a way to generate Java code to a .java file?

EDIT: I'm building a plataform that its main objective his to automate an operation. Giving some input, i want to generate code for an external tool. So it isn't generation on runtime. I want to generate and output that to an actual file.

A: 

I have had some success using ASM to both modify existing classes at the bytecode level or to generate completely new classes on the fly. The tutorial walks you through this in a very understandable fashion.

ASM like most such tools generates bytecode not source. The reason for this is if you want to dynamically generate and execute new code from with a program, historically it was not straight forward to invoke the Java compiler. Therefore it was generally easier to generate and use bytecode than source.

If you need to generate and run the code immediately within your program I recommend you use bytecode manipulation tool. If all you need is Java source, I would roll my own code generator that takes my input format and generates the code. You may want to look for a framework to help you with this but since a source file is just text, usually it is just as easy to create this yourself especially if you have a custom input format.

Tendayi Mawushe
that would be nice if i wanted to alter something on runtime, but i actually generate static code for further use.
the qwerty
ASM is not just for byte code modifications. If you don't need to source code it would be perfect. Especially with the great Eclipse Bytecode Outline.
tcurdt
thanks, but Freemarker can do just the thing that i wanted (:
the qwerty
+1  A: 

JET maybe outdated (I didn't use it) JET Tutorial Part 1

More Plugins for Eclipse Plugins in Code Generation

EDIT: Sorry I don't know codedom and what features this tool implies.

Standalone is Freemarker and Velocity see also this example

stacker
but that would be eclipse-dependent, and this must be standalone. that's why i said something like c#'s codedom.
the qwerty
A: 

What you could try is to use an existing grammar (e.g. from ANTLR) and build the AST. Then from the AST generate the code. That should be much more robust than simple templating. For something in the middle I suggest the (eye-opening) talk from Terence Parr about StringTemplate. (Sorry, don't have the link for the talk at hand)

tcurdt
thanks, but Freemarker can do just the thing that i wanted (:
the qwerty
A: 

Hi, I am not sure what you really need, but take a look at javassist. Is it the thing you are looking for?

Denis
+1  A: 

ABSE and AtomWeaver form a code generation and model-driven-development framework where you can easily implement what you want. ABSE is a new methodology where you compose your code generator from smaller bits (called Atoms) and AtomWaver is an straightforward IDE that lets you implement, manipulate and use your generator models.

It also allows non-programmers to build programs/configurations/whatever, made from already-built parts (Atoms you have previously prepared).

This project is just being publicly launched now, and an alpha version is being made available now. ABSE is open, and AtomWeaver is free for personal and commercial use.

Get more info here : http://www.abse.info (Disclaimer: I am the project lead)

Rui Curado
thanks, but Freemarker can do just the thing that i wanted (:
the qwerty