Java 中的动态函数创建

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5314955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:34:41  来源:igfitidea点击:

Dynamic Function Creation in Java

javamethodsjava-6

提问by Briggs

So I'm trying to figure out if there is some method to dynamically create/assign a method to a class in Java. If it were C, I would just do it as follows using pointers:

所以我想弄清楚是否有一些方法可以在 Java 中动态地创建/分配一个方法给一个类。如果是 C,我会使用指针按如下方式执行:

public class Foo {  

  void bar(void *ptr) {....}  

};  

int main() {  
  Foo f = new Foo();  
  f.bar({"my function" ...})  
}  

However, Java of course has no pointers, so is there any way to get a similar functionality out of a Java application?

然而,Java当然没有指针,那么有没有办法从Java应用程序中获得类似的功能呢?

采纳答案by Pa?lo Ebermann

In Java, you would normally declare an interface with a method to be called. For example, if your function simply wants to execute some code, you would declare a Runnable and implement its run method.

在 Java 中,您通常会声明一个带有要调用的方法的接口。例如,如果你的函数只是想执行一些代码,你可以声明一个 Runnable 并实现它的 run 方法。

public class Foo {
    void bar(Runnable function) {
       for(int i = 0; i < 5; i++) {
           function.run();
       }
    }

    static void myFunction() {
         System.out.println("my Function!");
    }

    public static void main(String[] ignored) {
         Foo f = new Foo();
         f.bar( new Runnable() { public void run() {
             myFunction();
         }});
    }

}

回答by Jim Garrison

To generate truly dynamic methods you need a bytecode-manipulation library, such as Javassistor cglib.

要生成真正的动态方法,您需要一个字节码操作库,例如Javassistcglib

回答by d-live

In java it is achieved by something called anonymous classes, here is an example -

在 Java 中,它是通过称为匿名类的东西来实现的,这是一个示例 -

abstract class Bar {
    public void myfunc();
}

public class Client {

    public void execute()
    {
        doSomething(new Bar() {
            // define your dynamic function here ie provide its implementation
            public void myfunc() {
                //do whatever
            }
        });
    }

    public void doSomething(Bar b)
    {
        b.myfunc();
    }
}

回答by Amir Afghani

Here's a linkto how you can use the built in runtime version of javac to compile classes you define on the fly.

这里有一个链接,说明如何使用内置的 javac 运行时版本来编译动态定义的类。

回答by Ben

If you really want to change classes at runtime, the only way is to actually modify the bytecode, assuming your set-up allows it (Java security would normally kick in). That said, there's an java.lang.instrumentpackage in Java 6 which may help:

如果您真的想在运行时更改类,唯一的方法是实际修改字节码,假设您的设置允许(Java 安全性通常会启动)。也就是说,java.lang.instrumentJava 6 中有一个包可能会有所帮助:

http://download.oracle.com/javase/6/docs/api/java/lang/instrument/package-summary.html

http://download.oracle.com/javase/6/docs/api/java/lang/instrument/package-summary.html

You might find the cglib project of use also:

您可能还会发现使用的 cglib 项目:

http://sourceforge.net/projects/cglib/

http://sourceforge.net/projects/cglib/

回答by user unknown

See http://functionaljava.org/for a whole functional library for Java.

有关Java 的完整函数库,请参阅http://functionaljava.org/

回答by Daniel

You can use the Java Scripting API, create the function as a Script and call it. But only do this if your functions are really completely defineable at runtime, because interpreting scripts is always slower than implementing it in native Java.

您可以使用 Java Scripting API,将函数创建为脚本并调用它。但只有在您的函数在运行时真的完全可定义时才这样做,因为解释脚本总是比在原生 Java 中实现它慢。