动态代理性能比较

转自http://javatar.iteye.com/blog/814426
//用javassit得到动态代理
    public T createJavassistBytecodeDynamicProxy(LoadBalance loadBalance,ConcurrentMap<String, T> map,Class ifaces){
        try{
            ClassPool mPool = new ClassPool(true);
            CtClass mCtc = mPool.makeClass(ifaces.getName() + "JavaassistProxy");
            mCtc.addInterface(mPool.get(ifaces.getName()));
            mCtc.addConstructor(CtNewConstructor.defaultConstructor(mCtc));

            mCtc.addField(CtField.make("public " + loadBalance.getClass().getName() + " sub;", mCtc));
            mCtc.addField(CtField.make("public " + map.getClass().getName() + " map;", mCtc));
//            mCtc.addField(CtField.make("public " + ArrayList.class.getName() + " list;", mCtc));

            mCtc.addMethod(CtNewMethod.make("public Object getRealClient() { return (Object)sub.select(new "+ArrayList.class.getName()+"(map.values())); }",mCtc));

            //获取接口的方法
            for(Method method: ifaces.getMethods()){
                Class returnType = method.getReturnType();
                String modifiers =  "public";
                if(Modifier.PUBLIC==method.getModifiers()){
                    modifiers="public";
                }else if(Modifier.PROTECTED==method.getModifiers()){
                    modifiers="protected";
                }else if(Modifier.PRIVATE==method.getModifiers()){
                    modifiers="private";
                }
                Class<?>[] parameter = method.getParameterTypes();

                String params = "";
                String ps = "";
                for(Class param:parameter){
                   params += param.getName()+" "+param.getName()+",";
                   ps+=param.getName()+",";
                }
                if(params.equals("")){
                    params = "";
                    ps="";
                }else{
                    params=params.substring(0,params.length());
                    ps=ps.substring(0,ps.length());
                }
                
                mCtc.addMethod(CtNewMethod.make(modifiers +" void "+method.getName()+"(String a,String b){ Object t=this.getRealClient(); return (("+ifaces.getName()+")t)."+method.getName()+"(a,b) ;}", mCtc));
//                mCtc.addMethod(CtNewMethod.make("public int count() { return delegate.count(); }", mCtc));
            }


            Class<?> pc = mCtc.toClass();

            mCtc.debugWriteFile("/home/liguojun");
            mCtc.writeFile("/home/liguojun");

            T bytecodeProxy = (T) pc.newInstance();
            Field filed = bytecodeProxy.getClass().getField("sub");
            filed.set(bytecodeProxy, loadBalance);

            Field filed1 = bytecodeProxy.getClass().getField("map");
            filed1.set(bytecodeProxy, map);

            return bytecodeProxy;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;

    }


注意:但写new ArrayList时无法识别,必须全写为字符串形式,即new ArrayList.class.getName(),return (("+ifaces.getName()+")t)为同理

猜你喜欢

转载自kevin1.iteye.com/blog/1772244
今日推荐