Skip to content

Instantly share code, notes, and snippets.

@electrum
Created October 30, 2015 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save electrum/bc0f63ad75a72bf92b84 to your computer and use it in GitHub Desktop.
Save electrum/bc0f63ad75a72bf92b84 to your computer and use it in GitHub Desktop.
JDBI ObjectBinder
public interface ObjectBinder<T>
{
void bind(SQLStatement<?> query, T value);
}
@BindingAnnotation(BindObjectFactory.class)
@Retention(RUNTIME)
@Target(PARAMETER)
public @interface BindObject
{
Class<? extends ObjectBinder<?>> value();
}
public static class BindObjectFactory
implements BinderFactory
{
@Override
public Binder<BindObject, ?> build(Annotation annotation)
{
return (query, object, value) -> createBinder(object).bind(query, value);
}
@SuppressWarnings("unchecked")
private static ObjectBinder<Object> createBinder(BindObject annotation)
{
try {
return (ObjectBinder<Object>) annotation.value().getConstructor().newInstance();
}
catch (ReflectiveOperationException e) {
throw Throwables.propagate(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment