Java code generation [closed]
Asked Answered
L

10

30

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.

Lenka answered 2/8, 2010 at 6:28 Comment(6)
c2.com/cgi/wiki?CodeGenerationIsaDesignSmellTaynatayra
Are you trying to do something specific, or is this just a way of harvesting another list of frameworks?Fagen
@Brent The link you provide seems to be against code generation (due to its title) but is in fact a kind of discussion, where the number of arguments against it is roughly the same as the ones in favorDwaindwaine
IMO, the biggest problem with APT is the lack of a good way of generating code.Fraenum
I am looking for a template based generation tool.Lenka
If the code you want to generate can be based on an existing class, JavaForger might be of help. It parses existing classes and fills templates with the parsed data. github.com/daanvdh/JavaForgerNoisome
O
14

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.

Orontes answered 2/8, 2010 at 6:56 Comment(1)
cglib is used by JUnit/EasyMock, so is maybe already included in users projectBinucleate
P
9

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

Palembang answered 2/8, 2010 at 6:50 Comment(0)
C
6

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.

Champagne answered 2/8, 2010 at 16:46 Comment(0)
S
3

[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.

Sectionalism answered 2/8, 2010 at 7:2 Comment(1)
[Why] do you put [brackets] around [things]? :)Germanophile
A
3

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).

Adultery answered 3/8, 2010 at 12:53 Comment(0)
N
2

SQL2Java generates Database CRUD code from the DB schema.

Nucleonics answered 2/8, 2010 at 6:54 Comment(0)
A
2

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.

Along answered 2/8, 2010 at 8:4 Comment(0)
I
0

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.

Inactive answered 2/8, 2010 at 7:37 Comment(4)
But from an ease of programming perspective, it's a PITA and generating source is in some sense easier.Fraenum
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.Inactive
The question was about code-generation; not compilation. ASM is far too low level for what was asked.Sequin
Also bytecode is code. And generating bytecode is not compilation, because compiling means transforming a high-level language to into another language (dictionary.reference.com/browse/compiling) - it's not compiling when there is no source language which is being transformed.Inactive
D
0

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.

Dwaindwaine answered 2/8, 2010 at 8:56 Comment(0)
S
0

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.

Siouan answered 4/8, 2010 at 6:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.