All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vertx.ext.unit.impl.Helper Maven / Gradle / Ivy

There is a newer version: 5.0.0.CR3
Show newest version
package io.vertx.ext.unit.impl;

import io.vertx.core.Handler;
import io.vertx.ext.unit.TestContext;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.function.Supplier;

/**
 * @author Julien Viet
 */
public class Helper {

  /**
   * JVM hack to throw a throwable as unchecked.
   *
   * @param throwable the throwable to throw
   */
  public static void uncheckedThrow(Throwable throwable) {
    Helper.throwIt(throwable);
  }

  private static  void throwIt(Throwable throwable) throws T {
    throw (T)throwable;
  }

  public static Handler invoker(Method method, Supplier instance) {
    Class[] paramTypes = method.getParameterTypes();
    if (!(paramTypes.length == 0 || (paramTypes.length == 1 && paramTypes[0].equals(TestContext.class)))) {
      throw new IllegalArgumentException("Incorrect method handler mapping " + method);
    }
    return context -> {
      try {
        Object o = instance.get();
        if (paramTypes.length == 0) {
          method.invoke(o);
        } else {
          method.invoke(o, context);
        }
      } catch (IllegalAccessException e) {
        Helper.uncheckedThrow(e);
      } catch (InvocationTargetException e) {
        Helper.uncheckedThrow(e.getCause());
      }
    };
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy