0

I seen some APIs accepting method as parameter. But no idea how do they actually work. May be using some reflection inside but no idea how.

Please tell me how should I write a method which can accept a method as input? For example;

someMethod(on(SomeClass.class).someMethod());

Alternative:

someMethod("fieldName" , SomeClass.class);

But it is not type Safe. And creating enum would be an extra burden.

4
  • Are you sure it is a method and not a field name? Because it looks like a field name. Commented May 3, 2013 at 3:03
  • Do you mean something like this someMethod(obj.getFoo())? Commented May 3, 2013 at 3:03
  • yup, it is a getter method for a field. Sorry for typo. I have update the question Commented May 3, 2013 at 6:23
  • Please refer stackoverflow.com/questions/8360510/java-reflection-getmethod And use java reflection Method class as type Commented May 3, 2013 at 6:26

4 Answers 4

2

In Java you would normally write an interface like this:

public interface Function{
    public void apply(); 
}

Then you can use your function like this:

static void main( String args[] ){
    callSomeMethod( new Function(){
        public void apply(){
            System.out.println( "hurray" ); 
        }
    } );
}

static void callSomeMethod( Function f ){
    f.apply(); 
}

In Java 8 this will be greatly simplified with the addition of lambdas. But for the time being this is how it's done in java.

Built-in classes

Java has a few built in classes that are generic. For instance take a look at callable: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Callable.html

Reflection

You can also use reflection. The above example would look like this:

class Thing{
    public void whatever(){
        System.out.println( "hi!" ); 
    }
}

public static void main( String args[] ){
    Thing thing = new Thing(); 
    callSomeMethod( thing, "whatever" ); 
}

public static void callSomeMethod( Object obj, String methodName ){
    obj.getClass().getMethod( methodName ).invoke( obj ); 
}

However, reflection is pretty slow compared to the above method and you also lose the static type safety. All in all i suggest not doing it this way if not absolutely necessary.

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

Comments

1

In java usually this is done with a class that implements Runnable or Callable

So instead of passing in a function, you pass an object that has a function that can be called.

2 Comments

yes and no. runnable is a class with a concrete purpose. i prefer to use the more generic callable. in cases where you need parameters you'll have to write your own interface.
@kritzikratzi thanks, I only ever used java for a few courses in school, I'm lucky to remember as much as I do ;)
0

The generic way to do this is Callable, but most passing methods around in java is usually done through creating an interface. Inside the interface you define several methods which reflect what you want to do, then you pass classes that implement the interface as the method arguments.

Comments

0

If the method already exists in a class, you can use java reflection. Have a look here java reflection tutorial

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.