com.xiaomi.infra.galaxy.emq.client.EMQClient Maven / Gradle / Ivy
The newest version!
package com.xiaomi.infra.galaxy.emq.client;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
/**
* Copyright 2015, Xiaomi.
* All rights reserved.
* Author: [email protected]
*/
public class EMQClient {
private static class EMQClientHandler implements InvocationHandler {
private final Object instance;
public EMQClientHandler(Class interfaceClass, Object instance) {
this.instance = instance;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if (!Arrays.asList(Object.class.getMethods()).contains(method)) {
EMQRequestCheckUtils.checkRequest(args);
}
try {
return method.invoke(instance, args);
} catch (InvocationTargetException e) {
throw e.getCause();
}
}
}
/**
* Create client wrapper which validate parameters of client.
*/
@SuppressWarnings("unchecked")
public static T getClient(Class interfaceClass, Object instance) {
return (T) Proxy.newProxyInstance(EMQClient.class.getClassLoader(),
new Class[]{interfaceClass},
new EMQClientHandler(interfaceClass, instance));
}
}