Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java source code generation frameworks [closed]

I have a set of Java 5 source files with old-style Doclet tags, comments and annotations. And based on that I would like to write a generator for another set of Java classes.

What is the best way to do that? And are there any good standalone libraries for code analysis/generation in Java? Any shared exprience in this field is appreciated.

So, far I have found these:

  • JaxME's Java Source Reflection - seems good, but it does not seem to support annotations. Also it had no release since 2006.

  • Annogen - uses JDK's Doclet generator, which has some bugs under 1.5 JDK. Also it had no releases for a long time.

  • Javaparser - seems good as well and pretty recent, but only supports Visitor pattern for a single class i.e. no query mechanism like in the 2 above packages.

like image 997
Andrey Adamovich Avatar asked Mar 28 '10 15:03

Andrey Adamovich


People also ask

How to auto generate Java code?

By specifying these attributes, Java state machine code can be generated. You can create a generator model with the YAKINDU Statechart generator model wizard by selecting File → New → Code generator model. The code generation is performed automatically whenever the statechart or the generator file is modified.

What is Java code generation?

The Java with code generation implementation for an action processor creates a Java processor that composes messages to be sent to a device, evaluates the response for errors, extracts output information from the response, and populates the information into output parameters.

What do you mean by code generation?

In computing, code generation is part of the process chain of a compiler and converts intermediate representation of source code into a form (e.g., machine code) that can be readily executed by the target system. Sophisticated compilers typically perform multiple passes over various intermediate forms.

What does a code generator do?

A code generator is a tool or resource that generates a particular sort of code or computer programming language.


2 Answers

If you only need to generate syntactically correct Java code, check the Codemodel.

like image 76
lexicore Avatar answered Oct 01 '22 22:10

lexicore


I ended up using PMD. Code example can be seen below:

    final Java15Parser parser = new Java15Parser();
    final FileInputStream stream = new FileInputStream("VehicleServiceType.java");

    final Object c = parser.parse(new InputStreamReader(stream));

    final XPath xpath = new BaseXPath("//TypeDeclaration/Annotation/NormalAnnotation[Name/@Image = 'WebService']",
        new DocumentNavigator());

    for (final Iterator iter = xpath.selectNodes(c).iterator(); iter.hasNext();) {
      final Object obj = iter.next();
      // Do code generation based on annotations...
    }
like image 22
Andrey Adamovich Avatar answered Oct 01 '22 22:10

Andrey Adamovich