mmarquee.automation.utils.Canalizer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ui-automation Show documentation
Show all versions of ui-automation Show documentation
A Java library that wraps the MS UIAutomation library.
package mmarquee.automation.utils;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
* Wraps an interface instance in a way that all calls to interface methods originate from the same thread.
*
* @author Pascal Bihler
*
*/
public class Canalizer {
/** The thread where all calls originate from. */
static final ExecutorService executor = Executors.newSingleThreadExecutor();
/**
* Shutdown the ExecutorService nicely.
*/
public static void shutdown() {
executor.shutdown();
}
/**
* Wraps an interface instance in a way that all calls to interface methods originate from the same thread.
*
* @param The type of the Object to canalize
* @param plainInstance The instance to canalize
* @return the canalized instance
*/
@SuppressWarnings("unchecked")
public static T canalize(final T plainInstance) {
final CanalizerInvocationHandler invocationHandler = new CanalizerInvocationHandler(executor,plainInstance);
return (T) java.lang.reflect.Proxy
.newProxyInstance(plainInstance.getClass().getClassLoader(),
getInterfaces(plainInstance),
invocationHandler);
}
private static Class>[] getInterfaces(final Object target) {
Class> base = target.getClass();
final Set> interfaces = new HashSet<>();
if (base.isInterface()) {
interfaces.add(base);
}
while (base != null && !Object.class.equals(base)) {
interfaces.addAll(Arrays.asList(base.getInterfaces()));
base = base.getSuperclass();
}
return interfaces.toArray(new Class[0]);
}
static class CanalizerInvocationHandler implements InvocationHandler {
private ExecutorService executor;
private Object underlying;
public CanalizerInvocationHandler(final ExecutorService executor, final Object underlying) {
this.executor = executor;
this.underlying = underlying;
}
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable
{
final Callable