从java程序动态生成java类

发布于 2024-12-01 20:51:32 字数 736 浏览 2 评论 0原文

在我的项目的构建(编译)时,我需要为 java 类进行代码生成。生成的 java 类是一个带有一组 getter 和 setter 的 java bean 类。在构建时,我获取类的名称和变量的名称。所以我需要做的是根据我所拥有的信息动态生成java bean。

例如。在编译时我得到以下数据。

class-name=Test
variable-name=aaa

所以生成类应该如下所示。

public class Test {
   public String aaa;
   public void setVar(String str) {
      this.aaa = str; 
   }
   public String getVar(){
      return this.aaa;
   }
}

当我搜索可以使用的工具时,我发现 Arch4j [1] 很有趣,但问题是它与 Apache 2.0 许可证不兼容。我正在寻找与 Apache 2.0 许可证兼容的项目/工具。

如果有人能给我一些关于如何做到这一点的见解,我将不胜感激。

[1] - http://arch4j.sourceforge.net/components/generator/index.html

At the build(compile) time of my project I need to do code-generation for a java class. The generated java class is a java bean class with a set of getters and setters. At the build time I am getting the name of the class and names of variables. So what I need to do is dynamically generate the java bean from the information which I have.

eg. At the compile time I am getting following data.

class-name=Test
variable-name=aaa

So the generate class should look like following.

public class Test {
   public String aaa;
   public void setVar(String str) {
      this.aaa = str; 
   }
   public String getVar(){
      return this.aaa;
   }
}

When I searched for a tool that I can use, I found Arch4j [1] interesting but the problem with that is it is not compatible with Apache 2.0 license. I am looking for a project/tool that is compatible with Apache 2.0 license.

I would appreciate if someone can give me some insight on how I can do this.

[1] - http://arch4j.sourceforge.net/components/generator/index.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦年海沫深 2024-12-08 20:51:32

为什么不在构建过程中使用自定义 ant 任务或 Maven 插件生成 .java 文件?这似乎是一个相当简单的任务,不需要任何复杂的库。您甚至可以使用带有类名和字段名占位符的模板文件,并使用 replace 任务生成真正的 .java 文件。

Why not just generate the .java file during your build, using a custom ant task or Maven plugin? This seems like a rather easy task which doesn't need any complex library. You could even use a template file with placeholders for the class name and the field name, and generate the real .java file using a replace task.

窗影残 2024-12-08 20:51:32

看一下 javax.tools 包。您只能使用该包创建和加载动态生成的类。

请记住,您需要可用的 JDK,并且该 JDK 不可重新分发,因此您的客户需要单独下载它(就像您今天在任何 IDE 中所做的那样)

http://download.oracle.com/javase/6/docs/api/javax/tools/package-summary.html

例如,您可以通过以下方式以编程方式调用 java 编译器:

< a href="http://download.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html" rel="nofollow">http://download.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html

并且您可以使用 URLClassLoader 加载它

Take a look at javax.tools package. You can create and load a dynamically generated class only with that package.

Just bear in mind you need the JDK available and that's not re-distributable so your customer would need to downloaded it separately ( just like you do in any IDE today )

http://download.oracle.com/javase/6/docs/api/javax/tools/package-summary.html

For instance, you can invoke the java compiler programatically with:

http://download.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html

And you can load it with URLClassLoader

z祗昰~ 2024-12-08 20:51:32

奇怪的建议,但似乎你想生成豆子。为什么不使用 apache common 的 DynaBean 之类的东西?它们允许您在运行时创建 bean。以下是使用 DynaBean 的示例

当然这是在运行时而不是编译时。对于编译时,我建议使用 ant 任务来编译源代码并添加编译依赖项以生成类。您可以通过编写一个小型 java 应用程序来处理类生成,该应用程序使用 velocity 作为 java 类模板的引擎。

因此,编译时的 ant 任务首先调用一个小型 java 程序,该程序使用速度模板生成 java 类文件(如果需要,请删除 ant 中的旧文件)。然后正常编译。

Odd suggestion, but it seems like you want to generate beans. Why not use something like apache common's DynaBean? They allow you to create beans at run time. Here is an example of using DynaBean.

Of course this is at run time and not compile time. For compile time, I would recommend using an ant task to compile your source and add a dependency for compile on generation of your classes. You can handle the classes generation by writing a small java application that uses velocity as the java class template's engine.

So your ant task on compile first calls a small java program that generates the java class files using velocity template (delete the old files in ant if needed). Then compile as normal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文