在运行时编写和实现新的Java类文件 [英] Writing and implementing new Java class files during run-time

查看:123
本文介绍了在运行时编写和实现新的Java类文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以以某种方式将新的Java类导入正在运行的程序并使用它?

Is it possible to somehow 'import' a new Java class into a running program and make use of it?

我可以让程序创建一个新的文件类型.java,然后将其包含在项目文件中并引用它而不必重新启动程序?

Could I have a program create a new File of type '.java' and then include it in the project files and reference it without having to restart the program?

以下是我的意思示例:

import java.io.*;

public class Program {

File JClass = new File("JClass.java");
public static BufferedWriter out = null;

    public static void main(String[] args) {
        try {
            out = new BufferedWriter(new FileWriter("JClass.java"));
            out.write("public abstract class JClass {");
            out.newLine();
            out.newLine();
            out.write("    public void printSomething(String a) {");
            out.newLine();
            out.write("        System.out.println(a);");
            out.newLine();
            out.write("    }");
            out.newLine();
            out.write("}");
            out.close();
        } catch (IOException e)
        {
            System.exit(-1);
        }

        //Somehow import JClass.java as a class here

        JClass.printSomething("Yay! It worked!");
    }

}

生成'JClass.java'文件:

Resulting 'JClass.java' file:

public abstract class JClass {

    public void printSomething(String a) {
        System.out.println(a);
    }
}

同样,一个项目的源文件,编辑文件中的代码,然后以某种方式强制对正在运行的程序的更改?

Similarly, would it be possible to create a copy of one of the project's source files, edit the code in the file, and then somehow force the changes upon the running program?

我不关心实际应用程序这一点。我只是探索我有关编程的不同想法。我也明白这是潜在的各种灾难。编辑正在运行的代码,并且包括实时的类(我不认为会在项目构建时检查其他类的错误),可能产生非常不可预测的结果。我只想玩弄这个想法。

I do not care so much about practical application at this point. I am merely exploring different ideas I have had relating to programming. I also understand that this is potential for all sorts of disaster. Editing running code, and including classes on the fly (which I don't imagine would be checked for errors as the other classes are when the project is built), could have very unpredictable results. I merely want to play around with the idea.

这就是说,如果任何人有任何有用的警告或事情要注意,我会感谢他们。否则,我将欣赏,如果人们不回应这是一个坏主意或有更容易和更好的方法来解决问题。我不是试图解决这个问题。

That said, if anyone has any HELPFUL warnings or things to look out for, I would appreciate them. Otherwise, I would appreciate if people kept from responding 'This is a bad idea' or 'there are easier and better ways to solve problems'. I am NOT trying to solve a problem with this. I am only exploring the idea.

推荐答案

Javassist将允许您修改现有类并在运行时创建新类。
http://www.csg.is.titech.ac .jp /〜chiba / javassist /

Javassist will let you modify existing classes and create new classes at run time. http://www.csg.is.titech.ac.jp/~chiba/javassist/

在Javassist 教程有一个从头开始定义新类的部分。查看 API 以查看如何添加新方法等。查看

In the Javassist tutorial there is a section for defining a new class from scratch. Check out the API to see how to add new methods and so on. Check out CtNewMethod.make in the Javassist API.

Javassist是JBoss用来实现面向方面编程的方法。

Javassist is what is used by JBoss to implement aspect oriented programming.

您还可以查看 EATS (仪器方法将感兴趣),它利用Javassist在运行时向现有方法添加新代码。 Eats不是发布版本,但它的工作原理:o

You can also check out EATS (the instrument method will be of interest) which makes use of Javassist to add new code to existing methods at runtime. Eats isn't at a release version, but it works :o

JPDA 提供了一些修改已经由JVM加载和运行的类的机制。

JPDA provides some mechanisms for modifying classes that are already loaded by and running in the JVM.

这篇关于在运行时编写和实现新的Java类文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆