/** * <p> * Wraps the given target instance in the proxy. * </p> * * <p> * The list of interfaces which should be implemented by the proxy is automatically computer from provided instance. * </p> * * * @param target the target instance to be wrapped * @return the proxy wrapping the target */ @SuppressWarnings("unchecked") public static <T> T getProxyForTarget(GrapheneContext context, T target) { if (Modifier.isFinal(target.getClass().getModifiers())) { if (target.getClass().getInterfaces().length > 0) { return GrapheneProxy.getProxyForTargetWithInterfaces(context, target, target.getClass().getInterfaces()); } else { throw new IllegalStateException("Can't create a proxy for " + target.getClass() + ", it's final and id doesn't implement any interface."); } } GrapheneProxyHandler handler = GrapheneContextualHandler.forTarget(context, target); return (T) createProxy(handler, target.getClass()); }
/** * <p> * Wraps the given target instance in the proxy. * </p> * * <p> * The list of interfaces which should be implemented by the proxy is automatically computer from provided instance. * </p> * * * @param target the target instance to be wrapped * @return the proxy wrapping the target */ @SuppressWarnings("unchecked") public static <T> T getProxyForTarget(GrapheneContext context, T target) { if (Modifier.isFinal(target.getClass().getModifiers())) { if (target.getClass().getInterfaces().length > 0) { return GrapheneProxy.getProxyForTargetWithInterfaces(context, target, target.getClass().getInterfaces()); } else { throw new IllegalStateException("Can't create a proxy for " + target.getClass() + ", it's final and id doesn't implement any interface."); } } GrapheneProxyHandler handler = GrapheneContextualHandler.forTarget(context, target); return (T) createProxy(handler, target.getClass()); }
@Test public void test() throws Throwable { InterceptorBuilder builder = new InterceptorBuilder(); builder.interceptInvocation(WebDriver.class, interceptor1).findElement(Interceptors.any(By.class)); builder.interceptInvocation(WebDriver.class, interceptor2).findElement(Interceptors.any(By.class)); Interceptor builtInterceptor = builder.build(); WebDriver driverProxy = GrapheneProxy.getProxyForTargetWithInterfaces(context, driver, WebDriver.class); GrapheneProxyInstance proxy = (GrapheneProxyInstance) driverProxy; proxy.registerInterceptor(builtInterceptor); driverProxy.findElement(by); Mockito.inOrder(interceptor1, interceptor2); verify(interceptor1).intercept(Mockito.any(InvocationContext.class)); verify(interceptor2).intercept(Mockito.any(InvocationContext.class)); } }
Object newProxy = GrapheneProxy.getProxyForTargetWithInterfaces(context, result, interfaces);
Object newProxy = GrapheneProxy.getProxyForTargetWithInterfaces(context, result, interfaces);