Skip to content

Commit

Permalink
better handling of class methods
Browse files Browse the repository at this point in the history
  • Loading branch information
brianm committed May 19, 2012
1 parent f00c8a5 commit 5a59aee
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
42 changes: 42 additions & 0 deletions src/main/java/org/skife/jdbi/v2/sqlobject/PassThroughHandler.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
3 changes: 1 addition & 2 deletions src/main/java/org/skife/jdbi/v2/sqlobject/SqlObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand All @@ -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();

}
}

0 comments on commit 5a59aee

Please sign in to comment.