开发者

Java source code generation frameworks [closed]

开发者 https://www.devze.com 2022-12-24 17:53 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.

Closed 8 years ago.

Improve this question

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.

  • 开发者_如何学Python
  • 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.


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


Both the NetBeans IDE and Eclipse JDT projects have considerable Java code analysis/generation logic. I don't know what their dependencies are (i.e., can you use them as standalone libs), but other than that, I would take a good look at those two: it's unlikely there's a java code analysis library under more intensive development and more up to date.

Update:

PMD might be of interest as well:

PMD scans Java source code and looks for potential problems like:

* Possible bugs - empty try/catch/finally/switch statements
* Dead code - unused local variables, parameters and private methods
* Suboptimal code - wasteful String/StringBuffer usage
* Overcomplicated expressions - unnecessary if statements, for loops that could be while loops
* Duplicate code - copied/pasted code means copied/pasted bugs

Additionally, this blog entry discusses various static code analysis tools.


it's not exactly a code generator, but if you need some bean-related functionality, apache beanutils is a time-saver

http://commons.apache.org/proper/commons-beanutils/javadocs/v1.9.1/apidocs/org/apache/commons/beanutils/package-summary.html#dynamic

0

精彩评论

暂无评论...
验证码 换一张
取 消