Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

better handling of class methods

  • Loading branch information...
commit 5a59aeed9c0dfda61c67e3450f7be0aa74bb5670 1 parent f00c8a5
@brianm brianm authored
View
42 src/main/java/org/skife/jdbi/v2/sqlobject/PassThroughHandler.java
@@ -0,0 +1,42 @@
+package org.skife.jdbi.v2.sqlobject;
+
+import net.sf.cglib.proxy.MethodProxy;
+
+import java.lang.reflect.Method;
+
+class PassThroughHandler implements Handler
+{
+
+ private final Method method;
+
+ PassThroughHandler(Method method)
+ {
+ this.method = method;
+ }
+
+ @Override
+ public Object invoke(HandleDing h, Object target, Object[] args, MethodProxy mp)
+ {
+ try {
+ return mp.invokeSuper(target, args);
+ }
+ catch (AbstractMethodError e) {
+ throw new AbstractMethodError("Method " + method.getDeclaringClass().getName() + "#" + method.getName() +
+ " doesn't make sense -- it probably needs a @Sql* annotation of some kind.");
+ }
+ catch (Throwable throwable) {
+ /*
+
+ */
+ if (throwable instanceof RuntimeException) {
+ throw (RuntimeException) throwable;
+ }
+ else if (throwable instanceof Error) {
+ throw (Error) throwable;
+ }
+ else {
+ throw new RuntimeException(throwable);
+ }
+ }
+ }
+}
View
3  src/main/java/org/skife/jdbi/v2/sqlobject/SqlObject.java
@@ -112,8 +112,7 @@ else if (mixinHandlers.containsKey(raw_method)) {
handlers.put(raw_method, mixinHandlers.get(raw_method));
}
else {
- throw new IllegalArgumentException("Method " + raw_method.getDeclaringClass()
- .getName() + "#" + raw_method.getName() + " doesn't make sense -- it probably needs a @Sql* annotation of some kind.");
+ handlers.put(raw_method, new PassThroughHandler(raw_method));
}
}
View
25 ...a/org/skife/jdbi/v2/sqlobject/TestTx.java → ...v2/sqlobject/TestClassBasedSqlObject.java
@@ -17,7 +17,7 @@
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
-public class TestTx
+public class TestClassBasedSqlObject
{
private DBI dbi;
private Handle handle;
@@ -63,6 +63,23 @@ public void testTxFail() throws Exception
assertThat(dao.findById(1), nullValue());
}
+ @Test
+ public void testPassThroughMethod() throws Exception
+ {
+ Dao dao = handle.attach(Dao.class);
+ dao.insert(3, "Cora");
+
+ Something c = dao.findByIdHeeHee(3);
+ assertThat(c, equalTo(new Something(3, "Cora")));
+ }
+
+ @Test(expected = AbstractMethodError.class)
+ public void testUnimplementedMethod() throws Exception
+ {
+ Dao dao = handle.attach(Dao.class);
+ dao.totallyBroken();
+ }
+
@RegisterMapper(SomethingMapper.class)
public static abstract class Dao
{
@@ -86,5 +103,11 @@ public Something failed(int id, String name) throws IOException
throw new IOException("woof");
}
+ public Something findByIdHeeHee(int id) {
+ return findById(id);
+ }
+
+ public abstract void totallyBroken();
+
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.