Java可以通过用户输入进行自我修改吗?

发布于 2024-12-19 08:16:49 字数 54 浏览 3 评论 0原文

我对允许用户输入修改进程和相应源的执行脚本感兴趣。

实施这样的结构有哪些先例?

I'm interested in an executed script allowing user input to modify the process and corresponding source.

What precedents exist to implement such a structure?

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

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

发布评论

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

评论(4

怀中猫帐中妖 2024-12-26 08:16:49

是的,要看具体意思。

考虑诸如 ObjectWeb ASM 之类的项目(请参阅 ASM 2.0 教程,了解一般概要)。

尝试发出需要反编译的 Java 源代码是另一回事:如果这是目标,那么也许应该编辑、重新编译源代码,并以某种方式加载/覆盖。 (这也是可能的,请考虑像 JRebel 这样的工具。)

快乐编码。

Yes, depending on what is meant.

Consider such projects as ObjectWeb ASM (see the the ASM 2.0 tutorial for a general rundown).

Trying to emit the-would-need-to-be-decompiled Java source code is another story: if this was the goal then perhaps the source should be edited, re-compiled, and somehow loaded in/over. (This is possible as well, consider tools like JRebel.)

Happy coding.

伴随着你 2024-12-26 08:16:49

您不应该能够修改现有的类。但是,如果您实现 ClassLoader,那么您可以从非传统来源动态加载类:网络、XML 文件、用户输入、随机数生成器等。

可能还有其他更好的方法。

You should not be able to modify existing classes. But if you implement a ClassLoader then you can dynamically load classes from non-traditional sources: network, XML file, user input, random number generator, etc.

There are probably other, better ways.

っ〆星空下的拥抱 2024-12-26 08:16:49

我曾经编写过一个应用程序,它使用反射来允许测试由文本文件驱动。例如,如果您有一个这样的类:

class Tuner(String Channel) {

    tune(){...
    play(){...
    stop(){...
}

您可以通过如下代码执行方法:

tuner=Channel 1
tune tuner
play tuner
stop tuner

它有更多功能(您可以将对象传递到其他对象等),但大多数情况下我用它来驱动电缆盒上的测试,其中为了进行测试,完整的编写/构建/部署大约需要半个小时。

您可以创建一些可重用的类,并将它们与此测试语言结合在一起,以进行一些非常复杂且易于创建的测试。

这是一个 DSL,而不是通过消除括号并在随机位置添加下划线和点来摆弄松散语法语言,使其看起来像一些奇怪的半英语。

I wrote an app once that used reflection to allow tests to be driven by a text file. For instance, if you had a class like this:

class Tuner(String Channel) {

    tune(){...
    play(){...
    stop(){...
}

You could execute methods via code like:

tuner=Channel 1
tune tuner
play tuner
stop tuner

It had some more capabilities (You could pass objects into other objects, etc), but mostly I used it to drive tests on a cable box where a full write/build/deploy in order to test took on the order of a half hour.

You could create a few reusable classes and tie them together with this test language to make some very complex and easy to create tests.

THAT is a DSL, not monkeying around with your loose-syntax language by eliminating parenthesis and adding underscores and dots in random locations to make it look like some strange semi-English.

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