How can I improve my Java MethodPointer? This was just an experiment to see if I could replicate something like C++ function pointers in Java.  Basically, the idea was to have an object which represented a method call, and when you call .invoke() on that object, the method would run.

It feels a little sloppy to me, and I feel like there may be some tricks of Reflection and Generics that would make the code more elegant/usable which I haven't thought of or am not aware of.  Any tips or tricks?

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.jpgohlke.util.logging.LogUtil;

public class MethodPointer&lt;T, R&gt; {

    //Logger instance
    private static final Logger LOGGER = Logger.getLogger(MethodPointer.class);

    //Object fields
    private final Method method;
    private final Class&lt;R&gt; returnClass;
    private ArrayList&lt;Object&gt; args = new ArrayList&lt;Object&gt;();
    private T object = null;


    //Constructors
    public MethodPointer(Method method, Class&lt;R&gt; returnClass) {
        this.method = method;
        this.returnClass = returnClass;
    }

    public MethodPointer(Class&lt;T&gt; clazz, T object, String methodName, Class&lt;R&gt; returnClass, Class&lt;?&gt; ... paramClasses) {
        Method theMethod = null;
        try {
            theMethod = clazz.getMethod(methodName, paramClasses);
        }
        catch(NoSuchMethodException nsme) {
            LogUtil.log(LOGGER, Level.ERROR, "Unable to find method " + methodName + " in " + clazz.getSimpleName(), nsme);
            throw new IllegalArgumentException("Method signature does not exist in " + clazz.getSimpleName());
        }
        method = theMethod;
        this.object = object;
        this.returnClass = returnClass;
    }

    public MethodPointer(Class&lt;T&gt; clazz, String methodName, Class&lt;R&gt; returnClass, Class&lt;?&gt; ... paramClasses) {
        this(clazz, null, methodName, returnClass, paramClasses);
    }


    //Accessors and mutators
    public Method getMethod() {
        return method;
    }

    public Class&lt;R&gt; getReturnClass() {
        return returnClass;
    }

    public Object[] getArguments() {
        return args.toArray();
    }

    public void setArguments(Object ... args) {
        this.args = new ArrayList&lt;Object&gt;(Arrays.asList(args));
    }

    public void setArguments(ArrayList&lt;Object&gt; args) {
        this.args = args;
    }

    public T getObject() {
        return object;
    }

    public void setObject(T object) {
        this.object = object;
    }


    //Invoking the method
    public R invoke() throws Exception {

        if(Modifier.isStatic(method.getModifiers())) {
            object = null;
        }
        else if(object == null) {
            LOGGER.error("An object must be provided to invoke a non-static method upon.");
            throw new IllegalArgumentException("An object must be provided to invoke a non-static method upon.");
        }

        try {
            Object returnValue = method.invoke(object, args.toArray());
            if(returnClass.isInstance(returnValue)) {
                return returnClass.cast(returnValue);
            }
            else {
                LOGGER.error("Unable to return value from method invocation; the object is not an instance of " + returnClass.getSimpleName());
                return null;
            }
        }
        catch(Exception e) {
            LogUtil.log(LOGGER, Level.ERROR, "Unable to invoke method " + method.getName(), e);
            throw e;
        }
    }

    public R invoke(Object ... args) throws Exception {
        this.args.addAll(Arrays.asList(args));  
        return invoke(returnClass);
    }

}
It seams like there is a lot of work going on there for just a simple concept. I suspect you don't need all the flexibility and structure that this implementation gives you. Instead, I suggest you use a simple interfaces. Here is the Function interfaces that Guava uses:

public interface Function&lt;F,T&gt; {
    T apply(F input);
    boolean equals(Object object);
}


All you have to do is implement apply() and you are done. If it is a one off, then you can just create an anonymous class. If you are calling the same type of function repeatedly in a number of places or you need something slightly different, you can make a concrete class for those cases.

The good news is that Java 8 will make this much easier. The introduction of lambdas and method references makes it much easier to pass code to be called into a function. The Method Reference page has examples of lambdas and method references.