Java source code generation frameworks [closed]

六月ゝ 毕业季﹏ 提交于 2019-12-03 04:11:15

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.

lexicore

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!