package com.jiuqi.mobile.nigo.comeclose.manager; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; public class ManagerFactory { @SuppressWarnings("unchecked") public static <T> T instanceManager(Class<T> c, String session){ if(c.isInterface()){ return instantceInterface(c, session); } return (T) Proxy.newProxyInstance(ManagerFactory.class.getClassLoader(), c.getInterfaces(), instanceInvocation(c, session)); } private synchronized static <T> InvocationHandler instanceInvocation(Class<T> c, String session) { try { Class<?> inv = Class.forName("com.jiuqi.mobile.nigo.biz.core.manager.ManagerInvocationHandler", true, Thread.currentThread().getContextClassLoader()); return (InvocationHandler) inv.getConstructors()[0].newInstance(c, session); } catch (Exception e) { throw new RuntimeException("实例化Manager时,invocation构建失败",e); } } // private static InvocationHandler instanceManagerInvocation(Class<?> managerClass, String session){ // return instanceInvocation(managerClass, session); // } @SuppressWarnings("unchecked") private static <T> T instantceInterface(Class<T> c, String session){ ManagerAnnotation ma = c.getAnnotation(ManagerAnnotation.class); if(null == ma){ throw new RuntimeException("Manager未使用ManagerAnnotation标签"); } try { Class<?> impl = Class.forName(ma.implClass(), true, Thread.currentThread().getContextClassLoader()); return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[]{c}, instanceInvocation(impl, session)); } catch (ClassNotFoundException e) { throw new RuntimeException("Manager实例化失败",e); } } }