java 从java程序动态生成java类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7216500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamically generating java class from a java program
提问by user915745
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.
在我的项目的构建(编译)时,我需要为 java 类进行代码生成。生成的java类是一个带有一组getter和setter的java bean类。在构建时,我得到了类的名称和变量的名称。所以我需要做的是根据我拥有的信息动态生成 java bean。
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.
当我搜索可以使用的工具时,我发现 Arch4j [1] 很有趣,但问题是它与 Apache 2.0 许可证不兼容。我正在寻找与 Apache 2.0 许可证兼容的项目/工具。
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
[1] - http://arch4j.sourceforge.net/components/generator/index.html
回答by JB Nizet
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.
为什么不在构建期间使用自定义 ant 任务或 Maven 插件生成 .java 文件?这似乎是一项相当简单的任务,不需要任何复杂的库。您甚至可以使用带有类名和字段名占位符的模板文件,并使用replace
任务生成真正的 .java 文件。
回答by Ant Kutschera
JET from Eclipse can be used:
可以使用来自 Eclipse 的 JET:
http://eclipse.org/articles/Article-JET/jet_tutorial1.html
http://eclipse.org/articles/Article-JET/jet_tutorial1.html
It can be called by ant: http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.jet.doc/references/ant/antTasks.xhtml
它可以被蚂蚁调用:http: //help.eclipse.org/helios/index.jsp?topic=/org.eclipse.jet.doc/ references/ant/ antTasks.xhtml
And I think from maven: http://mvnrepository.com/artifact/org.eclipse/jet/0.8.0-v20070605
我认为来自 maven:http: //mvnrepository.com/artifact/org.eclipse/jet/0.8.0-v20070605
回答by OscarRyz
Take a look at javax.tools package. You can create and load a dynamically generated class only with that package.
看看 javax.tools 包。您只能使用该包创建和加载动态生成的类。
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 )
请记住,您需要 JDK 可用且不可重新分发,因此您的客户需要单独下载它(就像您今天在任何 IDE 中所做的一样)
http://download.oracle.com/javase/6/docs/api/javax/tools/package-summary.html
http://download.oracle.com/javase/6/docs/api/javax/tools/package-summary.html
For instance, you can invoke the java compiler programatically with:
例如,您可以通过以下方式以编程方式调用 java 编译器:
http://download.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html
http://download.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html
And you can load it with URLClassLoader
你可以加载它 URLClassLoader
回答by Ali
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.
奇怪的建议,但您似乎想生成 bean。为什么不使用像 apache common 的 DynaBean 这样的东西?它们允许您在运行时创建 bean。这是使用 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 velocityas the java class template's engine.
当然,这是在运行时而不是编译时。对于编译时间,我建议使用 ant 任务来编译您的源代码,并在生成类时添加用于编译的依赖项。您可以通过编写一个使用速度作为 Java 类模板引擎的小型 Java 应用程序来处理类生成。
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.
因此,您在编译时的 ant 任务首先调用一个小型 java 程序,该程序使用速度模板生成 java 类文件(如果需要,请删除 ant 中的旧文件)。然后正常编译。