Tabnine Logo
Reflects
Code IndexAdd Tabnine to your IDE (free)

How to use
Reflects
in
org.jupiter.common.util

Best Java code snippets using org.jupiter.common.util.Reflects (Showing top 20 results out of 315)

origin: fengjiachun/Jupiter

/**
 * The shortcut to {@link #simpleClassName(Class) simpleClassName(o.getClass())}.
 */
public static String simpleClassName(Object o) {
  return o == null ? "null_object" : simpleClassName(o.getClass());
}
origin: fengjiachun/Jupiter

/**
 * Find an array of parameter {@link Type}s that matches the given compatible parameters.
 */
public static Class<?>[] findMatchingParameterTypes(List<Class<?>[]> parameterTypesList, Object[] args) {
  if (parameterTypesList.size() == 1) {
    return parameterTypesList.get(0);
  }
  // 获取参数类型
  Class<?>[] parameterTypes;
  if (args == null || args.length == 0) {
    parameterTypes = new Class[0];
  } else {
    parameterTypes = new Class[args.length];
    for (int i = 0; i < args.length; i++) {
      if (args[i] != null) {
        parameterTypes[i] = args[i].getClass();
      }
    }
  }
  Class<?>[] bestMatch = null;
  for (Class<?>[] pTypes : parameterTypesList) {
    if (isAssignable(parameterTypes, pTypes, true)) {
      if (bestMatch == null
          || compareParameterTypes(pTypes, bestMatch, parameterTypes) < 0) {
        bestMatch = pTypes;
      }
    }
  }
  return bestMatch;
}
origin: fengjiachun/Jupiter

  private static RegistryServer newInstance(Object... parameters) {
    if (defaultRegistryClass == null || allConstructorsParameterTypes == null) {
      throw new UnsupportedOperationException("Unsupported default registry");
    }
    // 根据JLS方法调用的静态分派规则查找最匹配的方法parameterTypes
    Class<?>[] parameterTypes = Reflects.findMatchingParameterTypes(allConstructorsParameterTypes, parameters);
    if (parameterTypes == null) {
      throw new IllegalArgumentException("Parameter types");
    }
    try {
      @SuppressWarnings("JavaReflectionMemberAccess")
      Constructor<RegistryServer> c = defaultRegistryClass.getConstructor(parameterTypes);
      c.setAccessible(true);
      return c.newInstance(parameters);
    } catch (Exception e) {
      ThrowUtil.throwException(e);
    }
    return null; // should never get here
  }
}
origin: fengjiachun/Jupiter

/**
 * Sets new value by name, on the specified {@code Class}. The new value is
 * automatically unwrapped if the underlying field has
 * a primitive type.
 *
 * @param clazz the specified class
 * @param name  the name of the the field in class
 * @param value the new value for the field in class
 */
public static void setStaticValue(Class<?> clazz, String name, Object value) {
  try {
    Field fd = setAccessible(getField(clazz, name));
    fd.set(null, value);
  } catch (Exception e) {
    ThrowUtil.throwException(e);
  }
}
origin: fengjiachun/Jupiter

return getPrimitivePromotionCost(srcClass, dstClass);
if (dstClass.isInterface() && isAssignable(srcClass, dstClass, true)) {
origin: fengjiachun/Jupiter

private static Object invoke(MessageWrapper msg, Context invokeCtx) throws Signal {
  ServiceWrapper service = invokeCtx.getService();
  Object provider = service.getServiceProvider();
  String methodName = msg.getMethodName();
  Object[] args = msg.getArgs();
  Timer.Context timerCtx = null;
  if (METRIC_NEEDED) {
    timerCtx = Metrics.timer(msg.getOperationName()).time();
  }
  Class<?>[] expectCauseTypes = null;
  try {
    List<Pair<Class<?>[], Class<?>[]>> methodExtension = service.getMethodExtension(methodName);
    if (methodExtension == null) {
      throw new NoSuchMethodException(methodName);
    }
    // 根据JLS方法调用的静态分派规则查找最匹配的方法parameterTypes
    Pair<Class<?>[], Class<?>[]> bestMatch = Reflects.findMatchingParameterTypesExt(methodExtension, args);
    Class<?>[] parameterTypes = bestMatch.getFirst();
    expectCauseTypes = bestMatch.getSecond();
    return Reflects.fastInvoke(provider, methodName, parameterTypes, args);
  } catch (Throwable t) {
    invokeCtx.setCauseAndExpectTypes(t, expectCauseTypes);
    throw INVOKE_ERROR;
  } finally {
    if (METRIC_NEEDED) {
      timerCtx.stop();
    }
  }
}
origin: fengjiachun/Jupiter

  @RuntimeType
  public Object invoke(@Origin Method method, @AllArguments @RuntimeType Object[] args) throws Throwable {
    Class<?> returnType = method.getReturnType();

    Object result = doInvoke(method.getName(), args, returnType, false);

    InvokeFutureContext.set((InvokeFuture<?>) result);

    return Reflects.getTypeDefaultValue(returnType);
  }
}
origin: fengjiachun/Jupiter

/**
 * Compares the relative fitness of two sets of parameter types in terms of
 * matching a third set of runtime parameter types, such that a list ordered
 * by the results of the comparison would return the best match first
 * (least).
 *
 * @param left   the "left" parameter set
 * @param right  the "right" parameter set
 * @param actual the runtime parameter types to match against
 *               {@code left}/{@code right}
 * @return int consistent with {@code compare} semantics
 */
private static int compareParameterTypes(Class<?>[] left, Class<?>[] right, Class<?>[] actual) {
  final float leftCost = getTotalTransformationCost(actual, left);
  final float rightCost = getTotalTransformationCost(actual, right);
  return Float.compare(leftCost, rightCost);
}
origin: fengjiachun/Jupiter

/**
 * Returns the sum of the object transformation cost for each class in the
 * source argument list.
 *
 * @param srcArgs the source arguments
 * @param dstArgs the destination arguments
 * @return the total transformation cost
 */
private static float getTotalTransformationCost(final Class<?>[] srcArgs, final Class<?>[] dstArgs) {
  float totalCost = 0.0f;
  for (int i = 0; i < srcArgs.length; i++) {
    Class<?> srcClass, dstClass;
    srcClass = srcArgs[i];
    dstClass = dstArgs[i];
    totalCost += getObjectTransformationCost(srcClass, dstClass);
  }
  return totalCost;
}
origin: fengjiachun/Jupiter

/**
 * Sets new value by name, on the specified {@code Class}. The new value is
 * automatically unwrapped if the underlying field has
 * a primitive type.
 *
 * @param clazz the specified class
 * @param name  the name of the the field in class
 * @param value the new value for the field in class
 */
public static void setStaticValue(Class<?> clazz, String name, Object value) {
  try {
    Field fd = setAccessible(getField(clazz, name));
    fd.set(null, value);
  } catch (Exception e) {
    ThrowUtil.throwException(e);
  }
}
origin: fengjiachun/Jupiter

private static Object invoke(MessageWrapper msg, Context invokeCtx) throws Signal {
  ServiceWrapper service = invokeCtx.getService();
  Object provider = service.getServiceProvider();
  String methodName = msg.getMethodName();
  Object[] args = msg.getArgs();
  Timer.Context timerCtx = null;
  if (METRIC_NEEDED) {
    timerCtx = Metrics.timer(msg.getOperationName()).time();
  }
  Class<?>[] expectCauseTypes = null;
  try {
    List<Pair<Class<?>[], Class<?>[]>> methodExtension = service.getMethodExtension(methodName);
    if (methodExtension == null) {
      throw new NoSuchMethodException(methodName);
    }
    // 根据JLS方法调用的静态分派规则查找最匹配的方法parameterTypes
    Pair<Class<?>[], Class<?>[]> bestMatch = Reflects.findMatchingParameterTypesExt(methodExtension, args);
    Class<?>[] parameterTypes = bestMatch.getFirst();
    expectCauseTypes = bestMatch.getSecond();
    return Reflects.fastInvoke(provider, methodName, parameterTypes, args);
  } catch (Throwable t) {
    invokeCtx.setCauseAndExpectTypes(t, expectCauseTypes);
    throw INVOKE_ERROR;
  } finally {
    if (METRIC_NEEDED) {
      timerCtx.stop();
    }
  }
}
origin: fengjiachun/Jupiter

return getPrimitivePromotionCost(srcClass, dstClass);
if (dstClass.isInterface() && isAssignable(srcClass, dstClass, true)) {
origin: fengjiachun/Jupiter

  @RuntimeType
  public Object invoke(@Origin Method method, @AllArguments @RuntimeType Object[] args) throws Throwable {
    Class<?> returnType = method.getReturnType();

    Object result = doInvoke(method.getName(), args, returnType, false);

    InvokeFutureContext.set((InvokeFuture<?>) result);

    return Reflects.getTypeDefaultValue(returnType);
  }
}
origin: fengjiachun/Jupiter

/**
 * Compares the relative fitness of two sets of parameter types in terms of
 * matching a third set of runtime parameter types, such that a list ordered
 * by the results of the comparison would return the best match first
 * (least).
 *
 * @param left   the "left" parameter set
 * @param right  the "right" parameter set
 * @param actual the runtime parameter types to match against
 *               {@code left}/{@code right}
 * @return int consistent with {@code compare} semantics
 */
private static int compareParameterTypes(Class<?>[] left, Class<?>[] right, Class<?>[] actual) {
  final float leftCost = getTotalTransformationCost(actual, left);
  final float rightCost = getTotalTransformationCost(actual, right);
  return Float.compare(leftCost, rightCost);
}
origin: fengjiachun/Jupiter

/**
 * Returns the sum of the object transformation cost for each class in the
 * source argument list.
 *
 * @param srcArgs the source arguments
 * @param dstArgs the destination arguments
 * @return the total transformation cost
 */
private static float getTotalTransformationCost(final Class<?>[] srcArgs, final Class<?>[] dstArgs) {
  float totalCost = 0.0f;
  for (int i = 0; i < srcArgs.length; i++) {
    Class<?> srcClass, dstClass;
    srcClass = srcArgs[i];
    dstClass = dstArgs[i];
    totalCost += getObjectTransformationCost(srcClass, dstClass);
  }
  return totalCost;
}
origin: fengjiachun/Jupiter

/**
 * The shortcut to {@link #simpleClassName(Class) simpleClassName(o.getClass())}.
 */
public static String simpleClassName(Object o) {
  return o == null ? "null_object" : simpleClassName(o.getClass());
}
origin: fengjiachun/Jupiter

/**
 * Sets new value by name, on the specified object. The new value
 * is automatically unwrapped if the underlying field has a primitive type.
 *
 * @param o     the specified object
 * @param name  the name of the the field in object
 * @param value the new value for the field in object
 */
public static void setValue(Object o, String name, Object value) {
  try {
    Field fd = setAccessible(getField(o.getClass(), name));
    fd.set(o, value);
  } catch (Exception e) {
    ThrowUtil.throwException(e);
  }
}
origin: fengjiachun/Jupiter

/**
 * Find an array of parameter {@link Type}s that matches the given compatible parameters.
 */
public static Class<?>[] findMatchingParameterTypes(List<Class<?>[]> parameterTypesList, Object[] args) {
  if (parameterTypesList.size() == 1) {
    return parameterTypesList.get(0);
  }
  // 获取参数类型
  Class<?>[] parameterTypes;
  if (args == null || args.length == 0) {
    parameterTypes = new Class[0];
  } else {
    parameterTypes = new Class[args.length];
    for (int i = 0; i < args.length; i++) {
      if (args[i] != null) {
        parameterTypes[i] = args[i].getClass();
      }
    }
  }
  Class<?>[] bestMatch = null;
  for (Class<?>[] pTypes : parameterTypesList) {
    if (isAssignable(parameterTypes, pTypes, true)) {
      if (bestMatch == null
          || compareParameterTypes(pTypes, bestMatch, parameterTypes) < 0) {
        bestMatch = pTypes;
      }
    }
  }
  return bestMatch;
}
origin: org.jupiter-rpc/jupiter-rpc

private static Object invoke(MessageWrapper msg, Context invokeCtx) throws Signal {
  ServiceWrapper service = invokeCtx.getService();
  Object provider = service.getServiceProvider();
  String methodName = msg.getMethodName();
  Object[] args = msg.getArgs();
  Timer.Context timerCtx = null;
  if (METRIC_NEEDED) {
    timerCtx = Metrics.timer(msg.getOperationName()).time();
  }
  Class<?>[] expectCauseTypes = null;
  try {
    List<Pair<Class<?>[], Class<?>[]>> methodExtension = service.getMethodExtension(methodName);
    if (methodExtension == null) {
      throw new NoSuchMethodException(methodName);
    }
    // 根据JLS方法调用的静态分派规则查找最匹配的方法parameterTypes
    Pair<Class<?>[], Class<?>[]> bestMatch = Reflects.findMatchingParameterTypesExt(methodExtension, args);
    Class<?>[] parameterTypes = bestMatch.getFirst();
    expectCauseTypes = bestMatch.getSecond();
    return Reflects.fastInvoke(provider, methodName, parameterTypes, args);
  } catch (Throwable t) {
    invokeCtx.setCauseAndExpectTypes(t, expectCauseTypes);
    throw INVOKE_ERROR;
  } finally {
    if (METRIC_NEEDED) {
      timerCtx.stop();
    }
  }
}
origin: org.jupiter-rpc/jupiter-common

return getPrimitivePromotionCost(srcClass, dstClass);
if (dstClass.isInterface() && isAssignable(srcClass, dstClass, true)) {
org.jupiter.common.utilReflects

Javadoc

Static utility methods pertaining to reflection. jupiter org.jupiter.common.util

Most used methods

  • simpleClassName
    The shortcut to #simpleClassName(Class).
  • compareParameterTypes
    Compares the relative fitness of two sets of parameter types in terms of matching a third set of run
  • fastInvoke
    Invokes the underlying method, fast invoke using ASM.
  • findMatchingParameterTypes
    Find an array of parameter Types that matches the given compatible parameters.
  • findMatchingParameterTypesExt
    Find an array of parameter Types that matches the given compatible parameters.
  • getField
    Returns a Field object that reflects the specified declared field of the Class or interface represen
  • getObjectTransformationCost
    Gets the number of steps required needed to turn the source class into the destination class. This r
  • getPrimitivePromotionCost
    Gets the number of steps required to promote a primitive number to another type.
  • getTotalTransformationCost
    Returns the sum of the object transformation cost for each class in the source argument list.
  • getTypeDefaultValue
  • isAssignable
    Checks if an array of Classes can be assigned to another array of Classes.
  • primitiveToWrapper
    Converts the specified primitive Class object to its corresponding wrapper Class object.
  • isAssignable,
  • primitiveToWrapper,
  • setAccessible,
  • wrapperToPrimitive,
  • getStaticValue

Popular in Java

  • Parsing JSON documents to java classes using gson
  • requestLocationUpdates (LocationManager)
  • getSupportFragmentManager (FragmentActivity)
  • addToBackStack (FragmentTransaction)
  • Point (java.awt)
    A point representing a location in (x,y) coordinate space, specified in integer precision.
  • Rectangle (java.awt)
    A Rectangle specifies an area in a coordinate space that is enclosed by the Rectangle object's top-
  • SecureRandom (java.security)
    This class generates cryptographically secure pseudo-random numbers. It is best to invoke SecureRand
  • StringTokenizer (java.util)
    Breaks a string into tokens; new code should probably use String#split.> // Legacy code: StringTo
  • ConcurrentHashMap (java.util.concurrent)
    A plug-in replacement for JDK1.5 java.util.concurrent.ConcurrentHashMap. This version is based on or
  • Base64 (org.apache.commons.codec.binary)
    Provides Base64 encoding and decoding as defined by RFC 2045.This class implements section 6.8. Base
  • Top Sublime Text plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now