Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Code Generation (Metaprogramming, Reflection, wtv)

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 platform the main objective of which is 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.

like image 787
the qwerty Avatar asked Apr 12 '10 16:04

the qwerty


3 Answers

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)

like image 34
Rui Curado Avatar answered Oct 07 '22 10:10

Rui Curado


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

like image 91
stacker Avatar answered Oct 07 '22 11:10

stacker


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.

like image 34
Tendayi Mawushe Avatar answered Oct 07 '22 11:10

Tendayi Mawushe