反射與代理

ClassLoadTest .javajava

package ZHANG.Proxy;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collection;

public class ClassLoadTest {

	/**
	 * @param args
	 * @throws NoSuchMethodException 
	 * @throws SecurityException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws IllegalArgumentException 
	 */
	public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
		Class clazz = Proxy.getProxyClass(Collection.class.getClassLoader(),Collection.class);
		System.out.println(clazz.getName());
		
		Constructor[] constructors = clazz.getConstructors();
		for(Constructor constructor : constructors){
			String name = constructor.getName();
			StringBuilder sb = new StringBuilder(name);
			sb.append('(');
				Class[] classparams = constructor.getParameterTypes();
				for(Class classparam : classparams){
					sb.append(classparam.getName()).append(',');
				}
				if(constructors!=null && classparams.length!=-1)
					sb.deleteCharAt(sb.length()-1);
			sb.append(')');
			System.out.println(name);
		}
		System.out.println("------------------------method list");
		Method[] Methods = clazz.getMethods();
		for(Method constructor : Methods){
			String name = constructor.getName();
			StringBuilder sb = new StringBuilder(name);
			sb.append('(');
				Class[] classparams = constructor.getParameterTypes();
				for(Class classparam : classparams){
					sb.append(classparam.getName()).append(',');
				}
				if(constructors!=null && classparams.length!=-1)
					sb.deleteCharAt(sb.length()-1);
			sb.append(')');
			System.out.println(name);
		}
		//第一種方式
		Constructor constructor = clazz.getConstructor(InvocationHandler.class);
		class MyInvocationHandler implements InvocationHandler{

			public Object invoke(Object proxy, Method method, Object[] args)
					throws Throwable {
				// TODO Auto-generated method stub
				return null;
			}
		}
		Collection proxy1 = (Collection) constructor.newInstance(new MyInvocationHandler());
		System.out.println(proxy1.toString());
		proxy1.clear();
		//proxy1.size();
		//第二種
		Collection proxy2 = (Collection)constructor.newInstance(new InvocationHandler(){
			public Object invoke(Object proxy, Method method, Object[] args)
					throws Throwable {
				// TODO Auto-generated method stub
				return null;
			}
		});
		
		//第三種 proxy與實例對象合二爲一
		Collection proxy3 = (Collection) Proxy.newProxyInstance(Collection.class.getClassLoader(), 
				new Class[]{Collection.class}, 
				new InvocationHandler(){
					ArrayList target = new ArrayList();//放在Invoke裏每次調用會返回0,放外面是成員變量了
					public Object invoke(Object proxy, Method method,
							Object[] args) throws Throwable {
						long begintime = System.currentTimeMillis();
						Object retVal = method.invoke(target, args);
						long endtime = System.currentTimeMillis();
						System.out.println("runnig time = "+(begintime-endtime));
						return retVal;
					}
		});
		proxy3.add("xyz");
		proxy3.add("yuws");
		proxy3.add("haha");
		System.out.println(proxy3.size());
	}

}
相關文章
相關標籤/搜索