/** * Tries to find a single-parameter method with a parameter compatible with (can be coerced to) the argument, and * invokes it. * * @param instance the object to invoke the method on * @param methodName the name of the method to invoke * @param argument the argument to the method's parameter. * @return the result of the method call, or {@link org.apache.brooklyn.util.guava.Maybe#absent()} if method could not be matched. */ public static Maybe<?> tryFindAndInvokeSingleParameterMethod(final Object instance, final String methodName, final Object argument) { Class<?> clazz = instance.getClass(); Iterable<Method> methods = Arrays.asList(clazz.getMethods()); Optional<Method> matchingMethod = Iterables.tryFind(methods, matchSingleParameterMethod(methodName, argument)); if (matchingMethod.isPresent()) { Method method = matchingMethod.get(); Method accessibleMethod = Reflections.findAccessibleMethod(method).get(); try { Type paramType = method.getGenericParameterTypes()[0]; Object coercedArgument = TypeCoercions.coerce(argument, TypeToken.of(paramType)); return Maybe.of(accessibleMethod.invoke(instance, coercedArgument)); } catch (IllegalAccessException | InvocationTargetException e) { throw Exceptions.propagate(e); } } else { return Maybe.absent(); } }
if (matchingMethod.isPresent()) { Method method = matchingMethod.get(); Method accessibleMethod = Reflections.findAccessibleMethod(method).get(); try { int numOptionParams = ((List<?>)arguments).size();
@Test public void testFindAccessibleMethodFromSuperType() throws Exception { Method objectHashCode = Object.class.getMethod("hashCode", new Class[0]); Method methodOnSuperClass = PublicSuperClass.class.getMethod("methodOnSuperClass", new Class[0]); Method subMethodOnSuperClass = PrivateClass.class.getMethod("methodOnSuperClass", new Class[0]); Method methodOnInterface = PublicInterface.class.getMethod("methodOnInterface", new Class[0]); Method subMethodOnInterface = PrivateClass.class.getMethod("methodOnInterface", new Class[0]); Method inaccessibleStaticMethod = PrivateClass.class.getMethod("otherStaticMethod", new Class[0]); Method inaccessiblePublicMethod = PrivateClass.class.getMethod("otherPublicMethod", new Class[0]); Method inaccessibleProtectedMethod = PrivateClass.class.getDeclaredMethod("otherProtectedMethod", new Class[0]); assertEquals(Reflections.findAccessibleMethod(objectHashCode).get(), objectHashCode); assertEquals(Reflections.findAccessibleMethod(methodOnSuperClass).get(), methodOnSuperClass); assertEquals(Reflections.findAccessibleMethod(methodOnInterface).get(), methodOnInterface); assertEquals(Reflections.findAccessibleMethod(subMethodOnSuperClass).get(), methodOnSuperClass); assertEquals(Reflections.findAccessibleMethod(subMethodOnInterface).get(), methodOnInterface); assertFalse(Reflections.findAccessibleMethod(inaccessibleStaticMethod).isPresent()); assertFalse(Reflections.findAccessibleMethod(inaccessiblePublicMethod).isPresent()); assertFalse(Reflections.findAccessibleMethod(inaccessibleProtectedMethod).isPresent()); }