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); }
@Test public void testGetMethodFoo(){ Optional<Method> method = MethodProvider.getInstance().getMethod(C.class, methodName, parametersTypes); assertTrue(method.isPresent()); assertEquals(methodName, method.get().getName()); }
/** * Gets declared method from specified type by mame and parameters types. * * @param type the type * @param methodName the name of the method * @param parameterTypes the parameter array * @return a {@link Method} object or null if method doesn't exist */ public static Method getDeclaredMethod(Class<?> type, String methodName, Class<?>... parameterTypes) { Method method = null; try { method = type.getDeclaredMethod(methodName, parameterTypes); if(method.isBridge()){ method = MethodProvider.getInstance().unbride(method, type); } } catch (NoSuchMethodException e) { Class<?> superclass = type.getSuperclass(); if (superclass != null) { method = getDeclaredMethod(superclass, methodName, parameterTypes); } } catch (ClassNotFoundException e) { Throwables.propagate(e); } catch (IOException e) { Throwables.propagate(e); } return method; }
/** * Gets method by name and parameters types using reflection, * if the given type doesn't contain required method then continue applying this method for all super classes up to Object class. * * @param type the type to search method * @param name the method name * @param parameterTypes the parameters types * @return Some if method exists otherwise None */ public static Optional<Method> getMethod(Class<?> type, String name, Class<?>... parameterTypes) { Method[] methods = type.getDeclaredMethods(); for (Method method : methods) { if (method.getName().equals(name) && Arrays.equals(method.getParameterTypes(), parameterTypes)) { return Optional.of(method); } } Class<?> superClass = type.getSuperclass(); if (superClass != null && !superClass.equals(Object.class)) { return getMethod(superClass, name, parameterTypes); } else { return Optional.absent(); } }
public FallbackMethod getFallbackMethod(Class<?> type, Method commandMethod) { return getFallbackMethod(type, commandMethod, false); }
private static int getParameterCount(String desc) { return parseParams(desc).length; }
private FallbackMethod doFind(Class<?> enclosingType, Method commandMethod, boolean extended) { String name = getFallbackName(enclosingType, commandMethod); Class<?>[] fallbackParameterTypes = null; if (isDefault()) { fallbackParameterTypes = new Class[0]; } else { fallbackParameterTypes = commandMethod.getParameterTypes(); } if (extended && fallbackParameterTypes[fallbackParameterTypes.length - 1] == Throwable.class) { fallbackParameterTypes = ArrayUtils.remove(fallbackParameterTypes, fallbackParameterTypes.length - 1); } Class<?>[] extendedFallbackParameterTypes = Arrays.copyOf(fallbackParameterTypes, fallbackParameterTypes.length + 1); extendedFallbackParameterTypes[fallbackParameterTypes.length] = Throwable.class; Optional<Method> exFallbackMethod = getMethod(enclosingType, name, extendedFallbackParameterTypes); Optional<Method> fMethod = getMethod(enclosingType, name, fallbackParameterTypes); Method method = exFallbackMethod.or(fMethod).orNull(); if (method == null) { throw new FallbackDefinitionException("fallback method wasn't found: " + name + "(" + Arrays.toString(fallbackParameterTypes) + ")"); } return new FallbackMethod(method, exFallbackMethod.isPresent(), isDefault()); }
public FallbackMethod getFallbackMethod(Class<?> type, Method commandMethod) { return getFallbackMethod(type, commandMethod, false); }
private static int getParameterCount(String desc) { return parseParams(desc).length; }
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); }
@Test public void testUnbridgeFoo() throws NoSuchMethodException, IOException, ClassNotFoundException { // given Method bridgeMethod = getBridgeMethod(GenericInterfaceImpl.class, "foo"); assertNotNull(bridgeMethod); // when Method genMethod = MethodProvider.getInstance().unbride(bridgeMethod, GenericInterfaceImpl.class); // then assertNotNull(bridgeMethod); assertReturnType(Child.class, genMethod); assertParamsTypes(genMethod, Child.class); }
/** * Gets method by name and parameters types using reflection, * if the given type doesn't contain required method then continue applying this method for all super classes up to Object class. * * @param type the type to search method * @param name the method name * @param parameterTypes the parameters types * @return Some if method exists otherwise None */ public static Optional<Method> getMethod(Class<?> type, String name, Class<?>... parameterTypes) { Method[] methods = type.getDeclaredMethods(); for (Method method : methods) { if (method.getName().equals(name) && Arrays.equals(method.getParameterTypes(), parameterTypes)) { return Optional.of(method); } } Class<?> superClass = type.getSuperclass(); if (superClass != null && !superClass.equals(Object.class)) { return getMethod(superClass, name, parameterTypes); } else { return Optional.absent(); } }
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; }
/** * Gets declared method from specified type by mame and parameters types. * * @param type the type * @param methodName the name of the method * @param parameterTypes the parameter array * @return a {@link Method} object or null if method doesn't exist */ public static Method getDeclaredMethod(Class<?> type, String methodName, Class<?>... parameterTypes) { Method method = null; try { method = type.getDeclaredMethod(methodName, parameterTypes); if(method.isBridge()){ method = MethodProvider.getInstance().unbride(method, type); } } catch (NoSuchMethodException e) { Class<?> superclass = type.getSuperclass(); if (superclass != null) { method = getDeclaredMethod(superclass, methodName, parameterTypes); } } catch (ClassNotFoundException e) { Throwables.propagate(e); } catch (IOException e) { Throwables.propagate(e); } return method; }
private FallbackMethod doFind(Class<?> enclosingType, Method commandMethod, boolean extended) { String name = getFallbackName(enclosingType, commandMethod); Class<?>[] fallbackParameterTypes = null; if (isDefault()) { fallbackParameterTypes = new Class[0]; } else { fallbackParameterTypes = commandMethod.getParameterTypes(); } if (extended && fallbackParameterTypes[fallbackParameterTypes.length - 1] == Throwable.class) { fallbackParameterTypes = ArrayUtils.remove(fallbackParameterTypes, fallbackParameterTypes.length - 1); } Class<?>[] extendedFallbackParameterTypes = Arrays.copyOf(fallbackParameterTypes, fallbackParameterTypes.length + 1); extendedFallbackParameterTypes[fallbackParameterTypes.length] = Throwable.class; Optional<Method> exFallbackMethod = getMethod(enclosingType, name, extendedFallbackParameterTypes); Optional<Method> fMethod = getMethod(enclosingType, name, fallbackParameterTypes); Method method = exFallbackMethod.or(fMethod).orNull(); if (method == null) { throw new FallbackDefinitionException("fallback method wasn't found: " + name + "(" + Arrays.toString(fallbackParameterTypes) + ")"); } return new FallbackMethod(method, exFallbackMethod.isPresent(), isDefault()); }
@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); }
builder.executionType(ExecutionType.getExecutionType(batchReturnType)); builder.observable(observable); FallbackMethod fallbackMethod = MethodProvider.getInstance().getFallbackMethod(obj.getClass(), batchCommandMethod); if (fallbackMethod.isPresent()) { fallbackMethod.validateReturnType(batchCommandMethod);
@Test @DataProvider({"true", "false"}) public void testGetFallbackForExtendedCommand(boolean extended) throws NoSuchMethodException { // given Method extFallback = Service.class.getDeclaredMethod("extCommand", String.class, Integer.class, Throwable.class); // when Method fallback = MethodProvider.getInstance().getFallbackMethod(Service.class, extFallback, extended).getMethod(); // then assertParamsTypes(fallback, String.class, Integer.class, Throwable.class); }
private CommandAction createFallbackAction(MetaHolder metaHolder) { FallbackMethod fallbackMethod = MethodProvider.getInstance().getFallbackMethod(metaHolder.getObj().getClass(), metaHolder.getMethod(), metaHolder.isExtendedFallback()); fallbackMethod.validateReturnType(metaHolder.getMethod());
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; }