标题:动态代理问题
只看楼主
tompobing
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:260
专家分:809
注 册:2012-12-9
结帖率:78.13%
 问题点数:0 回复次数:0 
动态代理问题

想动态代理TreeSet集合,代理类能用的方法只能是Set集合里面的方法吗?难道不能用代理类的对象调用TreeSet集合特有的方法吗?(比如pollFirst方法)
求大神解答!!!


package com.lianxi.test02;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class Test1 {

    public static void main(String[] args) {
        
        final TreeSet ts = new TreeSet();
        Set proxy = (Set)Proxy.newProxyInstance(TreeSet.class.getClassLoader(),
                TreeSet.class.getInterfaces(),
                new InvocationHandler() {
                    
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args)
                            throws Throwable {
                        long beginTime = System.currentTimeMillis();
                        Object retVal = method.invoke(ts, args);
                        long endTime = System.currentTimeMillis();
                        System.out.println(method.getName()+" running time is "+(endTime-beginTime));
                        return retVal;
                    }
                });
        
        proxy.add("Tom");
        proxy.add("Tommy");
        proxy.add("Ben");
        
        System.out.println(proxy.toString());
    }
   
}

搜索更多相关主题的帖子: package public import 动态 
2014-09-05 16:58



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-435733-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.262063 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved