7

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.

3
  • 1
    What kind of code generation, for what kind of programs? Code generation at runtime or not? Please be more specific. Commented Apr 12, 2010 at 16:52
  • What's the end goal here? Java has many tools for creating or modifying byte code (compiled Java code), but I don't know of any for generating Java code directly. Commented Apr 12, 2010 at 16:57
  • 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. Commented Apr 12, 2010 at 17:09

5 Answers 5

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

but that would be eclipse-dependent, and this must be standalone. that's why i said something like c#'s codedom.
1

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.

3 Comments

that would be nice if i wanted to alter something on runtime, but i actually generate static code for further use.
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.
thanks, but Freemarker can do just the thing that i wanted (:
1

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)

1 Comment

thanks, but Freemarker can do just the thing that i wanted (:
0

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)

1 Comment

thanks, but Freemarker can do just the thing that i wanted (:
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.