本帖最后由 位丹丹 于 2012-7-12 12:50 编辑
- package cn.itcast.day3;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.InvocationHandler;
- import java.lang.reflect.Method;
- import java.lang.reflect.Proxy;
- import java.util.Collection;
- public class ProxyTest {
- /**
- * @param args
- */
- public static void main(String[] args)throws Exception {
- // TODO Auto-generated method stub
- Class clazzProxy1 = Proxy.getProxyClass(Collection.class.getClassLoader(), Collection.class);
- System.out.println(clazzProxy1.getName());
- System.out.println("------begin constructors list------");
- /*$Proxy0()
- $Proxy0(InvocationHandler,int)*/
- Constructor[] constructors = clazzProxy1.getConstructors();
- for(Constructor constructor : constructors){
- String name = constructor.getName();
- StringBuilder sBuilder = new StringBuilder(name);
- sBuilder.append('(');
- Class[] clazzParams = constructor.getParameterTypes();
- for(Class clazzParam : clazzParams){
- sBuilder.append(clazzParam.getName()).append(',');
- }
- if(clazzParams!=null && clazzParams.length != 0)
- sBuilder.deleteCharAt(sBuilder.length()-1);
- sBuilder.append(')');
- System.out.println(sBuilder.toString());
- }
- System.out.println("------begin methods list------");
- /*$Proxy0()
- $Proxy0(InvocationHandler,int)*/
- Method[] methods = clazzProxy1.getMethods();
- for(Method method : methods){
- String name = method.getName();
- StringBuilder sBuilder = new StringBuilder(name);
- sBuilder.append('(');
- Class[] clazzParams = method.getParameterTypes();
- for(Class clazzParam : clazzParams){
- sBuilder.append(clazzParam.getName()).append(',');
- }
- if(clazzParams!=null && clazzParams.length != 0)
- sBuilder.deleteCharAt(sBuilder.length()-1);
- sBuilder.append(')');
- System.out.println(sBuilder.toString());
- }
- System.out.println("------begin create instance object------");
- //Object obj = clazzProxy1.newInstance();
- Constructor constructor = clazzProxy1.getConstructor(InvocationHandler.class);
- class MyInvocationHandler1 implements InvocationHandler {
- @Override
- public Object invoke(Object proxy, Method method, Object[] args)
- throws Throwable {
- // TODO Auto-generated method stub
- return null;
- }
- Collection proxy1 = (Collection)constructor.newInstance(new MyInvocationHandler1());//
- System.out.println(proxy1);
- }
- }
- }
复制代码最后一行打印代码System.out.println(proxy1);为什么不能打印?求解答。。。。。。
|
|