Thursday, February 14, 2013

Pointers to Functions in Java


It is often thought that the Java language does not have pointers to methods.  In some sense it is true, but the feature is kind of there – it is just a little trickier to use than other languages.
Java method pointers comes via reflection and the ability to have a reference to a method. Given a particular class, one can grab a reference to methods of that class. These methods can then be passed around and invoked - as one would expect with pointers to methods.

Let's see how this works, say we had a class Person defined as such and we want to pass around specific methods of this class.

public class Person {
    String firstName, lastName;
    int age;
    public Person(String f, String l) {
        firstName=f;
        lastName=l;
    }
    public String getFullName() {
        return firstName + " " +lastName;
    }
     public void setAge(int age ) {
         this.age=age;
    }
}



Getting a method from a class:

Using reflection we can obtain a method of a class by calling the getMethod() of a class, which can then be invoked  For example:

[1]    Person p = new Person("Geroge", "Washington");
[2]    Method method = Person.class.getMethod("getFullName");
[3]    String s  = (String) method.invoke(p);
[4]    System.out.println(s);

Lets break down the code.  Line one creates the an instance of the object.  Line 2 gets a reference to a method of the class person.  This is how we get a reference to a method!!  Line 3 invokes that method on a particular object (p), and finally we print out the results 


Handling Method Parameters and Arguments

The above method had no parameters.  Things get a bit more complex when we have to deal with method parameters.

Method method = Person.class.getMethod("setAge", int.class);
method.invoke(p, 33);

If the method has parameters, we need to tell Java "which" method we want, as there can be multiple methods with the same name but different parameters.  We let Java know which parameter in getMethod by supplying an array (or sequence) of "Class" types.  In this case, the method setAge takes an int, so we supply the class of int, which can be obtained by through int.class.  If it took a String we would use String.class

What if there were two arguments to that method?  Imagine there was a changeName method to class Person that took a new first and last name, we would obtain that method with the following call:

method = Person.class.getMethod("changeName", String.class, String.class);
method.invoke(p, "Thomas", "Jefferson");


Where to go from here?

Once you know how to grab a method of a class/object, you can do lots of interesting stuff.  You can pass method reference around between methods, you can store the methods in data structures to be invoked at another time, etc.

But in order to be effective at using method references in Java, you need to have a solid grasp of Reflection in Java.  And you will likely want to wrap the method references in a layer of abstraction, as the reflection classes are a bit cumbersome.

Check out some of the core docs:

- the Class docs 
- the Method docs

No comments:

Post a Comment