package org.apache.ibatis.binding;

import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashSet;
import java.util.Set;

import org.apache.ibatis.session.SqlSession;

public class MapperProxy implements InvocationHandler, Serializable {

  private static final long serialVersionUID = -6424540398559729838L;

  private static final Set<String> OBJECT_METHODS = new HashSet<String>() {
    private static final long serialVersionUID = -1782950882770203582L;
  {
    add("toString");
    add("getClass");
    add("hashCode");
    add("equals");
    add("wait");
    add("notify");
    add("notifyAll");
  }};

  private SqlSession sqlSession;

  private <T> MapperProxy(SqlSession sqlSession) {
    this.sqlSession = sqlSession;
  }

  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (!OBJECT_METHODS.contains(method.getName())) {
      final Class<?> declaringInterface = findDeclaringInterface(proxy, method);
      final MapperMethod mapperMethod = new MapperMethod(declaringInterface, method, sqlSession);
      final Object result = mapperMethod.execute(args);
      if (result == null && method.getReturnType().isPrimitive() && !method.getReturnType().equals(Void.TYPE)) {
        throw new BindingException("Mapper method '" + method.getName() + "' (" + method.getDeclaringClass() + ") attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");
      }
      return result;
    }
    return null;
  }

  private Class<?> findDeclaringInterface(Object proxy, Method method) {
    Class<?> declaringInterface = null;
    for (Class<?> iface : proxy.getClass().getInterfaces()) {
      try {
        Method m = iface.getMethod(method.getName(), method.getParameterTypes());
        if (declaringInterface != null) {
          throw new BindingException("Ambiguous method mapping.  Two mapper interfaces contain the identical method signature for " + method);
        } else if (m != null) {
          declaringInterface = iface;
        }
      } catch (Exception e) {
        // Intentionally ignore.
        // This is using exceptions for flow control,
        // but it's definitely faster.
      }
    }
    if (declaringInterface == null) {
      throw new BindingException("Could not find interface with the given method " + method);
    }
    return declaringInterface;
  }

  @SuppressWarnings("unchecked")
public static <T> T newMapperProxy(Class<T> mapperInterface, SqlSession sqlSession) {
    ClassLoader classLoader = mapperInterface.getClassLoader();
    Class<?>[] interfaces = new Class[]{mapperInterface};
    MapperProxy proxy = new MapperProxy(sqlSession);
    return (T) Proxy.newProxyInstance(classLoader, interfaces, proxy);
  }

}

@@ -176,58 +176,8 @@
 xy;%0D
-%0Aimport java.util.HashSet;%0D%0Aimport java.util.Set;%0D
 %0A%0D%0Ai
@@ -371,321 +371,8 @@
 L;%0D%0A
-%0D%0A  private static final Set%3CString%3E OBJECT_METHODS = new HashSet%3CString%3E() %7B%0D%0A    private static final long serialVersionUID = -1782950882770203582L;%0D%0A  %7B%0D%0A    add(%22toString%22);%0D%0A    add(%22getClass%22);%0D%0A    add(%22hashCode%22);%0D%0A    add(%22equals%22);%0D%0A    add(%22wait%22);%0D%0A    add(%22notify%22);%0D%0A    add(%22notifyAll%22);%0D%0A  %7D%7D;%0D%0A%0D%0A
   pr
@@ -596,50 +596,50 @@
 if (
-!OBJECT_METHODS.contains(method.getName())
+method.getDeclaringClass() != Object.class
 ) %7B%0D
@@ -1251,26 +1251,63 @@
    %7D
-%0D%0A    return null;
+ else %7B%0D%0A      return method.invoke(this, args);%0D%0A    %7D
 %0D%0A  
@@ -2245,16 +2245,18 @@
 cked%22)%0D%0A
+  
 public s

Fix for http://code.google.com/p/mybatis/issues/detail?id=362 . Mappers should return a not null value from toString(), hashCode() and equals()