/** * Performs the actual write to the repository. This can be overridden by * a subclass if necessary. * * @param items the list of items to be persisted. * * @throws Exception thrown if error occurs during writing. */ protected void doWrite(List<? extends T> items) throws Exception { if (logger.isDebugEnabled()) { logger.debug("Writing to the repository with " + items.size() + " items."); } MethodInvoker invoker = createMethodInvoker(repository, methodName); for (T object : items) { invoker.setArguments(new Object [] {object}); doInvoke(invoker); } }
/** * Invokes the target method with given arguments. * * @param args arguments for the invoked method * @return object returned by invoked method * * @throws Exception exception thrown when executing the delegate method. */ protected T invokeDelegateMethodWithArguments(Object[] args) throws Exception { MethodInvoker invoker = createMethodInvoker(targetObject, targetMethod); invoker.setArguments(args); return doInvoke(invoker); }
methodInvoker.setTargetObject(target); methodInvoker.setTargetMethod(name); methodInvoker.setArguments(args); methodInvoker.prepare();
/** * Invoker the target method with arguments set by * {@link #setArguments(Object[])}. * * @return object returned by invoked method * * @throws Exception exception thrown when executing the delegate method. */ protected T invokeDelegateMethod() throws Exception { MethodInvoker invoker = createMethodInvoker(targetObject, targetMethod); invoker.setArguments(arguments); return doInvoke(invoker); }
/** * Invokes the target method with given argument. * * @param object argument for the target method * @return object returned by target method * * @throws Exception exception thrown when executing the delegate method. */ protected T invokeDelegateMethodWithArgument(Object object) throws Exception { MethodInvoker invoker = createMethodInvoker(targetObject, targetMethod); invoker.setArguments(new Object[] { object }); return doInvoke(invoker); }
/** * Performs the actual reading of a page via the repository. * Available for overriding as needed. * * @return the list of items that make up the page * @throws Exception Based on what the underlying method throws or related to the * calling of the method */ @SuppressWarnings("unchecked") protected List<T> doPageRead() throws Exception { Pageable pageRequest = PageRequest.of(page, pageSize, sort); MethodInvoker invoker = createMethodInvoker(repository, methodName); List<Object> parameters = new ArrayList<>(); if(arguments != null && arguments.size() > 0) { parameters.addAll(arguments); } parameters.add(pageRequest); invoker.setArguments(parameters.toArray()); Page<T> curPage = (Page<T>) doInvoke(invoker); return curPage.getContent(); }
methodInvoker.setTargetObject(getDelegate()); methodInvoker.setTargetMethod(methodName); methodInvoker.setArguments(arguments); methodInvoker.prepare(); return methodInvoker.invoke();
@Test public void testInvokeWithNullArgument() throws Exception { MethodInvoker methodInvoker = new MethodInvoker(); methodInvoker.setTargetClass(TestClass1.class); methodInvoker.setTargetMethod("nullArgument"); methodInvoker.setArguments(new Object[] {null}); methodInvoker.prepare(); methodInvoker.invoke(); }
@Test public void stringWithMethodInvoker() throws Exception { MethodInvoker methodInvoker = new MethodInvoker(); methodInvoker.setTargetObject(new Greeter()); methodInvoker.setTargetMethod("greet"); methodInvoker.setArguments("no match"); exception.expect(NoSuchMethodException.class); methodInvoker.prepare(); }
mi.setTargetObject(tc1); mi.setTargetMethod("method1"); mi.setArguments((Object[]) null); mi.prepare(); i = (Integer) mi.invoke(); mi.setTargetClass(TestClass1.class); mi.setTargetMethod("supertypes"); mi.setArguments(new ArrayList<>(), new ArrayList<>(), "hello"); mi.prepare(); assertEquals("hello", mi.invoke()); mi.setTargetClass(TestClass1.class); mi.setTargetMethod("supertypes2"); mi.setArguments(new ArrayList<>(), new ArrayList<>(), "hello", "bogus"); mi.prepare(); assertEquals("hello", mi.invoke()); mi.setTargetClass(TestClass1.class); mi.setTargetMethod("supertypes2"); mi.setArguments(new ArrayList<>(), new ArrayList<>(), "hello", Boolean.TRUE);
@Test public void purchaserWithMethodInvoker() throws Exception { MethodInvoker methodInvoker = new MethodInvoker(); methodInvoker.setTargetObject(new Greeter()); methodInvoker.setTargetMethod("greet"); methodInvoker.setArguments(new Purchaser()); methodInvoker.prepare(); String greeting = (String) methodInvoker.invoke(); assertEquals("purchaser: hello", greeting); }
@Test public void shopperWithMethodInvoker() throws Exception { MethodInvoker methodInvoker = new MethodInvoker(); methodInvoker.setTargetObject(new Greeter()); methodInvoker.setTargetMethod("greet"); methodInvoker.setArguments(new Shopper()); methodInvoker.prepare(); String greeting = (String) methodInvoker.invoke(); assertEquals("purchaser: may I help you?", greeting); }
@Test public void salesmanWithMethodInvoker() throws Exception { MethodInvoker methodInvoker = new MethodInvoker(); methodInvoker.setTargetObject(new Greeter()); methodInvoker.setTargetMethod("greet"); methodInvoker.setArguments(new Salesman()); methodInvoker.prepare(); String greeting = (String) methodInvoker.invoke(); assertEquals("greetable: how are sales?", greeting); }
@Test public void customerWithMethodInvoker() throws Exception { MethodInvoker methodInvoker = new MethodInvoker(); methodInvoker.setTargetObject(new Greeter()); methodInvoker.setTargetMethod("greet"); methodInvoker.setArguments(new Customer()); methodInvoker.prepare(); String greeting = (String) methodInvoker.invoke(); assertEquals("customer: good day", greeting); }
@Test public void regularWithMethodInvoker() throws Exception { MethodInvoker methodInvoker = new MethodInvoker(); methodInvoker.setTargetObject(new Greeter()); methodInvoker.setTargetMethod("greet"); methodInvoker.setArguments(new Regular("Kotter")); methodInvoker.prepare(); String greeting = (String) methodInvoker.invoke(); assertEquals("regular: welcome back Kotter", greeting); }
@Test public void vipWithMethodInvoker() throws Exception { MethodInvoker methodInvoker = new MethodInvoker(); methodInvoker.setTargetObject(new Greeter()); methodInvoker.setTargetMethod("greet"); methodInvoker.setArguments(new VIP("Fonzie")); methodInvoker.prepare(); String greeting = (String) methodInvoker.invoke(); assertEquals("regular: whassup dude?", greeting); }
/** * Invoker the target method with arguments set by * {@link #setArguments(Object[])}. * * @return object returned by invoked method * * @throws Exception exception thrown when executing the delegate method. */ protected T invokeDelegateMethod() throws Exception { MethodInvoker invoker = createMethodInvoker(targetObject, targetMethod); invoker.setArguments(arguments); return doInvoke(invoker); }
public static Object invokeMethod(Class<?> targetClass, String targetMethod, Object... arguments) { MethodInvoker invoker = new MethodInvoker(); invoker.setTargetClass(targetClass); invoker.setTargetMethod(targetMethod); invoker.setArguments(arguments); return invoke(invoker); }
public static Object invokeMethod(Object targetObject, String targetMethod, Object... arguments) { MethodInvoker invoker = new MethodInvoker(); invoker.setTargetObject(targetObject); invoker.setTargetMethod(targetMethod); invoker.setArguments(arguments); return invoke(invoker); }
private Object invoke(CachedInvocation invocation) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { final MethodInvoker invoker = new MethodInvoker(); invoker.setTargetObject(invocation.getTargetBean()); invoker.setArguments(invocation.getArguments()); invoker.setTargetMethod(invocation.getTargetMethod().getName()); invoker.prepare(); return invoker.invoke(); }