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.
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.
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.
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.
A code generator is a tool or resource that generates a particular sort of code or computer programming language.
If you only need to generate syntactically correct Java code, check the Codemodel.
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...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With