/** * Delegates to {@link #invoke(Object, Method, Object[])} to serve as {@link MethodInterceptor}. */ @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { return invoke(obj, method, args); }
/** * Returns invocation handler which wraps the given target instance. * * @param target the target of invocation * @return invocation handler which wraps the given target instance */ public static GrapheneContextualHandler forTarget(GrapheneContext context, Object target) { GrapheneContextualHandler handler = new GrapheneContextualHandler(context, target); return handler; }
protected <T> T createWrapper(GrapheneContext grapheneContext, final Class<T> type, final WebElement element) throws Exception { T wrapper = GrapheneProxy.getProxyForHandler( GrapheneContextualHandler.forFuture(grapheneContext, new GrapheneProxy.FutureTarget() { @Override public Object getTarget() { try { return instantiate(type, element); } catch (Exception e) { throw new IllegalStateException("Can't instantiate the " + type, e); } } }), type); return wrapper; }
/** * <p> * Wraps the given target instance in the proxy. * </p> * * <p> * The list of interfaces which should be implemented by the proxy needs to be provided. * </p> * * * @param target the target instance to be wrapped * @param interfaces the list of interfaces which should be implemented by created proxy * @return the proxy wrapping the target */ @SuppressWarnings("unchecked") public static <T> T getProxyForTargetWithInterfaces(GrapheneContext context, T target, Class<?>... interfaces) { GrapheneProxyHandler handler = GrapheneContextualHandler.forTarget(context, target); return (T) createProxy(handler, null, interfaces); }
private WebElement getProxified() { // right now browser.switchTo().activeElement() is an example of a proxified but not wrapped element // we won't rely on that though, because it could easily change, so instead we'll use the proxifier directly (and in // such a way so as to benefit from the fix relevant to the test) GrapheneContextualHandler handler = GrapheneContextualHandler.forTarget(GrapheneContext.lastContext(), browser); WebElement proxy; try { Method findElement = WebDriver.class.getMethod("findElement", By.class); proxy = (WebElement) handler.invoke(browser, findElement, new Object[] { By.id(TEST_ELEMENT_ID) }); } catch (Throwable e) { throw new RuntimeException(e); } // in case you need to reproduce the problem, create the proxy manually: // Class<?>[] interfaces = GrapheneProxyUtil.getInterfaces(getUnwrapped().getClass()); // WebElement proxy = GrapheneProxy.getProxyForTargetWithInterfaces(GrapheneContext.lastContext(), getUnwrapped(), // interfaces); if (!(proxy instanceof GrapheneProxyInstance) || proxy instanceof GrapheneElement) { // it's not right to use asserts here - not the point of the test // also a caller might use @Test(expected = AssertionError.class) throw new RuntimeException( "Not proxified correctly: " + Arrays.toString(GrapheneProxyUtil.getInterfaces(proxy.getClass()))); } return proxy; }
@Override public Object invoke() throws Throwable { Object result = invokeReal(getTarget(), method, args); if (result == null) { return null; if (isProxyable(method, args) && !(result instanceof GrapheneProxyInstance)) { Class<?>[] interfaces = GrapheneProxyUtil.getInterfaces(result.getClass());
@Override public Object answer(InvocationOnMock invocation) throws Throwable { Method method = invocation.getMethod(); if (!handler.isProxyable(method, invocation.getArguments())) { violations.add(method); } return null; }
method.setAccessible(true); Object target = getTarget(); if (target instanceof GrapheneProxyInstance) { return ((GrapheneProxyInstance) target).unwrap(); Object target = getTarget(); if (target instanceof GrapheneProxyInstance) { return ((GrapheneProxyInstance) target).unwrap(); clone = (GrapheneProxyInstance) GrapheneProxy.getProxyForTarget(context, getTarget()); for (Interceptor interceptor : getSortedInterceptorsByPrecedence()) { clone.registerInterceptor(interceptor); for (Interceptor interceptor : getSortedInterceptorsByPrecedence()) { invocationContext = new InvocationContextImpl(interceptor, invocationContext);
@Override public Object getTarget() { return GrapheneContextualHandler.this.getTarget(); }
/** * <p> * Wraps the given target instance in the proxy. * </p> * * <p> * The list of interfaces which should be implemented by the proxy needs to be provided. * </p> * * * @param target the target instance to be wrapped * @param interfaces the list of interfaces which should be implemented by created proxy * @return the proxy wrapping the target */ @SuppressWarnings("unchecked") public static <T> T getProxyForTargetWithInterfaces(GrapheneContext context, T target, Class<?>... interfaces) { GrapheneProxyHandler handler = GrapheneContextualHandler.forTarget(context, target); return (T) createProxy(handler, null, interfaces); }
@Override public Object invoke() throws Throwable { Object result = invokeReal(getTarget(), method, args); if (result == null) { return null; if (isProxyable(method, args) && !(result instanceof GrapheneProxyInstance)) { Class<?>[] interfaces = GrapheneProxyUtil.getInterfaces(result.getClass());
@Override public Object answer(InvocationOnMock invocation) throws Throwable { Method method = invocation.getMethod(); if (handler.isProxyable(method, invocation.getArguments())) { violations.add(method); } return null; }
method.setAccessible(true); Object target = getTarget(); if (target instanceof GrapheneProxyInstance) { return ((GrapheneProxyInstance) target).unwrap(); Object target = getTarget(); if (target instanceof GrapheneProxyInstance) { return ((GrapheneProxyInstance) target).unwrap(); clone = (GrapheneProxyInstance) GrapheneProxy.getProxyForTarget(context, getTarget()); for (Interceptor interceptor : getSortedInterceptorsByPrecedence()) { clone.registerInterceptor(interceptor); for (Interceptor interceptor : getSortedInterceptorsByPrecedence()) { invocationContext = new InvocationContextImpl(interceptor, invocationContext);
@Override public Object getTarget() { return GrapheneContextualHandler.this.getTarget(); }
/** * <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()); }
protected <T> T createWrapper(GrapheneContext grapheneContext, final Class<T> type, final WebElement element) throws Exception { T wrapper = GrapheneProxy.getProxyForHandler( GrapheneContextualHandler.forFuture(grapheneContext, new GrapheneProxy.FutureTarget() { @Override public Object getTarget() { try { return instantiate(type, element); } catch (Exception e) { throw new IllegalStateException("Can't instantiate the " + type, e); } } }), type); return wrapper; }
/** * Delegates to {@link #invoke(Object, Method, Object[])} to serve as {@link MethodInterceptor}. */ @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { return invoke(obj, method, args); }
/** * Returns invocation handler which wraps the target for future computation. * * @param future the future target * @return invocation handler which wraps the target for future computation */ public static GrapheneContextualHandler forFuture(GrapheneContext context, FutureTarget future) { GrapheneContextualHandler handler = new GrapheneContextualHandler(context, future); return handler; }
/** * <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()); }
@SuppressWarnings("unchecked") protected <T> List<T> createWrappers(GrapheneContext grapheneContext, final Class<T> type, final List<WebElement> elements) { List<T> wrapper = GrapheneProxy.getProxyForHandler( GrapheneContextualHandler.forFuture(grapheneContext, new GrapheneProxy.FutureTarget() { @Override public Object getTarget() { try { List<T> target = new ArrayList<T>(); for (WebElement element : elements) { target.add((T) instantiate(type, element)); } return target; } catch (Exception e) { throw new IllegalStateException("Can't instantiate the " + type, e); } } }), List.class); return wrapper; }