/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2017 abel533@gmail.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.github.pagehelper.util;

import com.github.pagehelper.PageException;
import org.apache.ibatis.reflection.MetaObject;

import java.lang.reflect.Method;

/**
 * @author liuzh
 */
public class MetaObjectUtil {
    public static Method method;

    static {
        try {
            // 高版本中的 MetaObject.forObject 有 4 个参数，低版本是 1 个
            // 下面这个 MetaObjectWithReflectCache 带反射的缓存信息
            Class<?> metaClass = Class.forName("com.github.pagehelper.util.MetaObjectWithReflectCache");
            method = metaClass.getDeclaredMethod("forObject", Object.class);
        } catch (Throwable e1) {
            try {
                Class<?> metaClass = Class.forName("org.apache.ibatis.reflection.MetaObject");
                method = metaClass.getDeclaredMethod("forObject", Object.class);
            } catch (Exception e2) {
                throw new PageException(e2);
            }
        }

    }

    public static MetaObject forObject(Object object) {
        try {
            return (MetaObject) method.invoke(null, object);
        } catch (Exception e) {
            throw new PageException(e);
        }
    }

}

@@ -1488,16 +1488,121 @@
 %E7%89%88%E6%9C%AC%E6%98%AF 1 %E4%B8%AA%0A
+            //%E5%85%88%E5%88%A4%E6%96%AD%E5%BD%93%E5%89%8D%E4%BD%BF%E7%94%A8%E7%9A%84%E6%98%AF%E5%90%A6%E4%B8%BA%E9%AB%98%E7%89%88%E6%9C%AC%0A            Class.forName(%22org.apache.ibatis.reflection.ReflectorFactory%22);%0A
         
@@ -1649,16 +1649,16 @@
 %E5%8F%8D%E5%B0%84%E7%9A%84%E7%BC%93%E5%AD%98%E4%BF%A1%E6%81%AF%0A
-
         
@@ -1955,32 +1955,38 @@
 atis.reflection.
+System
 MetaObject%22);%0A  
@@ -2097,24 +2097,275 @@
 ption e2) %7B%0A
+                try %7B%0A                    Class%3C?%3E metaClass = Class.forName(%22org.apache.ibatis.reflection.MetaObject%22);%0A                    method = metaClass.getDeclaredMethod(%22forObject%22, Object.class);%0A                %7D catch (Exception e3) %7B%0A    
             
@@ -2397,12 +2397,30 @@
 on(e
-2
+3
 );%0A
+                %7D%0A
     
@@ -2421,32 +2421,32 @@
 %7D%0A            %7D%0A
+
         %7D%0A%0A    %7D
@@ -2435,25 +2435,24 @@
 %7D%0A        %7D%0A
-%0A
     %7D%0A%0A    p

解决 MetaObject 版本兼容性问题 fixed #349