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

org.mvnsearch.spring.boot.nats.client.DefaultMethodHandler Maven / Gradle / Ivy

Go to download

Spring Boot starter NATS with publish/subscribe, KeyValue, Object Store, Durable Component, Services framework etc. support.

There is a newer version: 0.2.1
Show newest version
package org.mvnsearch.spring.boot.nats.client;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * Default Method Handler
 * 
 * if (method.isDefault()) {
 * return DefaultMethodHandler.getMethodHandle(method, serviceInterface).bindTo(proxy).invokeWithArguments(allArguments);
 * }
 * 
 *
 * @author linux_china
 */
public class DefaultMethodHandler {
  /**
   * default method handles
   */
  private static final Map methodHandles = new HashMap<>();
  private static boolean isJdk8 = false;

  static {
    String version = System.getProperty("java.version");
    if (version.startsWith("1.8.")) {
      isJdk8 = true;
    }
  }

  public static MethodHandle getMethodHandle(Method method, Class serviceInterface) throws Exception {
    MethodHandle methodHandle = methodHandles.get(method);
    if (methodHandle == null) {
      if (isJdk8) {
        Constructor lookupConstructor = MethodHandles.Lookup.class.getDeclaredConstructor(Class.class, Integer.TYPE);
        if (!lookupConstructor.isAccessible()) {
          lookupConstructor.setAccessible(true);
        }
        methodHandle = lookupConstructor.newInstance(method.getDeclaringClass(), MethodHandles.Lookup.PRIVATE)
          .unreflectSpecial(method, method.getDeclaringClass());
      } else {
        methodHandle = MethodHandles.lookup().findSpecial(
          method.getDeclaringClass(),
          method.getName(),
          MethodType.methodType(method.getReturnType(), method.getParameterTypes()),
          serviceInterface);
      }
      methodHandles.put(method, methodHandle);
    }
    return methodHandle;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy