用于生成 Java 源文件的 Java API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/121324/
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
A Java API to generate Java source files
提问by Daniel Fanjul
I'm looking for a framework to generate Java source files.
我正在寻找一个框架来生成 Java 源文件。
Something like the following API:
类似于以下 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.
然后,应该在目标目录的子目录中找到一个java源文件。
Does anyone know such a framework?
有谁知道这样的框架?
EDIT:
编辑:
- I really need the source files.
- I also would like to fill out the code of the methods.
- I'm looking for a high-level abstraction, not direct bytecode manipulation/generation.
- I also need the "structure of the class" in a tree of objects.
- 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 CodeModeland with Eclipse JDT.
解决方案
我根据您的答案发布了 2 个答案......使用 CodeModel和Eclipse JDT。
I have used CodeModelin my solution, :-)
我在我的解决方案中使用了CodeModel,:-)
采纳答案by skaffman
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.
Sun 提供了一个名为 CodeModel 的 API,用于使用 API 生成 Java 源文件。获取信息并不是最容易的事情,但它就在那里,而且效果非常好。
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.
获取它的最简单方法是作为 JAXB 2 RI 的一部分——XJC 模式到 Java 生成器使用 CodeModel 生成其 Java 源代码,它是 XJC jar 的一部分。您可以仅将其用于 CodeModel。
Grab it from http://codemodel.java.net/
回答by Steve g
If you REALLY need the source, I don't know of anything that generates source. You can however use ASMor CGLIBto directly create the .class files.
如果你真的需要源代码,我不知道任何生成源代码的东西。但是,您可以使用ASM或CGLIB直接创建 .class 文件。
You might be able to generate source from these, but I've only used them to generate bytecode.
您可能能够从这些生成源代码,但我只使用它们来生成字节码。
回答by Mike Deck
The Eclipse JETproject can be used to do source generation. I don't think it's API is exactly like the one you described, but every time I've heard of a project doing Java source generation they've used JET or a homegrown tool.
在Eclipse的JET项目可以用来做源代码的生成。我不认为它的 API 与您描述的完全一样,但是每次我听说一个项目进行 Java 源代码生成时,他们都会使用 JET 或自制工具。
回答by Vladimir Dyuzhev
I was doing it myself for a mock generator tool. It's a very simple task, even if you need to follow Sun formatting guidelines. I bet you'd finish the code that does it faster then you found something that fits your goal on the Internet.
我自己做的是一个模拟生成器工具。这是一项非常简单的任务,即使您需要遵循 Sun 格式指南。我敢打赌,当您在 Internet 上找到符合您目标的代码时,您会更快地完成代码。
You've basically outlined the API yourself. Just fill it with the actual code now!
您基本上已经自己概述了 API。现在只需用实际代码填充它!
回答by Squirrel
Another alternative is Eclipse JDT's AST which is good if you need to rewrite arbitrary Java source code rather than just generate source code. (and I believe it can be used independently from eclipse).
另一种替代方法是 Eclipse JDT 的 AST,如果您需要重写任意 Java 源代码而不仅仅是生成源代码,它会很好。(我相信它可以独立于 eclipse 使用)。
回答by ykaganovich
Don't know of a library, but a generic template engine might be all you need. There are a bunch of them, I personally have had good experience with FreeMarker
不知道有什么库,但您可能只需要一个通用模板引擎。有一群人,我个人有过很好的经验的FreeMarker
回答by Daniel Fanjul
Solution found with CodeModel
Thanks, skaffman.
使用 CodeModel 找到了解决方案
谢谢,skaffman。
For example, with this code:
例如,使用此代码:
JCodeModel cm = new JCodeModel();
JDefinedClass dc = cm._class("foo.Bar");
JMethod m = dc.method(0, int.class, "foo");
m.body()._return(JExpr.lit(5));
File file = new File("./target/classes");
file.mkdirs();
cm.build(file);
I can get this output:
我可以得到这个输出:
package foo;
public class Bar {
int foo() {
return 5;
}
}
回答by Daniel Fanjul
Solution found with Eclipse JDT's AST
Thanks, Giles.
使用 Eclipse JDT 的 AST 找到的解决方案
谢谢,Giles。
For example, with this code:
例如,使用此代码:
AST ast = AST.newAST(AST.JLS3);
CompilationUnit cu = ast.newCompilationUnit();
PackageDeclaration p1 = ast.newPackageDeclaration();
p1.setName(ast.newSimpleName("foo"));
cu.setPackage(p1);
ImportDeclaration id = ast.newImportDeclaration();
id.setName(ast.newName(new String[] { "java", "util", "Set" }));
cu.imports().add(id);
TypeDeclaration td = ast.newTypeDeclaration();
td.setName(ast.newSimpleName("Foo"));
TypeParameter tp = ast.newTypeParameter();
tp.setName(ast.newSimpleName("X"));
td.typeParameters().add(tp);
cu.types().add(td);
MethodDeclaration md = ast.newMethodDeclaration();
td.bodyDeclarations().add(md);
Block block = ast.newBlock();
md.setBody(block);
MethodInvocation mi = ast.newMethodInvocation();
mi.setName(ast.newSimpleName("x"));
ExpressionStatement e = ast.newExpressionStatement(mi);
block.statements().add(e);
System.out.println(cu);
I can get this output:
我可以得到这个输出:
package foo;
import java.util.Set;
class Foo<X> {
void MISSING(){
x();
}
}
回答by Bala
There is also StringTemplate. It is by the author of ANTLR and is quite powerful.
还有StringTemplate。它是由 ANTLR 的作者编写的,功能非常强大。
回答by Berlin Brown
It really depends on what you are trying to do. Code generation is a topic within itself. Without a specific use-case, I suggest looking at velocity code generation/template library. Also, if you are doing the code generation offline, I would suggest using something like ArgoUML to go from UML diagram/Object model to Java code.
这真的取决于你想要做什么。代码生成本身就是一个主题。如果没有特定的用例,我建议查看速度代码生成/模板库。此外,如果您要离线生成代码,我建议您使用 ArgoUML 之类的东西从 UML 图/对象模型到 Java 代码。