Skip to content

Commit 306634e

Browse files
committed
Minor refactor (thanks Simo) and some tests
1 parent 9b34e7a commit 306634e

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

src/main/java/org/apache/ibatis/binding/MapperProxy.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,16 @@ private <T> MapperProxy(SqlSession sqlSession) {
1717
}
1818

1919
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
20-
if (method.getDeclaringClass() != Object.class) {
21-
final Class<?> declaringInterface = findDeclaringInterface(proxy, method);
22-
final MapperMethod mapperMethod = new MapperMethod(declaringInterface, method, sqlSession);
23-
final Object result = mapperMethod.execute(args);
24-
if (result == null && method.getReturnType().isPrimitive() && !method.getReturnType().equals(Void.TYPE)) {
25-
throw new BindingException("Mapper method '" + method.getName() + "' (" + method.getDeclaringClass() + ") attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");
26-
}
27-
return result;
28-
} else {
20+
if (method.getDeclaringClass() == Object.class) {
2921
return method.invoke(this, args);
3022
}
23+
final Class<?> declaringInterface = findDeclaringInterface(proxy, method);
24+
final MapperMethod mapperMethod = new MapperMethod(declaringInterface, method, sqlSession);
25+
final Object result = mapperMethod.execute(args);
26+
if (result == null && method.getReturnType().isPrimitive() && !method.getReturnType().equals(Void.TYPE)) {
27+
throw new BindingException("Mapper method '" + method.getName() + "' (" + method.getDeclaringClass() + ") attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");
28+
}
29+
return result;
3130
}
3231

3332
private Class<?> findDeclaringInterface(Object proxy, Method method) {

src/test/java/org/apache/ibatis/binding/BindingTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,5 +400,38 @@ public void shouldSelectDraftTypedPostsWithResultMap() {
400400
}
401401
}
402402

403+
@Test
404+
public void shouldReturnANotNullToString() throws Exception {
405+
SqlSession session = sqlSessionFactory.openSession();
406+
try {
407+
BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
408+
assertNotNull(mapper.toString());
409+
} finally {
410+
session.close();
411+
}
412+
}
413+
414+
@Test
415+
public void shouldReturnANotNullHashCode() throws Exception {
416+
SqlSession session = sqlSessionFactory.openSession();
417+
try {
418+
BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
419+
assertNotNull(mapper.hashCode());
420+
} finally {
421+
session.close();
422+
}
423+
}
424+
425+
@Test
426+
public void shouldCompareToMappers() throws Exception {
427+
SqlSession session = sqlSessionFactory.openSession();
428+
try {
429+
BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
430+
BoundBlogMapper mapper2 = session.getMapper(BoundBlogMapper.class);
431+
assertFalse(mapper.equals(mapper2));
432+
} finally {
433+
session.close();
434+
}
435+
}
403436

404437
}

0 commit comments

Comments
 (0)