/** * 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); } } }
/** * 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); } }
this.targetClass = resolveClassName(className); this.targetMethod = methodName; Class<?> targetClass = getTargetClass(); String targetMethod = getTargetMethod(); Assert.notNull(targetClass, "Either 'targetClass' or 'targetObject' is required"); Assert.notNull(targetMethod, "Property 'targetMethod' is required"); Object[] arguments = getArguments(); Class<?>[] argTypes = new Class<?>[arguments.length]; for (int i = 0; i < arguments.length; ++i) { this.methodObject = findMatchingMethod(); if (this.methodObject == null) { throw ex;
/** * Constructor for JobMethodInvocationFailedException. * @param methodInvoker the MethodInvoker used for reflective invocation * @param cause the root cause (as thrown from the target method) */ public JobMethodInvocationFailedException(MethodInvoker methodInvoker, Throwable cause) { super("Invocation of method '" + methodInvoker.getTargetMethod() + "' on target class [" + methodInvoker.getTargetClass() + "] failed", cause); }
private MethodInvoker createMethodInvoker(Object targetObject, String targetMethod) { MethodInvoker invoker = new MethodInvoker(); invoker.setTargetObject(targetObject); invoker.setTargetMethod(targetMethod); return invoker; } }
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); } }
Object toPath = MethodUtils.invokeExactMethod(aTarget, "toPath", new Object[0]); Object options = Array.newInstance(Class.forName("java.nio.file.attribute.FileAttribute"), 0); MethodInvoker inv = new MethodInvoker(); inv.setStaticMethod("java.nio.file.Files.createSymbolicLink"); inv.setArguments(new Object[] { toPath, fromPath, options }); inv.prepare(); inv.invoke(); return;
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); }
@Override public void execute(QuartzJobContext quartzJobContext, Job job) throws Throwable { methodInvoker.invoke(); } }
/** {@inheritDoc} */ public void onApplicationEvent(ApplicationEvent event) { boolean doIt = false; ApplicationContext ac = ctx; while (ac != null && !doIt) { if (event.getSource() == ac) { doIt = true; } ac = ac.getParent(); } if (!doIt) { return; } List<MethodInvoker> invokers = findMethodInvokersByEvent(event); if (invokers != null && !invokers.isEmpty()) { for (MethodInvoker invoker : invokers) { try { if (autoPrepareInvoker && !invoker.isPrepared()) { invoker.prepare(); } invoker.invoke(); } catch (Exception e) { log.error("Failed to invoke application event callback invoker. " + e, e); } } } }
String targetMethod = getTargetMethod(); Object[] arguments = getArguments(); int argCount = arguments.length; Class<?> targetClass = getTargetClass(); Assert.state(targetClass != null, "No target class set"); Method[] candidates = ReflectionUtils.getAllDeclaredMethods(targetClass); Class<?>[] paramTypes = candidate.getParameterTypes(); if (paramTypes.length == argCount) { int typeDiffWeight = getTypeDifferenceWeight(paramTypes, arguments); if (typeDiffWeight < minTypeDiffWeight) { minTypeDiffWeight = typeDiffWeight;
protected void executeInternal(JobExecutionContext context) throws JobExecutionException { try { String name = ScheduleUtil.getTaskNameFormBean(context.getJobDetail().getKey().getName(), this.methodInvoker.getTargetMethod()); boolean isOwner = false; try { ReflectionUtils.invokeMethod(setResultMethod, context, this.methodInvoker.invoke()); LOGGER.info("Cron job has been executed.");
/** * Find a matching method with the specified name for the specified arguments. * @return a matching method, or <code>null</code> if none * @see #getTargetClass() * @see #getTargetMethod() * @see #getArguments() */ protected Method findMatchingMethod() { Method[] candidates = getTargetClass().getMethods(); int argCount = getArguments().length; Method matchingMethod = null; int numberOfMatchingMethods = 0; for (int i = 0; i < candidates.length; i++) { // Check if the inspected method has the correct name and number of parameters. if (candidates[i].getName().equals(getTargetMethod()) && candidates[i].getParameterTypes().length == argCount) { matchingMethod = candidates[i]; numberOfMatchingMethods++; } } // Only return matching method if exactly one found. if (numberOfMatchingMethods == 1) { return matchingMethod; } else { return null; } }
/** * Set a fully qualified static method name to invoke, * e.g. "example.MyExampleClass.myExampleMethod". * Convenient alternative to specifying targetClass and targetMethod. * @see #setTargetClass * @see #setTargetMethod */ public void setStaticMethod(String staticMethod) throws ClassNotFoundException { int lastDotIndex = staticMethod.lastIndexOf('.'); if (lastDotIndex == -1 || lastDotIndex == staticMethod.length()) { throw new IllegalArgumentException("staticMethod must be a fully qualified class plus method name: " + "e.g. 'example.MyExampleClass.myExampleMethod'"); } String className = staticMethod.substring(0, lastDotIndex); String methodName = staticMethod.substring(lastDotIndex + 1); setTargetClass(ClassUtils.forName(className)); setTargetMethod(methodName); }
return new MethodInvoker(obj, methodName); return new MethodInvoker(clazz, methodName);
/** * This implementation looks for a method with matching parameter types. * @see #doFindMatchingMethod */ @Override protected Method findMatchingMethod() { Method matchingMethod = super.findMatchingMethod(); // Second pass: look for method where arguments can be converted to parameter types. if (matchingMethod == null) { // Interpret argument array as individual method arguments. matchingMethod = doFindMatchingMethod(getArguments()); } if (matchingMethod == null) { // Interpret argument array as single method argument of array type. matchingMethod = doFindMatchingMethod(new Object[] {getArguments()}); } return matchingMethod; }
this.targetClass = resolveClassName(className); this.targetMethod = methodName; this.methodObject = findMatchingMethod(); if (this.methodObject == null) { throw ex;
/** * 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); } }
Object toPath = MethodUtils.invokeExactMethod(aTarget, "toPath", new Object[0]); Object options = Array.newInstance(Class.forName("java.nio.file.attribute.FileAttribute"), 0); MethodInvoker inv = new MethodInvoker(); inv.setStaticMethod("java.nio.file.Files.createSymbolicLink"); inv.setArguments(new Object[] { toPath, fromPath, options }); inv.prepare(); inv.invoke(); return;
private MethodInvoker createMethodInvoker(Object targetObject, String targetMethod) { MethodInvoker invoker = new MethodInvoker(); invoker.setTargetObject(targetObject); invoker.setTargetMethod(targetMethod); return invoker; } }