snippetjavaMinor
How can I improve my Java MethodPointer?
Viewed 0 times
canjavaimprovemethodpointerhow
Problem
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
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 {
//Logger instance
private static final Logger LOGGER = Logger.getLogger(MethodPointer.class);
//Object fields
private final Method method;
private final Class returnClass;
private ArrayList args = new ArrayList();
private T object = null;
//Constructors
public MethodPointer(Method method, Class returnClass) {
this.method = method;
this.returnClass = returnClass;
}
public MethodPointer(Class clazz, T object, String methodName, Class returnClass, Class ... 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 clazz, String methodName, Class returnClass, Class ... paramClasses) {
this(clazz, null, methodName, returnClass, paramClasses);
}
//Accessors and mutators
public Method
.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 {
//Logger instance
private static final Logger LOGGER = Logger.getLogger(MethodPointer.class);
//Object fields
private final Method method;
private final Class returnClass;
private ArrayList args = new ArrayList();
private T object = null;
//Constructors
public MethodPointer(Method method, Class returnClass) {
this.method = method;
this.returnClass = returnClass;
}
public MethodPointer(Class clazz, T object, String methodName, Class returnClass, Class ... 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 clazz, String methodName, Class returnClass, Class ... paramClasses) {
this(clazz, null, methodName, returnClass, paramClasses);
}
//Accessors and mutators
public Method
Solution
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
All you have to do is implement
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.
Function interfaces that Guava uses:public interface Function {
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.
Code Snippets
public interface Function<F,T> {
T apply(F input);
boolean equals(Object object);
}Context
StackExchange Code Review Q#32998, answer score: 4
Revisions (0)
No revisions yet.