Skip to content

Instantly share code, notes, and snippets.

@jarrodhroberson
Created September 28, 2015 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jarrodhroberson/4ad5a988dbcf36f6a3ae to your computer and use it in GitHub Desktop.
Save jarrodhroberson/4ad5a988dbcf36f6a3ae to your computer and use it in GitHub Desktop.
package com.vertigrated.rtti;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.reflect.AbstractInvocationHandler;
import com.google.common.reflect.Reflection;
import javax.annotation.Nonnull;
import java.lang.reflect.Method;
import java.util.Map;
public class MapBackedJavaBeanProxy extends AbstractInvocationHandler
{
public static <T> Builder<T> as(@Nonnull final Class<T> cls)
{
return new Builder<T>()
{
@Override public T from(@Nonnull final Map<String, Object> map)
{
return Reflection.newProxy(cls, new MapBackedJavaBeanProxy(map));
}
};
}
private final Map<String, Object> values;
private MapBackedJavaBeanProxy(@Nonnull final Map<String, Object> values)
{
if (values instanceof ImmutableSortedMap) { this.values = values; }
else { this.values = ImmutableSortedMap.copyOf(values); }
}
@Override
protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable
{
final String methodName = method.getName();
if (this.values.containsKey(methodName)) { return this.values.get(methodName); }
else { throw new IllegalArgumentException(methodName + " does not exist in " + Joiner.on(',').join(values.keySet())); }
}
@Override
public boolean equals(Object obj) { return super.equals(obj); }
@Override
public String toString() { return Joiner.on(',').withKeyValueSeparator(":").join(this.values); }
interface Builder<T>
{
public T from(@Nonnull final Map<String, Object> map);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment