Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2006-2007 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.springframework.batch.item.adapter;
 
 import java.util.List;
 
Superclass for delegating classes which dynamically call a custom method of injected object. Provides convenient API for dynamic method invocation shielding subclasses from low-level details and exception handling. java.lang.Exceptions thrown by a successfully invoked delegate method are re-thrown without wrapping. In case the delegate method throws a java.lang.Throwable that doesn't subclass java.lang.Exception it will be wrapped by AbstractMethodInvokingDelegator.InvocationTargetThrowableWrapper.

Author(s):
Robert Kasanicky
 
 public abstract class AbstractMethodInvokingDelegator<T> implements InitializingBean {
 
 	private Object targetObject;
 
 	private String targetMethod;
 
 	private Object[] arguments;

Invoker the target method with arguments set by setArguments(java.lang.Object[]).

Returns:
object returned by invoked method
Throws:
DynamicMethodInvocationException if the org.springframework.util.MethodInvoker used throws exception
 
 	protected T invokeDelegateMethod() throws Exception {
 		return doInvoke(invoker);
 	}

Invokes the target method with given argument.

Parameters:
object argument for the target method
Returns:
object returned by target method
Throws:
DynamicMethodInvocationException if the org.springframework.util.MethodInvoker used throws exception
 
 	protected T invokeDelegateMethodWithArgument(Object objectthrows Exception {
 		invoker.setArguments(new Object[] { object });
 		return doInvoke(invoker);
 	}

Invokes the target method with given arguments.

Parameters:
args arguments for the invoked method
Returns:
object returned by invoked method
Throws:
DynamicMethodInvocationException if the org.springframework.util.MethodInvoker used throws exception
 
 	protected T invokeDelegateMethodWithArguments(Object[] argsthrows Exception {
 		invoker.setArguments(args);
 		return doInvoke(invoker);
 	}

Create a new configured instance of org.springframework.util.MethodInvoker.
 
 	private MethodInvoker createMethodInvoker(Object targetObjectString targetMethod) {
 		invoker.setTargetObject(targetObject);
 		invoker.setTargetMethod(targetMethod);
 		return invoker;
 	}

Prepare and invoke the invoker, rethrow checked exceptions as unchecked.

Parameters:
invoker configured invoker
Returns:
return value of the invoked method
	@SuppressWarnings("unchecked")
	private T doInvoke(MethodInvoker invokerthrows Exception {
		try {
			invoker.prepare();
		}
		}
		catch (NoSuchMethodException e) {
		}
		try {
			return (T) invoker.invoke();
		}
			if (e.getCause() instanceof Exception) {
				throw (Exceptione.getCause();
			}
			else {
			}
		}
		}
	}
	public void afterPropertiesSet() throws Exception {
				"target class must declare a method with matching name and parameter types");
	}

Returns:
true if target class declares a method matching target method name with given number of arguments of appropriate type.
	private boolean targetClassDeclaresTargetMethod() {
		Method[] memberMethods = invoker.getTargetClass().getMethods();
		Method[] declaredMethods = invoker.getTargetClass().getDeclaredMethods();
		List<MethodallMethods = new ArrayList<Method>();
		allMethods.addAll(Arrays.asList(memberMethods));
		allMethods.addAll(Arrays.asList(declaredMethods));
		String targetMethodName = invoker.getTargetMethod();
		for (Method method : allMethods) {
			if (method.getName().equals(targetMethodName)) {
				Class<?>[] params = method.getParameterTypes();
				if ( == null) {
					// don't check signature, assume arguments will be supplied
					// correctly at runtime
					return true;
				}
				if (. == params.length) {
					boolean argumentsMatchParameters = true;
					for (int j = 0; j < params.lengthj++) {
						if ([j] == null) {
							continue;
						}
						if (!(params[j].isAssignableFrom([j].getClass()))) {
							argumentsMatchParameters = false;
						}
					}
					if (argumentsMatchParameters)
						return true;
				}
			}
		}
		return false;
	}

Parameters:
targetObject the delegate - bean id can be used to set this value in Spring configuration
	public void setTargetObject(Object targetObject) {
		this. = targetObject;
	}

Parameters:
targetMethod name of the method to be invoked on setTargetObject(java.lang.Object).
	public void setTargetMethod(String targetMethod) {
		this. = targetMethod;
	}

Parameters:
arguments arguments values for the { setTargetMethod(java.lang.String). These will be used only when the subclass tries to invoke the target method without providing explicit argument values. If arguments are set to not-null value afterPropertiesSet() will check the values are compatible with target method's signature. In case arguments are null (not set) method signature will not be checked and it is assumed correct values will be supplied at runtime.
	public void setArguments(Object[] arguments) {
		this. = arguments == null ? null : Arrays.asList(arguments).toArray();
	}

Used to wrap a java.lang.Throwable (not an java.lang.Exception) thrown by a reflectively-invoked delegate.

Author(s):
Robert Kasanicky
	public static class InvocationTargetThrowableWrapper extends RuntimeException {
			super(cause);
		}
	}
New to GrepCode? Check out our FAQ X