package io.github.ibuildthecloud.gdapi.util;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

public class ProxyUtils {

    @SuppressWarnings("unchecked")
    public static <T> T proxy(final Map<String, Object> map, Class<T> typeClz) {
        final Object obj = new Object();

        return (T)Proxy.newProxyInstance(typeClz.getClassLoader(), new Class<?>[] { typeClz }, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                if (method.getDeclaringClass() == Object.class) {
                    return method.invoke(obj, args);
                }

                if (method.getName().startsWith("get")) {
                    String name = StringUtils.uncapitalize(method.getName().substring(3));
                    Object val = map.get(name);
                    if (val instanceof Long && (Integer.class.equals(method.getReturnType()) || Integer.TYPE.equals(method.getReturnType()))) {
                        return ((Long)val).intValue();
                    }
                    return val;
                }

                if (method.getName().startsWith("set") && args.length == 1) {
                    String name = StringUtils.uncapitalize(method.getName().substring(3));
                    map.put(name, args[0]);
                }

                return null;
            }
        });
    }
}

@@ -519,24 +519,66 @@
 Handler() %7B%0A
+            @SuppressWarnings(%22rawtypes%22)%0A
             
@@ -1231,32 +1231,246 @@
               %7D%0A
+                    if (val instanceof String && method.getReturnType().isEnum()) %7B%0A                        return Enum.valueOf((Class%3C? extends Enum%3E)method.getReturnType(), val.toString());%0A                    %7D%0A
                 

Support enums in proxying request objects