/** * Sets the invocation handler for a proxy. This method is less efficient than * {@link #setInvocationHandler(Object, InvocationHandler)}, however it will work on any proxy, not just proxies from a * specific factory. * * @param proxy the proxy to modify * @param handler the handler to use */ public static void setInvocationHandlerStatic(Object proxy, InvocationHandler handler) { try { final Field field = proxy.getClass().getDeclaredField(INVOCATION_HANDLER_FIELD); AccessController.doPrivileged(new SetAccessiblePrivilege(field)); field.set(proxy, handler); } catch (NoSuchFieldException e) { throw new RuntimeException("Could not find invocation handler on generated proxy", e); } catch (IllegalArgumentException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } }
/** * Gets the {@link InvocationHandler} for a given proxy instance. This method is less efficient than * {@link #getInvocationHandler(Object)}, however it will work for any proxy, not just proxies from a specific factory * instance. * * @param proxy the proxy * @return the invocation handler */ public static InvocationHandler getInvocationHandlerStatic(Object proxy) { try { final Field field = proxy.getClass().getDeclaredField(INVOCATION_HANDLER_FIELD); AccessController.doPrivileged(new SetAccessiblePrivilege(field)); return (InvocationHandler) field.get(proxy); } catch (NoSuchFieldException e) { throw new RuntimeException("Could not find invocation handler on generated proxy", e); } catch (IllegalArgumentException e) { throw new RuntimeException("Object is not a proxy of correct type", e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } }