Skip to content

Commit 9b34e7a

Browse files
committed
Fix for http://code.google.com/p/mybatis/issues/detail?id=362 . Mappers should return a not null value from toString(), hashCode() and equals()
1 parent 73d8691 commit 9b34e7a

File tree

1 file changed

+4
-18
lines changed

1 file changed

+4
-18
lines changed

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

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,30 @@
44
import java.lang.reflect.InvocationHandler;
55
import java.lang.reflect.Method;
66
import java.lang.reflect.Proxy;
7-
import java.util.HashSet;
8-
import java.util.Set;
97

108
import org.apache.ibatis.session.SqlSession;
119

1210
public class MapperProxy implements InvocationHandler, Serializable {
1311

1412
private static final long serialVersionUID = -6424540398559729838L;
15-
16-
private static final Set<String> OBJECT_METHODS = new HashSet<String>() {
17-
private static final long serialVersionUID = -1782950882770203582L;
18-
{
19-
add("toString");
20-
add("getClass");
21-
add("hashCode");
22-
add("equals");
23-
add("wait");
24-
add("notify");
25-
add("notifyAll");
26-
}};
27-
2813
private SqlSession sqlSession;
2914

3015
private <T> MapperProxy(SqlSession sqlSession) {
3116
this.sqlSession = sqlSession;
3217
}
3318

3419
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
35-
if (!OBJECT_METHODS.contains(method.getName())) {
20+
if (method.getDeclaringClass() != Object.class) {
3621
final Class<?> declaringInterface = findDeclaringInterface(proxy, method);
3722
final MapperMethod mapperMethod = new MapperMethod(declaringInterface, method, sqlSession);
3823
final Object result = mapperMethod.execute(args);
3924
if (result == null && method.getReturnType().isPrimitive() && !method.getReturnType().equals(Void.TYPE)) {
4025
throw new BindingException("Mapper method '" + method.getName() + "' (" + method.getDeclaringClass() + ") attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");
4126
}
4227
return result;
28+
} else {
29+
return method.invoke(this, args);
4330
}
44-
return null;
4531
}
4632

4733
private Class<?> findDeclaringInterface(Object proxy, Method method) {
@@ -67,7 +53,7 @@ private Class<?> findDeclaringInterface(Object proxy, Method method) {
6753
}
6854

6955
@SuppressWarnings("unchecked")
70-
public static <T> T newMapperProxy(Class<T> mapperInterface, SqlSession sqlSession) {
56+
public static <T> T newMapperProxy(Class<T> mapperInterface, SqlSession sqlSession) {
7157
ClassLoader classLoader = mapperInterface.getClassLoader();
7258
Class<?>[] interfaces = new Class[]{mapperInterface};
7359
MapperProxy proxy = new MapperProxy(sqlSession);

0 commit comments

Comments
 (0)