methodInvoker.setTargetMethod(name); methodInvoker.setArguments(args); methodInvoker.prepare();
private Object doInvoke(MethodInvoker invoker) throws Exception{ try { invoker.prepare(); } catch (ClassNotFoundException | NoSuchMethodException e) { throw new DynamicMethodInvocationException(e); } try { return invoker.invoke(); } catch (InvocationTargetException e) { if (e.getCause() instanceof Exception) { throw (Exception) e.getCause(); } else { throw new InvocationTargetThrowableWrapper(e.getCause()); } } catch (IllegalAccessException e) { throw new DynamicMethodInvocationException(e); } }
/** * Added due to JDK 7 compatibility. */ public Logger getParentLogger() throws SQLFeatureNotSupportedException{ MethodInvoker invoker = new MethodInvoker(); invoker.setTargetObject(dataSource); invoker.setTargetMethod("getParentLogger"); try { invoker.prepare(); return (Logger) invoker.invoke(); } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException nsme) { throw new SQLFeatureNotSupportedException(nsme); } } }
/** * Prepare and invoke the invoker, rethrow checked exceptions as unchecked. * @param invoker configured invoker * @return return value of the invoked method */ @SuppressWarnings("unchecked") private T doInvoke(MethodInvoker invoker) throws Exception { try { invoker.prepare(); } catch (ClassNotFoundException | NoSuchMethodException e) { throw new DynamicMethodInvocationException(e); } try { return (T) invoker.invoke(); } catch (InvocationTargetException e) { if (e.getCause() instanceof Exception) { throw (Exception) e.getCause(); } else { throw new InvocationTargetThrowableWrapper(e.getCause()); } } catch (IllegalAccessException e) { throw new DynamicMethodInvocationException(e); } }
private Object doInvoke(MethodInvoker invoker) throws Exception{ try { invoker.prepare(); } catch (ClassNotFoundException e) { throw new DynamicMethodInvocationException(e); } catch (NoSuchMethodException e) { throw new DynamicMethodInvocationException(e); } try { return invoker.invoke(); } catch (InvocationTargetException e) { if (e.getCause() instanceof Exception) { throw (Exception) e.getCause(); } else { throw new InvocationTargetThrowableWrapper(e.getCause()); } } catch (IllegalAccessException e) { throw new DynamicMethodInvocationException(e); } }
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(); }
mi.setTargetObject(tc1); mi.setTargetMethod("method1"); mi.prepare(); Integer i = (Integer) mi.invoke(); assertEquals(1, i.intValue()); mi.setTargetMethod("method1"); mi.setArguments((Object[]) null); mi.prepare(); i = (Integer) mi.invoke(); assertEquals(1, i.intValue()); mi.setTargetMethod("supertypes"); mi.setArguments(new ArrayList<>(), new ArrayList<>(), "hello"); mi.prepare(); assertEquals("hello", mi.invoke()); mi.setTargetMethod("supertypes2"); mi.setArguments(new ArrayList<>(), new ArrayList<>(), "hello", "bogus"); mi.prepare(); assertEquals("hello", mi.invoke()); mi.prepare();
@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(); }
@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 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 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); }
public static Object invoke(MethodInvoker invoker) { try { invoker.prepare(); return invoker.invoke(); } catch (ClassNotFoundException e) { throw new IllegalStateException(e); } catch (NoSuchMethodException e) { throw new IllegalStateException(e); } catch (InvocationTargetException e) { throw new IllegalStateException(e); } catch (IllegalAccessException e) { throw new IllegalStateException(e); } }
/** * Added due to JDK 7 compatibility. */ public Logger getParentLogger() throws SQLFeatureNotSupportedException{ MethodInvoker invoker = new MethodInvoker(); invoker.setTargetObject(dataSource); invoker.setTargetMethod("getParentLogger"); try { invoker.prepare(); return (Logger) invoker.invoke(); } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException nsme) { throw new SQLFeatureNotSupportedException(nsme); } } }
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(); }
private Object getBean(ThinJarAppWrapper wrapper, Class<?> type) throws IllegalAccessException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException { Object app = findContext(wrapper); MethodInvoker invoker = new MethodInvoker(); invoker.setTargetObject(app); invoker.setTargetMethod("getBean"); invoker.setArguments(new Object[] { findType(app, type) }); invoker.prepare(); return invoker.invoke(); }
private Map<String, Object> getBeansOfType(ThinJarAppWrapper wrapper, Class<?> type) throws IllegalAccessException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException { Object app = findContext(wrapper); MethodInvoker invoker = new MethodInvoker(); invoker.setTargetObject(app); invoker.setTargetMethod("getBeansOfType"); invoker.setArguments(new Object[] { findType(app, type) }); invoker.prepare(); @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) invoker.invoke(); return map; }