/*
 * Copyright 2014-2015  All rights reserved.
 * Email: han.yanjingyy@gmail.com
 */

package org.mybatis.plugin.pager.resolver;

import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.plugin.pager.Constants;
import org.mybatis.plugin.pager.model.Page;

/**
 * @Description: 
 * @author Hanyanjing
 * @date 2016年7月1日 上午11:43:52   
 * @version 1.0
 */
public class PageParameterResolver {

    public static Page<?> resolveParam(Object parameterObject, MappedStatement ms) {
        if (parameterObject instanceof Page<?>) {
            /**
             * Mapper形如：List<User> selectUsers(Page<User> page);
             */
            return (Page<?>) parameterObject;
        } else if (parameterObject != null) {
            /**
             * Mapper形如：
             * (1) List<User> selectUsers(@Param(Constants.PAGE_PARAMERTER_NAME)Page<User> page,Object... arguments);
             * (2) 或者：List<User> selectUsers(Map<String,Object> params);
             * 且传入参数形如 Map<String,Object> params = new HashMap<String,Object>();
             * params.put(Constants.PAGE_PARAMERTER_NAME,new Page<Object>());
             */
            MetaObject metaObject = ms.getConfiguration().newMetaObject(parameterObject);
            Object resolved = metaObject.getValue(Constants.PAGE_PARAMERTER_NAME);
            if (resolved instanceof Page<?>) {
                return (Page<?>) resolved;
            }
        }
        return null;
    }

}

@@ -308,16 +308,73 @@
 el.Page;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0A/**%0A *
@@ -502,16 +502,112 @@
 lver %7B%0A%0A
+    private static final Logger LOGGER = LoggerFactory.getLogger(PageParameterResolver.class);%0A%0A
     publ
@@ -1448,16 +1448,67 @@
 solved =
+ null;%0A            try %7B%0A                resolved =
  metaObj
@@ -1565,18 +1565,517 @@
         
-if
+%7D catch (Exception e) %7B%0A                // Most happened when there is not page parameter%0A                // org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'page'%0A                LOGGER.info(%22Resolve Page parameter failed. Most likely the page parameter doesn't contained in the parameterObject%5B%7B%7D,%7B%7D%5D. WILL NOT PROCEED TO PAGE QUERY!%22,%0A                            parameterObject.getClass(), parameterObject);%0A            %7D%0A            if (resolved != null &&
  (resolv
@@ -2088,32 +2088,33 @@
 tanceof Page%3C?%3E)
+)
  %7B%0A             

solve the mybatis reflection exception issue