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;

/**
 * Copyright 2015, Xiaomi.
 * All rights reserved.
 * Author: shenyuannan@xiaomi.com
 */

public class EMQClient {
  private static class EMQClientHandler<T> implements InvocationHandler {
    private final Object instance;

    public EMQClientHandler(Class<T> interfaceClass, Object instance) {
      this.instance = instance;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
        throws Throwable {
      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> T getClient(Class<T> interfaceClass, Object instance) {
    return (T) Proxy.newProxyInstance(EMQClient.class.getClassLoader(),
        new Class[]{interfaceClass},
        new EMQClientHandler(interfaceClass, instance));
  }
}

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: shenyuannan@xiaomi.com
 */

public class EMQClient {
  private static class EMQClientHandler<T> implements InvocationHandler {
    private final Object instance;

    public EMQClientHandler(Class<T> 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> T getClient(Class<T> interfaceClass, Object instance) {
    return (T) Proxy.newProxyInstance(EMQClient.class.getClassLoader(),
        new Class[]{interfaceClass},
        new EMQClientHandler(interfaceClass, instance));
  }
}

Fix SDK check methods NPE.
Fix SDK check methods NPE.

Summary: Ref T9503

Test Plan: done

Reviewers: fenghonghua, durunnan, xiewenliang

Reviewed By: durunnan

Maniphest Tasks: T9503

Differential Revision: https://phabricator.d.xiaomi.net/D63687
