views:

230

answers:

10

What are the leading frameworks for java code generation?

I am not looking for a DB or app generation tool. I have a skeleton of a class, and I need to generate it with different dynamic parts for different use cases. The majority of the class is identical, hence I want to run code that generates different flavors of the class.

Anyone know a good framework?

Thanks.

+2  A: 

Note that these utilities are mainly for kickstarting your project by generating boilerplate code.

Bozho
A: 

SQL2Java generates Database CRUD code from the DB schema.

Michael Borgwardt
+2  A: 

cglib is a powerful, high performance and quality Code Generation Library

jet/velocity are using templates.

If you're looking to generate a whole application check out JBoss Seam framework.

stacker
cglib is used by JUnit/EasyMock, so is maybe already included in users project
Markus Kull
A: 

[SQL2JAVA] is a great tool. It generates All [CRUD] code for the Database Schema. It's connection pooling is not good enough,you can customize it's Manager class to maintain your own pool.

Other than this if you are interested with Model Driven Development (MDD),you can use [AndroMDA] or [Borland's Together] , one of the best [eclipse] based tool out there.

If your are interested in going a bit further with code generation and getting into model driven software development you should have a look at [openArchitectureWare].

Other java development tools that support code generation are [Lombok] and [Spoon]. Project Lombok offers the features like auto-generation of the default getter/setter methods, automatic resource management (using @Cleanup annotation) and annotation driven exception handling.

[cglib],[Velocity template] and [AppFuse] are also great.

nil
A: 

I presume that ASM is the most popular Java bytecode generation library. It's the most low-level bytecode library there is, but there are other higher-level code generation libraries which are built on ASM, although using ASM directly isn't too hard either (one benefit of direct use is that ASM's JAR size is very small). Some of ASM's users are mentioned at http://asm.ow2.org/users.html


EDIT:

For the updated question, which mentions the use case ("I have a skeleton of a class, and I need to generate it with different dynamic parts for different use cases."), my answer is to improve the design so that all code duplication is removed and no code generation is needed. Reading about different design patterns can give some ideas on how to do it. For a more specific answer, you will need to show some code and be more specific about your needs.

Esko Luontola
But from an ease of programming perspective, it's a PITA and generating source is in some sense easier.
Tom Hawtin - tackline
Depends on what you need to do. Bytecode generation can be done also at runtime and it can be used to modify existing classes, so it is more powerful that generating source files.
Esko Luontola
The question was about code-generation; not compilation. ASM is far too low level for what was asked.
Recurse
Also bytecode is code. And generating bytecode is not compilation, because compiling means transforming a high-level language to into another language (http://dictionary.reference.com/browse/compiling) - it's not compiling when there is no source language which is being transformed.
Esko Luontola
A: 

I used the Druid Database Manager: http://druid.sourceforge.net/

It starts from db, which can be reverse engineered, and it can generate documentation, sql, classes and files based on tables and fields.

The template language used is velocity.

The software is extensible through a plugin system.

Impiastro
A: 

AtomWeaver http://www.atomweaver.com is now in public beta. It's a code generation IDE that can be used alongside your current IDE (so, it's no framework, but a standalone app). With it you can generate boilerplate code, but also a complete project.

AtomWeaver implements ABSE (Atom-Based Software Engineering), which is a form of model-driven software development (has nothing to do with UML or MDA).

It's essentially a template-based system.

Rui Curado
A: 

http://www.andromda.org

AndroMDA (pronounced: andromeda) is an open source code generation framework that follows the Model Driven Architecture (MDA) paradigm. It takes model(s) from CASE-tool(s) and generates fully deployable applications and other components.

I have used it and it is very powerfull. It not only generates configuration files and code from UML, leaving to the developer only the implementation of the business methods to develop, but also maintains the generate code through the use of well-known dessing patterns.

Pau
A: 

Try Xtext (http://www.eclipse.org/Xtext/). You use XText to define a DSL, XText will gernerate a Eclipse Editor supporting this DSL. Then you can use XPand to define templates to generating all kind of text (for example java souce code).

Ralph
A: 

Another vote for the Velocity template engine. I've used it in multiple projects, for generating EJB2.1 boilerplate code, database objects, etc. Works great and is pretty easy to learn as well.

Salil