use of org.springframework.util.MethodInvoker in project geode by apache.
the class IdentifiableUtils method getId.
@SuppressWarnings("unchecked")
public static <T> T getId(final Object identifiableObject) {
if (isGetIdMethodAvailable(identifiableObject)) {
final MethodInvoker method = new MethodInvoker();
method.setTargetObject(identifiableObject);
method.setTargetMethod("getId");
try {
return (T) method.invoke();
} catch (Exception ignore) {
ignore.printStackTrace();
}
}
return null;
}
use of org.springframework.util.MethodInvoker in project spring-framework by spring-projects.
the class MessageListenerAdapter method invokeListenerMethod.
/**
* Invoke the specified listener method.
* @param methodName the name of the listener method
* @param arguments the message arguments to be passed in
* @return the result returned from the listener method
* @throws JMSException if thrown by JMS API methods
* @see #getListenerMethodName
* @see #buildListenerArguments
*/
protected Object invokeListenerMethod(String methodName, Object[] arguments) throws JMSException {
try {
MethodInvoker methodInvoker = new MethodInvoker();
methodInvoker.setTargetObject(getDelegate());
methodInvoker.setTargetMethod(methodName);
methodInvoker.setArguments(arguments);
methodInvoker.prepare();
return methodInvoker.invoke();
} catch (InvocationTargetException ex) {
Throwable targetEx = ex.getTargetException();
if (targetEx instanceof JMSException) {
throw (JMSException) targetEx;
} else {
throw new ListenerExecutionFailedException("Listener method '" + methodName + "' threw exception", targetEx);
}
} catch (Throwable ex) {
throw new ListenerExecutionFailedException("Failed to invoke target method '" + methodName + "' with arguments " + ObjectUtils.nullSafeToString(arguments), ex);
}
}
use of org.springframework.util.MethodInvoker in project spring-framework by spring-projects.
the class ReflectionTestUtils method invokeMethod.
/**
* Invoke the method with the given {@code name} on the supplied target
* object with the supplied arguments.
* <p>This method traverses the class hierarchy in search of the desired
* method. In addition, an attempt will be made to make non-{@code public}
* methods <em>accessible</em>, thus allowing one to invoke {@code protected},
* {@code private}, and <em>package-private</em> methods.
* @param target the target object on which to invoke the specified method
* @param name the name of the method to invoke
* @param args the arguments to provide to the method
* @return the invocation result, if any
* @see MethodInvoker
* @see ReflectionUtils#makeAccessible(Method)
* @see ReflectionUtils#invokeMethod(Method, Object, Object[])
* @see ReflectionUtils#handleReflectionException(Exception)
*/
@SuppressWarnings("unchecked")
public static <T> T invokeMethod(Object target, String name, Object... args) {
Assert.notNull(target, "Target object must not be null");
Assert.hasText(name, "Method name must not be empty");
try {
MethodInvoker methodInvoker = new MethodInvoker();
methodInvoker.setTargetObject(target);
methodInvoker.setTargetMethod(name);
methodInvoker.setArguments(args);
methodInvoker.prepare();
if (logger.isDebugEnabled()) {
logger.debug(String.format("Invoking method '%s' on %s with arguments %s", name, safeToString(target), ObjectUtils.nullSafeToString(args)));
}
return (T) methodInvoker.invoke();
} catch (Exception ex) {
ReflectionUtils.handleReflectionException(ex);
throw new IllegalStateException("Should never get here");
}
}
use of org.springframework.util.MethodInvoker in project spring-framework by spring-projects.
the class MethodInvokingFactoryBeanTests method testInvokeWithNullArgument.
@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();
}
use of org.springframework.util.MethodInvoker in project geode by apache.
the class IdentifiableUtils method setId.
@SuppressWarnings("unchecked")
public static <T> void setId(final Object identifiableObject, final T id) {
if (isSetIdMethodAvailable(identifiableObject, id)) {
final MethodInvoker method = new MethodInvoker();
method.setTargetObject(identifiableObject);
method.setTargetMethod("setId");
method.setArguments(new Object[] { id });
try {
method.prepare();
method.invoke();
} catch (Exception e) {
throw new IllegalArgumentException(String.format("Failed to set ID (%1$s) on Object (%2$s)!", id, identifiableObject), e);
}
} else {
LOGGER.warning(String.format("Not able to set ID (%1$s) of type (%2$s) on Object of type (%3$s)", id, ObjectUtils.nullSafeClassName(id), ObjectUtils.nullSafeClassName(identifiableObject)));
}
}
Aggregations