public FallbackMethod find(Class<?> enclosingType, Method commandMethod, boolean extended) { if (canHandle(enclosingType, commandMethod)) { return doFind(enclosingType, commandMethod, extended); } else if (next != null) { return next.find(enclosingType, commandMethod, extended); } else { return FallbackMethod.ABSENT; } }
public static boolean isCompileWeaving() { return WeavingMode.COMPILE == getWeavingMode(); } }
static Result success() { return new Result(true); }
private static MetaHolder.Builder setFallbackMethod(MetaHolder.Builder builder, Class<?> declaringClass, Method commandMethod) { FallbackMethod fallbackMethod = MethodProvider.getInstance().getFallbackMethod(declaringClass, commandMethod); if (fallbackMethod.isPresent()) { fallbackMethod.validateReturnType(commandMethod); builder .fallbackMethod(fallbackMethod.getMethod()) .fallbackExecutionType(ExecutionType.getExecutionType(fallbackMethod.getMethod().getReturnType())); } return builder; }
public void testGetFallbackForExtendedCommandV2() throws NoSuchMethodException { // given Method extFallback = Service.class.getDeclaredMethod("extCommandV2", String.class, Integer.class, Throwable.class); // when Method fallback = MethodProvider.getInstance().getFallbackMethod(Service.class, extFallback, true).getMethod(); // then assertParamsTypes(fallback, String.class, Integer.class); }
private static Result equals(Type[] t1, Type[] t2) { if (t1 == null && t2 == null) return SUCCESS; if (t1 == null) return Result.failure(); if (t2 == null) return Result.failure(); if (t1.length != t2.length) return Result.failure(new Error(String.format("Different size of type literals. Command size = %d, fallback size = %d", t1.length, t2.length))); Result result = SUCCESS; for (int i = 0; i < t1.length; i++) { result = result.combine(equals(t1[i], t2[i])); if (result.isFailure()) return result; } return result; }
private Result equalsParametrizedTypes(List<Type> commandParametrizedTypes, List<Type> fallbackParametrizedTypes) { if (commandParametrizedTypes.size() != fallbackParametrizedTypes.size()) { return Result.failure(Collections.singletonList( new Error("Different size of types variables.\n" + "Command type literals size = " + commandParametrizedTypes.size() + ": " + commandParametrizedTypes + "\n" + "Fallback type literals size = " + fallbackParametrizedTypes.size() + ": " + fallbackParametrizedTypes + "\n" ))); } for (int i = 0; i < commandParametrizedTypes.size(); i++) { Type commandParametrizedType = commandParametrizedTypes.get(i); Type fallbackParametrizedType = fallbackParametrizedTypes.get(i); Result result = equals(commandParametrizedType, fallbackParametrizedType); if (!result.success) return result; } return SUCCESS; }
public FallbackMethod getFallbackMethod(Class<?> type, Method commandMethod) { return getFallbackMethod(type, commandMethod, false); }
private static int getParameterCount(String desc) { return parseParams(desc).length; }
@Override boolean canHandle(Class<?> enclosingType, Method commandMethod) { return StringUtils.isNotBlank(getFallbackName(enclosingType, commandMethod)); } }
/** * Check whether return type of the given method is parametrized or not. * * @param method the method * @return true - if return type is {@link ParameterizedType}, otherwise - false */ public static boolean isReturnTypeParametrized(Method method) { return isParametrizedType(method.getGenericReturnType()); }
@Override boolean canHandle(Class<?> enclosingType, Method commandMethod) { return StringUtils.isNotBlank(getFallbackName(enclosingType, commandMethod)); } }
private static String positionAsString(Type type, List<Type> types) { int pos = position(type, types); if (pos < 0) { return "unknown"; } return String.valueOf(pos); }
private static Result regularEquals(final Type commandType, final Type fallbackType) { return Result.of(Objects.equal(commandType, fallbackType), new Supplier<List<Error>>() { @Override public List<Error> get() { return Collections.singletonList(new Error( commandType, String.format("Different types. Command type: '%s'; fallback type: '%s'", commandType, fallbackType), fallbackType)); } }); }
public void testGetFallbackForExtendedCommandV2_extendedParameterFalse() throws NoSuchMethodException { // given Method extFallback = Service.class.getDeclaredMethod("extCommandV2", String.class, Integer.class, Throwable.class); // when Method fallback = MethodProvider.getInstance().getFallbackMethod(Service.class, extFallback, false).getMethod(); // then assertNull(fallback); }
static Result failure(List<Error> errors) { return new Result(false, errors); }
@Test public void testGetExtendedFallback() throws NoSuchMethodException { // given Method command = Service.class.getDeclaredMethod("command", String.class, Integer.class); // when Method extFallback = MethodProvider.getInstance().getFallbackMethod(Service.class, command).getMethod(); // then assertParamsTypes(extFallback, String.class, Integer.class, Throwable.class); }
static Result failure() { return new Result(false); }