/** invokes the given method on the given clazz or instance, doing reasonably good matching on args etc * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException */ public static Maybe<Object> invokeMethodFromArgs(Object clazzOrInstance, String method, List<?> args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { return invokeMethodFromArgs(clazzOrInstance, method, args, false); }
/** invokes the given method on the given clazz or instance, assuming that the method matches passed arguments * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException */ public static Object invokeMethodFromArgs(Object clazzOrInstance, Method m, List<?> args) throws IllegalAccessException, InvocationTargetException { return invokeMethodFromArgs(clazzOrInstance, m, args, false); }
/** as {@link #invokeMethodFromArgs(Object, Method, List)} but giving control over whether to set it accessible */ public static Object invokeMethodFromArgs(Object clazzOrInstance, Method m, List<?> args, boolean setAccessible) throws IllegalAccessException, InvocationTargetException { return invokeMethodFromArgs(clazzOrInstance, m, args, setAccessible, Optional.<TypeCoercer>absent()); }
/** as {@link #invokeMethodFromArgs(Object, String, List)} but giving control over whether to set it accessible */ public static Maybe<Object> invokeMethodFromArgs(Object clazzOrInstance, String method, List<?> args, boolean setAccessible) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { return invokeMethodFromArgs(clazzOrInstance, method, args, setAccessible, Optional.<TypeCoercer>absent()); }
/** @deprecated since 0.10.0 use {@link #invokeMethodFromArgs(Object, String, List)} */ @Deprecated public static Optional<Object> invokeMethodWithArgs(Object clazzOrInstance, String method, List<?> args, boolean setAccessible) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { return invokeMethodFromArgs(clazzOrInstance, method, args, setAccessible).toOptional(); }
@Test public void testMethodInvocation() throws Exception { Method m1Short = CI1.class.getMethod("m1", String.class, int.class); Method m1Long = CI1.class.getMethod("m1", String.class, int.class, int.class, int[].class); Assert.assertEquals(Reflections.invokeMethodFromArgs(CI1.class, m1Short, Arrays.<Object>asList("hello", 3)), "hello3"); Assert.assertEquals(Reflections.invokeMethodFromArgs(CI1.class, m1Long, Arrays.<Object>asList("hello", 3, 4, 5)), "hello12"); }
@Test public void testInvocation() throws Exception { Method m = CI1.class.getMethod("m1", String.class, int.class, int.class, int[].class); Assert.assertEquals(m.invoke(null, "hello", 1, 2, new int[] { 3, 4}), "hello10"); Assert.assertEquals(Reflections.invokeMethodFromArgs(CI1.class, "m1", Arrays.<Object>asList("hello", 3)).get(), "hello3"); Assert.assertEquals(Reflections.invokeMethodFromArgs(CI1.class, "m1", Arrays.<Object>asList("hello", 3, 4, 5)).get(), "hello12"); }
private String getOsgiSimpleObjectsVal(Object val) throws Exception { assertNotNull(val); return (String) Reflections.invokeMethodFromArgs(val, "getVal", ImmutableList.of()).get(); }
@Test public void testInvocationCoercingArgs() throws Exception { TypeCoercerExtensible rawCoercer = TypeCoercerExtensible.newDefault(); new CommonAdaptorTypeCoercions(rawCoercer).registerAllAdapters(); Optional<TypeCoercer> coercer = Optional.<TypeCoercer>of(rawCoercer); Method m1Short = CI1.class.getMethod("m1", String.class, int.class); Method m1Long = CI1.class.getMethod("m1", String.class, int.class, int.class, int[].class); Assert.assertEquals(m1Short.invoke(null, "hello", 1), "hello1"); Assert.assertEquals(m1Long.invoke(null, "hello", 1, 2, new int[] { 3, 4}), "hello10"); Assert.assertEquals(Reflections.invokeMethodFromArgs(CI1.class, "m1", Arrays.<Object>asList('a', "2"), false, coercer).get(), "a2"); Assert.assertEquals(Reflections.invokeMethodFromArgs(CI1.class, "m1", Arrays.<Object>asList('a', "3", "4", "5"), false, coercer).get(), "a12"); Assert.assertEquals(Reflections.invokeMethodFromArgs(CI1.class, "m1", Arrays.<Object>asList('a', (byte)3, (byte)4, (byte)5), false, coercer).get(), "a12"); }
@SuppressWarnings("unchecked") protected <T> T invokeEffectorMethodLocal(Entity entity, Effector<T> eff, Map<String, ?> args) { assert isManagedLocally(entity) : "cannot invoke effector method at "+this+" because it is not managed here"; totalEffectorInvocationCount.incrementAndGet(); Object[] transformedArgs = EffectorUtils.prepareArgsForEffector(eff, args); try { Maybe<Object> result = Reflections.invokeMethodFromArgs(entity, eff.getName(), Arrays.asList(transformedArgs)); if (result.isPresent()) { return (T) result.get(); } else { throw new IllegalStateException("Unable to invoke entity effector method "+eff.getName()+" on "+entity+" - not found matching args"); } } catch (IllegalAccessException | InvocationTargetException e) { // Note that if we do any "nicer" error, such as: // throw Exceptions.propagate("Unable to invoke entity effector method "+eff.getName()+" on "+entity, e) // then EffectorBasicTest.testInvokeEffectorErrorCollapsedNicely fails because its call to: // Exceptions.collapseTextInContext // does not unwrap this text. throw Exceptions.propagate(e); } }
/** as {@link #invokeMethodFromArgs(Object, String, List)} but giving control over whether to set it accessible */ public static Maybe<Object> invokeMethodFromArgs(Object clazzOrInstance, String method, List<?> args, boolean setAccessible, Optional<? extends TypeCoercer> coercer) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { Maybe<Method> maybeMethod = getMethodFromArgs(clazzOrInstance, method, args); if (coercer.isPresent() && maybeMethod.isAbsent()) { maybeMethod = getMethodFromArgs(clazzOrInstance, method, args, coercer); } if (maybeMethod.isAbsent()) { return Maybe.absent(Maybe.getException(maybeMethod)); } Method m = maybeMethod.get(); return Maybe.of(invokeMethodFromArgs(clazzOrInstance, m, args, setAccessible, coercer)); }