Created
October 30, 2015 23:49
-
-
Save electrum/bc0f63ad75a72bf92b84 to your computer and use it in GitHub Desktop.
JDBI ObjectBinder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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