用于生成Java源文件的Java API [英] A Java API to generate Java source files

查看:127
本文介绍了用于生成Java源文件的Java API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个生成Java源文件的框架。



类似于以下API:

  X clazz = Something.createClass(package name,class name); 
clazz.addSuperInterface(interface name);
clazz.addMethod(method name,returnType,argumentTypes,...);

文件targetDir = ...;
clazz.generate(targetDir);

然后,应在目标目录的子目录中找到一个java源文件。 p>

有谁知道这样的框架?






编辑


  1. 我真的需要源文件。

  2. 我也想填写方法的代码。

  3. 我正在寻找一个高级抽象,而不是直接的字节码操作/生成。

  4. 我也需要在对象树中的类的结构。

  5. 问题域是一般的:生成大量非常不同的类,没有公共结构。 li>






解决方案

我已经发布2个答案基于您的答案... 与CodeModel 与Eclipse JDT



我已经使用 CodeModel 在我的解决方案中: - )

解决方案

CodeModel用于使用API​​生成Java源文件。获取信息不是最简单的事情,但它在那里,它的工作非常好。



获取它的最简单的方法是作为JAXB 2 RI的一部分 - XJC模式到java生成器使用CodeModel生成其Java源代码,它是XJC jar的一部分。您可以将其用于CodeModel。



http ://codemodel.java.net/


I'm looking for a framework to generate Java source files.

Something like the following API:

X clazz = Something.createClass("package name", "class name");
clazz.addSuperInterface("interface name");
clazz.addMethod("method name", returnType, argumentTypes, ...);

File targetDir = ...;
clazz.generate(targetDir);

Then, a java source file should be found in a sub-directory of the target directory.

Does anyone know such a framework?


EDIT:

  1. I really need the source files.
  2. I also would like to fill out the code of the methods.
  3. I'm looking for a high-level abstraction, not direct bytecode manipulation/generation.
  4. I also need the "structure of the class" in a tree of objects.
  5. The problem domain is general: to generate a large amount of very different classes, without a "common structure".


SOLUTIONS
I have posted 2 answers based in your answers... with CodeModel and with Eclipse JDT.

I have used CodeModel in my solution, :-)

解决方案

Sun provides an API called CodeModel for generating Java source files using an API. It's not the easiest thing to get information on, but it's there and it works extremely well.

The easiest way to get hold of it is as part of the JAXB 2 RI - the XJC schema-to-java generator uses CodeModel to generate its java source, and it's part of the XJC jars. You can use it just for the CodeModel.

Grab it from http://codemodel.java.net/

这篇关于用于生成Java源文件的Java API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆