Java 代码生成(元编程、反射、wtv)

发布于 2024-08-28 23:19:38 字数 158 浏览 4 评论 0原文

有谁知道 Java 工具(类似于 C# 的 codedom)提供了将 Java 代码生成到 .java 文件的方法?

编辑: 我正在构建一个平台,其主要目标是实现操作自动化。提供一些输入,我想为外部工具生成代码。所以它不是在运行时生成的。我想生成并将其输出到实际文件。

Does anyone knows a tool for Java (something like codedom for C#) that provides a way to generate Java code to a .java file?

EDIT:
I'm building a platform the main objective of which is to automate an operation. Giving some input, I want to generate code for an external tool. So it isn't generation on runtime. I want to generate and output that to an actual file.

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

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

发布评论

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

评论(5

把人绕傻吧 2024-09-04 23:19:38

JET 可能已经过时(我没有使用它)JET 教程第 1 部分

更多 Eclipse 插件 代码生成中的插件

编辑:
抱歉,我不知道 codedom 以及这个工具意味着什么功能。

独立版是 Freemarker
Velocity 另请参阅此 示例

JET maybe outdated (I didn't use it) JET Tutorial Part 1

More Plugins for Eclipse Plugins in Code Generation

EDIT:
Sorry I don't know codedom and what features this tool implies.

Standalone is Freemarker
and Velocity see also this example

南烟 2024-09-04 23:19:38

我使用 ASM 在字节码级别修改现有类取得了一些成功或者动态生成全新的类。 教程以一种非常容易理解的方式引导您完成此过程。

ASM 就像大多数此类工具一样,生成字节码而不是源代码。这样做的原因是,如果您想通过程序动态生成并执行新代码,从历史上看,调用 Java 编译器并不直接。因此,生成和使用字节码通常比源代码更容易。

如果您需要在程序中立即生成并运行代码,我建议您使用字节码操作工具。如果您需要的只是 Java 源代码,我会推出自己的代码生成器,它采用我的输入格式并生成代码。您可能需要寻找一个 框架 来帮助您解决此问题,但是由于源文件只是文本,因此通常您自己创建它也很容易,特别是如果您有自定义输入格式。

I have had some success using ASM to both modify existing classes at the bytecode level or to generate completely new classes on the fly. The tutorial walks you through this in a very understandable fashion.

ASM like most such tools generates bytecode not source. The reason for this is if you want to dynamically generate and execute new code from with a program, historically it was not straight forward to invoke the Java compiler. Therefore it was generally easier to generate and use bytecode than source.

If you need to generate and run the code immediately within your program I recommend you use bytecode manipulation tool. If all you need is Java source, I would roll my own code generator that takes my input format and generates the code. You may want to look for a framework to help you with this but since a source file is just text, usually it is just as easy to create this yourself especially if you have a custom input format.

一张白纸 2024-09-04 23:19:38

ABSE 和 AtomWeaver 形成了代码生成和模型驱动开发框架,您可以在其中轻松实现您想要的内容。 ABSE 是一种新方法,您可以用较小的位(称为 Atom)组成代码生成器,而 AtomWaver 是一个简单的 IDE,可让您实现、操作和使用生成器模型。

它还允许非程序员构建由已构建的部件(您之前准备的原子)制成的程序/配置/任何内容。

该项目刚刚公开启动,并且正在提供 alpha 版本。 ABSE 是开放的,AtomWeaver 免费供个人和商业使用。

在此处获取更多信息:http://www.abse.info(免责声明:我是项目负责人)

ABSE and AtomWeaver form a code generation and model-driven-development framework where you can easily implement what you want. ABSE is a new methodology where you compose your code generator from smaller bits (called Atoms) and AtomWaver is an straightforward IDE that lets you implement, manipulate and use your generator models.

It also allows non-programmers to build programs/configurations/whatever, made from already-built parts (Atoms you have previously prepared).

This project is just being publicly launched now, and an alpha version is being made available now. ABSE is open, and AtomWeaver is free for personal and commercial use.

Get more info here : http://www.abse.info (Disclaimer: I am the project lead)

弃爱 2024-09-04 23:19:38

您可以尝试使用现有语法(例如来自 ANTLR)并构建 AST。然后从 AST 生成代码。这应该比简单的模板更加健壮。对于中间的内容,我建议 Terence Parr 关于 StringTemplate 的(令人大开眼界的)演讲。 (抱歉,没有当前演讲的链接)

What you could try is to use an existing grammar (e.g. from ANTLR) and build the AST. Then from the AST generate the code. That should be much more robust than simple templating. For something in the middle I suggest the (eye-opening) talk from Terence Parr about StringTemplate. (Sorry, don't have the link for the talk at hand)

峩卟喜欢 2024-09-04 23:19:38

我不确定您真正需要什么,但请查看 javassist< /a>.这是您正在寻找的东西吗?

I am not sure what you really need, but take a look at javassist. Is it the thing you are looking for?

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