com.xiaomi.infra.galaxy.talos.client.TalosClient Maven / Gradle / Ivy
/**
* Copyright 2015, Xiaomi.
* All rights reserved.
* Author: [email protected]
*/
package com.xiaomi.infra.galaxy.talos.client;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class TalosClient {
private static class TalosClientHandler implements InvocationHandler {
private final Object instance;
public TalosClientHandler(Class interfaceClass, Object instance) {
this.instance = instance;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
try {
return method.invoke(instance, args);
} catch (InvocationTargetException e) {
throw e.getCause();
}
}
}
@SuppressWarnings("unchecked")
public static T getClient(Class interfaceClass, Object instance) {
return (T) Proxy.newProxyInstance(TalosClient.class.getClassLoader(),
new Class[]{ interfaceClass },
new TalosClientHandler(interfaceClass, instance));
}
}