0

Here's the setup of my program. I want to tell the method checkerMethod which method to use, from either method1 or method2.

public Class executeCode{
World brave = new World();
brave.checkerMethod(Method method1);
}

My world class looks something like below.

public World{  //  changed to World from World() 

public World(){
//make world
}

public methodChecker(Method method){
//code involving using method1 or method2
//methods 1 and 2 are not static, so I would need to call this.method1()   
}

public void method1(){//method here}
public void method2(){//method here}

}

I have seen similar things, here, for instance

The solution above would not work obviously for passing non-static methods.

I have a feeling that if I rearrange things it may work, but I can't figure it out. Any help would be appreciated!

Community
  • 1
  • 1

3 Answers3

4

Instead of passing methods and doing things reflectively, just pass in an invocable object that wraps the method and closes over this.

If all the methods you'll be passing are void zero-argument methods you could just use java.lang.Runnable thus

// final local variables are available to inner classes
final World current = this;

World.methodChecker(
    new Runnable() {
      public void run() {
        current.method1();
        // Or alternatively {World.this.method1();}
      }
    });

If you need to actually return a value or pass parameters, then Guava's Function is not a bad choice.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
1

You can't do this in Java (without Reflection), but you can use a flag to mark which exact method to be executed:

public void methodChecker(boolean flag){
   flag ? method1() : method2();
}
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • Method class is already part of the reflection API, so probably author is expecting answer with reflection used. – Jakub H Mar 18 '14 at 16:51
  • I'm doing something similar now (I have more than two methods so I'm passing in a string and using a series of if statements), but it's not too pretty – Quincy Abrams Mar 18 '14 at 16:54
0

Right way is to consider redesign and make methodN() Runnable instead of method.

public class World {

public Runnable method1 = new Runnable() {

    @Override
    public void run() {
        System.out.println("1");
    }
};

public Runnable method2 = new Runnable() {

    @Override
    public void run() {
        System.out.println("2");
    }
};

public World() {
    //make world
}

public void methodChecker(Runnable method) {
    method.run();
}
}

It's certainly possible to keep methodN() as methods and run them using reflection. That is not a good practice, invocation is slower and you risk to face SecurityException on other environment. Do not use reflection until you have no choice. Solution is as follows:

public class World {

public World() {
    //make world
}

public void methodChecker(String method) {
    try {
        getClass().getMethod(method).invoke(this);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

public void method1() {
    System.out.println("1");
}

public void method2() {
    System.out.println("2");
}

}

The only difference with static methods invocation is that you pass reference to object to invoke method. In example with Runable there is no difference.

Sergey Fedorov
  • 2,169
  • 1
  • 15
  • 26