Java Code Generation (Metaprogramming, Reflection, wtv)
Asked Answered
C

5

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.

Chiropractor answered 12/4, 2010 at 16:44 Comment(3)
What kind of code generation, for what kind of programs? Code generation at runtime or not? Please be more specific.Aldebaran
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.Protagoras
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.Chiropractor
P
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

Pereyra answered 12/4, 2010 at 17:0 Comment(1)
but that would be eclipse-dependent, and this must be standalone. that's why i said something like c#'s codedom.Chiropractor
T
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.

Trio answered 12/4, 2010 at 16:59 Comment(3)
that would be nice if i wanted to alter something on runtime, but i actually generate static code for further use.Chiropractor
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.Stuppy
thanks, but Freemarker can do just the thing that i wanted (:Chiropractor
L
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)

Loughlin answered 14/4, 2010 at 17:20 Comment(1)
thanks, but Freemarker can do just the thing that i wanted (:Chiropractor
S
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)

Stuppy answered 13/4, 2010 at 12:36 Comment(1)
thanks, but Freemarker can do just the thing that i wanted (:Chiropractor
C
0

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

Courthouse answered 13/4, 2010 at 22:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.