4

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:

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?

2

7 Answers 7

4

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.

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();
         }});
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

This was exactly what I needed. Modifying this just a bit allowed me to create a different instance of a needed function in every instance of the class.
2

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

Comments

1

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

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();
    }
}

Comments

0

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.

Comments

0

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.instrument package in Java 6 which may help:

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

You might find the cglib project of use also:

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.