All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.gsonfire.util.reflection.MethodInvoker Maven / Gradle / Ivy

Go to download

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!

There is a newer version: 1.9.0
Show newest version
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);

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy