English 中文(简体)
我怎样才能改变班级行为呢?
原标题:How can I change change class behaviour on-the-fly?

I am trying to edit a class file and make it available to the JVM at runtime. Is that possible, how can I do it?

例如:

main() {
    for (int i=0;i<10;i++) {
        NewClass.method();
        //ask user if to continue..
    }
}

public class NewClass() {
    static void method() {
        sysout("hi");
    }
}

当此循环执行时, 我要更改文件 NewClasss () 并在 JVM 中装入它, 以便它打印“ 再见 ” 。

这是完整的代码:

try {
    for (int iCount = 0; iCount < 10; iCount++) {
        BufferedReader br = new BufferedReader(new InputStreamReader(
                System.in));
        System.out.print("Enter which method:");
        int i = Integer.parseInt(br.readLine());
        System.out.println(i);
        if (i == 1) {
            Called.calledMethod1();
        } else {
            Called.calledMethod2();
        }
    }
} catch (NumberFormatException nfe) {
    System.err.println("Invalid Format!");
}

我想运行一个类的主要方法, 当它运行时, 我想引用到另一个类, 编辑它, 并且将第二个类 以相同的方法引用, 并获得不同的输出 。

don t 想要:

  1. stop the jvm
  2. edit the code
  3. run the code again.

我想,我想,我想

  1. edit the code at run time, and get the changes reflected immediately.
最佳回答

你必须使用爪哇反射法。

  1. you load dynamically a class at runtime using Class forName method.
  2. Using invoke method on the instance of Class you got,you can call any static method you want giving it s name.

如果您的班级在设计时间是固定的, 请保留第一个点。 有些代码 :

...

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter which class:");
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
className=br.readLine()
int results = compiler.run(null, null, null, className+".java");
if(results == 0){

    Class clazz = Class.forName(className+".class");
    System.out.print("Compiled successfully.Enter which method:");
    Object returnValue=clazz.getMethod(br.readLine()).invoke(null);
}
...

但请注意安全问题:上面的代码允许任何人执行每个班级在运行时可以使用的每个静态方法。如果安全是您关注的一个问题,那么您最好先验证控制台输入的内容,然后测试它们是否与您通过控制台捕捉的一种类方法相匹配。

<强度 > EDIT :

阅读您的评论后,我更能理解您的问题。为了在运行时编译一个课程,如果您针对 Java 6, 您可以使用 < a href="http://docs.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html" rel=“nofollow”>Java Comparenter 物件 。 我编辑了该代码以包括 Java Confiler 的用法 。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签