/** * The shortcut to {@link #simpleClassName(Class) simpleClassName(o.getClass())}. */ public static String simpleClassName(Object o) { return o == null ? "null_object" : simpleClassName(o.getClass()); }
/** * 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; }
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 } }
/** * 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); } }
return getPrimitivePromotionCost(srcClass, dstClass); if (dstClass.isInterface() && isAssignable(srcClass, dstClass, true)) {
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(); } } }
@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); } }
/** * 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); }
/** * 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; }
/** * 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); } }
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(); } } }
return getPrimitivePromotionCost(srcClass, dstClass); if (dstClass.isInterface() && isAssignable(srcClass, dstClass, true)) {
@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); } }
/** * 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); }
/** * 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; }
/** * The shortcut to {@link #simpleClassName(Class) simpleClassName(o.getClass())}. */ public static String simpleClassName(Object o) { return o == null ? "null_object" : simpleClassName(o.getClass()); }
/** * 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); } }
/** * 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; }
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(); } } }
return getPrimitivePromotionCost(srcClass, dstClass); if (dstClass.isInterface() && isAssignable(srcClass, dstClass, true)) {