public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
//Object obj=method.invoke(target, args);
return null;
}
});
proxy1.setAge(1);
}
}
复制代码
错误信息显示:
Exception in thread "main" java.lang.IllegalArgumentException: Exercise.Student is not an interface
at java.lang.reflect.Proxy.getProxyClass(Proxy.java:362)
at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:581)
at Exercise.c2.main(c2.java:16)
new Class[]{Student.class}, 这儿的参数是一个接口类的字节码(Class),而Student类不是接口,所以student.class并非接口的字节码(Class);
所以会报Exception in thread "main" java.lang.IllegalArgumentException: Exercise.Student is not an interface错误的参数类型错误;
你可以定义一个Person接口,然后让Student类实现;最后把new Class[]{Student.class}改为new Class[]{Person.class}
newProxyInstance
public static Object newProxyInstance(ClassLoader loader,
Class<?>[] interfaces,
InvocationHandler h)
throws IllegalArgumentException
Returns an instance of a proxy class for the specified interfaces that dispatches method invocations to the specified invocation handler. This method is equivalent to:
Proxy.getProxyClass(loader, interfaces).
Proxy.newProxyInstance throws IllegalArgumentException for the same reasons that Proxy.getProxyClass does.
Parameters:
loader - the class loader to define the proxy class
interfaces - the list of interfaces for the proxy class to implement
h - the invocation handler to dispatch method invocations to