io.gsonfire.util.reflection.MethodInvoker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gson-fire Show documentation
Show all versions of gson-fire Show documentation
A java library that adds some very useful features to Gson, like Date serializing to unix timestamp or RFC3339, method (getter) serialization, pre and post processors and many more. Check out the documentation to learn how to use it!
package io.gsonfire.util.reflection;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* Created by julio on 12/12/15.
*/
public class MethodInvoker {
private final Method method;
private final List argsOrder;
public MethodInvoker(Method method, Set supportedInjectionTypes) {
this.method = method;
this.argsOrder = new ArrayList(supportedInjectionTypes.size());
for (Class parameterType : this.method.getParameterTypes()) {
if (supportedInjectionTypes.contains(parameterType)) {
argsOrder.add(parameterType);
} else {
throw new IllegalArgumentException("Cannot auto inject type: " + parameterType);
}
}
}
public void invoke(Object obj, ValueSupplier supplier) throws InvocationTargetException, IllegalAccessException {
Object[] args = new Object[method.getParameterTypes().length];
for (int i = 0; i < args.length; i++) {
args[i] = supplier.getValueForType(argsOrder.get(i));
}
this.method.invoke(obj, args);
}
public interface ValueSupplier {
Object getValueForType(Class type);
}
}