Java可以通过用户输入进行自我修改吗?
我对允许用户输入修改进程和相应源的执行脚本感兴趣。
实施这样的结构有哪些先例?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,要看具体意思。
考虑诸如 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.
您不应该能够修改现有的类。但是,如果您实现 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.
也许 Java 脚本 API 就是您正在寻找的:
http://docs.oracle.com/javase/6/docs/api/javax/script/package-summary.html
http://docs.oracle.com/javase/6 /docs/technotes/guides/scripting/programmer_guide/index.html
Maybe the Java scripting API is what you're looking for:
http://docs.oracle.com/javase/6/docs/api/javax/script/package-summary.html
http://docs.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html
我曾经编写过一个应用程序,它使用反射来允许测试由文本文件驱动。例如,如果您有一个这样的类:
您可以通过如下代码执行方法:
它有更多功能(您可以将对象传递到其他对象等),但大多数情况下我用它来驱动电缆盒上的测试,其中为了进行测试,完整的编写/构建/部署大约需要半个小时。
您可以创建一些可重用的类,并将它们与此测试语言结合在一起,以进行一些非常复杂且易于创建的测试。
这是一个 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:
You could execute methods via code like:
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.