Java code generation (metaprogramming, reflection, wtv)

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

EDIT: I am building a plataform that its main goal is to automate the operation. By providing some input, I want to generate code for an external tool. Therefore, this is not a generation at runtime. I want to generate and output this to the actual file.

+6
java reflection code-generation metaprogramming codedom
source share
5 answers

JET may be deprecated (I did not use it) JET Tutorial Part 1

Additional Plugins for Eclipse Plugins in Code Generation

EDIT: Sorry, I don’t know what code is and what functions this tool implies.

Standalone Freemarker and Velocity see also this example.

+1
source share

ABSE and AtomWeaver form a model-based code generation and modeling framework where you can easily implement what you want. ABSE is a new methodology in which you create a code generator from smaller bits (called Atoms), and AtomWaver is a simple IDE that allows you to implement, manage and use your generator models.

It also allows non-programmers to create programs / configurations / regardless of those made up of parts already created (Atoms that you previously prepared).

This project is only now publicly launched, and the alpha version is now available. ABSE is open, and AtomWeaver is free for personal and commercial use.

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

+1
source share

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

ASM , like most of these tools, generates bytecode, not the source. The reason for this is because you want to dynamically generate and execute new code using a program, historically there has not been a direct call to the Java compiler. Therefore, it was easier to generate and use bytecode than the original one.

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

0
source share

What you could try is to use an existing grammar (e.g. from ANTLR) and build an AST. Then, code is generated from the AST. This should be much more reliable than a simple template. For something in the middle, I suggest (open your eyes) Terence Parr's talk about StringTemplate . (Sorry, you do not have a link to the conversation)

0
source share

I'm not sure what you really need, but look at the javassist . Is this what you are looking for?

0
source share

All Articles