public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) Returns a proxy instance for the specified interfaces that dispatches method invocations to the specified invocation handler. IllegalArgumentException will be thrown if any of the following restrictions is violated:
All of Class objects in the given interfaces array must represent interfaces, not classes or primitive types. No two elements in the interfaces array may refer to identical Class objects. All of the interface types must be visible by name through the specified class loader. In other words, for class loader cl and every interface i, the following expression must be true: Class.forName(i.getName(), false, cl) == i
All of the types referenced by all public method signatures of the specified interfaces and those inherited by their superinterfaces must be visible by name through the specified class loader. All non-public interfaces must be in the same package and module, defined by the specified class loader and the module of the non-public interfaces can access all of the interface types; otherwise, it would not be possible for the proxy class to implement all of the interfaces, regardless of what package it is defined in. For any set of member methods of the specified interfaces that have the same signature: If the return type of any of the methods is a primitive type or void, then all of the methods must have that same return type. Otherwise, one of the methods must have a return type that is assignable to all of the return types of the rest of the methods. The resulting proxy class must not exceed any limits imposed on classes by the virtual machine. For example, the VM may limit the number of interfaces that a class may implement to 65535; in that case, the size of the interfaces array must not exceed 65535. Note that the order of the specified proxy interfaces is significant: two requests for a proxy class with the same combination of interfaces but in a different order will result in two distinct proxy classes.
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 Returns: a proxy instance with the specified invocation handler of a proxy class that is defined by the specified class loader and that implements the specified interfaces Throws: IllegalArgumentException - if any of the restrictions on the parameters are violated SecurityException - if a security manager, s, is present and any of the following conditions is met: the given loader is null and the caller's class loader is not null and the invocation of s.checkPermission with RuntimePermission("getClassLoader") permission denies access; for each proxy interface, intf, the caller's class loader is not the same as or an ancestor of the class loader for intf and invocation of s.checkPackageAccess() denies access to intf; any of the given proxy interfaces is non-public and the caller class is not in the same runtime package as the non-public interface and the invocation of s.checkPermission with ReflectPermission("newProxyInPackage.{package name}") permission denies access. NullPointerException - if the interfaces array argument or any of its elements are null, or if the invocation handler, h, is null
invocationHandlerHashCode: {789451787} method: {public abstract void DynamicProxyTest3$ISay.say(java.lang.String)} proxyInstanceHashCode: {1950409828} proxyInstanceClass: {class $Proxy0} Hello.say: we can reach corner in the world
invocationHandlerHashCode: {789451787} method: {public abstract void DynamicProxyTest3$ISay.say(java.lang.String)} proxyInstanceHashCode: {1950409828} proxyInstanceClass: {class $Proxy0} Hello.say: Across the Great Wall
Exception in thread "main" java.lang.ClassCastException: $Proxy0 cannot be cast to DynamicProxyTest3$Hello at DynamicProxyTest3.main(DynamicProxyTest3.java:55)