Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2002-2008 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.support;
 
 
Utility methods for create MethodInvoker instances.

Author(s):
Lucas Ward
Since:
2.0
 
 public class MethodInvokerUtils {

Create a MethodInvoker using the provided method name to search.

Parameters:
object to be invoked
methodName of the method to be invoked
paramsRequired boolean indicating whether the parameters are required, if false, a no args version of the method will be searched for.
paramTypes - parameter types of the method to search for.
Returns:
MethodInvoker if the method is found, null if it is not.
 
 	public static MethodInvoker getMethodInvokerByName(Object objectString methodNameboolean paramsRequired,
 			Class<?>... paramTypes) {
 		Assert.notNull(object"Object to invoke must not be null");
 		Method method = ClassUtils.getMethodIfAvailable(object.getClass(), methodNameparamTypes);
 		if (method == null) {
 			String errorMsg = "no method found with name [" + methodName + "] on class ["
 					+ object.getClass().getSimpleName() + "] compatable with the signature ["
 					+ getParamTypesString(paramTypes) + "].";
 			Assert.isTrue(!paramsRequirederrorMsg);
 			// if no method was found for the given parameters, and the
 			// parameters aren't required, then try with no params
 			method = ClassUtils.getMethodIfAvailable(object.getClass(), methodNamenew Class[] {});
 			Assert.notNull(methoderrorMsg);
 		}
 		return new SimpleMethodInvoker(objectmethod);
 	}

Create a String representation of the array of parameter types.

Parameters:
paramTypes
Returns:
String
 
 	public static String getParamTypesString(Class<?>... paramTypes) {
 		StringBuffer paramTypesList = new StringBuffer("(");
 		for (int i = 0; i < paramTypes.lengthi++) {
 			paramTypesList.append(paramTypes[i].getSimpleName());
 			if (i + 1 < paramTypes.length) {
 				paramTypesList.append(", ");
 			}
 		}
 		return paramTypesList.append(")").toString();
 	}

Create a MethodInvoker using the provided interface, and method name from that interface.

Parameters:
cls the interface to search for the method named
methodName of the method to be invoked
object to be invoked
paramTypes - parameter types of the method to search for.
Returns:
MethodInvoker if the method is found, null if it is not.
 
 	public static MethodInvoker getMethodInvokerForInterface(Class<?> clsString methodNameObject object,
 			Class<?>... paramTypes) {
 
 		if (cls.isAssignableFrom(object.getClass())) {
 			return MethodInvokerUtils.getMethodInvokerByName(objectmethodNametrueparamTypes);
 		}
 		else {
			return null;
		}
	}

Create a MethodInvoker from the delegate based on the annotationType. Ensure that the annotated method has a valid set of parameters.

Parameters:
annotationType the annotation to scan for
target the target object
expectedParamTypes the expected parameter types for the method
Returns:
a MethodInvoker
	public static MethodInvoker getMethodInvokerByAnnotation(final Class<? extends AnnotationannotationType,
			final Object targetfinal Class<?>... expectedParamTypes) {
		MethodInvoker mi = MethodInvokerUtils.getMethodInvokerByAnnotation(annotationTypetarget);
		final Class<?> targetClass = (target instanceof Advised) ? ((Advisedtarget).getTargetSource()
				.getTargetClass() : target.getClass();
		if (mi != null) {
			ReflectionUtils.doWithMethods(targetClassnew ReflectionUtils.MethodCallback() {
				public void doWith(Method methodthrows IllegalArgumentExceptionIllegalAccessException {
					Annotation annotation = AnnotationUtils.findAnnotation(methodannotationType);
					if (annotation != null) {
						Class<?>[] paramTypes = method.getParameterTypes();
						if (paramTypes.length > 0) {
							String errorMsg = "The method [" + method.getName() + "] on target class ["
targetClass.getSimpleName() + "] is incompatable with the signature ["
getParamTypesString(expectedParamTypes) + "] expected for the annotation ["
annotationType.getSimpleName() + "].";
							Assert.isTrue(paramTypes.length == expectedParamTypes.lengtherrorMsg);
							for (int i = 0; i < paramTypes.lengthi++) {
								Assert.isTrue(expectedParamTypes[i].isAssignableFrom(paramTypes[i]), errorMsg);
							}
						}
					}
				}
			});
		}
		return mi;
	}

Create MethodInvoker for the method with the provided annotation on the provided object. Annotations that cannot be applied to methods (i.e. that aren't annotated with an element type of METHOD) will cause an exception to be thrown.

Parameters:
annotationType to be searched for
target to be invoked
Returns:
MethodInvoker for the provided annotation, null if none is found.
	public static MethodInvoker getMethodInvokerByAnnotation(final Class<? extends AnnotationannotationType,
			final Object target) {
		Assert.notNull(target"Target must not be null");
		Assert.notNull(annotationType"AnnotationType must not be null");
		Assert.isTrue(ObjectUtils.containsElement(annotationType.getAnnotation(Target.class).value(),
				.), "Annotation [" + annotationType + "] is not a Method-level annotation.");
		final Class<?> targetClass = (target instanceof Advised) ? ((Advisedtarget).getTargetSource()
				.getTargetClass() : target.getClass();
		if (targetClass == null) {
			// Proxy with no target cannot have annotations
			return null;
		}
		final AtomicReference<MethodannotatedMethod = new AtomicReference<Method>();
		ReflectionUtils.doWithMethods(targetClassnew ReflectionUtils.MethodCallback() {
			public void doWith(Method methodthrows IllegalArgumentExceptionIllegalAccessException {
				Annotation annotation = AnnotationUtils.findAnnotation(methodannotationType);
				if (annotation != null) {
					Assert.isNull(annotatedMethod.get(), "found more than one method on target class ["
targetClass.getSimpleName() + "] with the annotation type ["
annotationType.getSimpleName() + "].");
					annotatedMethod.set(method);
				}
			}
		});
		Method method = annotatedMethod.get();
		if (method == null) {
			return null;
		}
		else {
			return new SimpleMethodInvoker(targetannotatedMethod.get());
		}
	}

Create a MethodInvoker for the delegate from a single public method.

Parameters:
target an object to search for an appropriate method
Returns:
a MethodInvoker that calls a method on the delegate
	public static <C, T> MethodInvoker getMethodInvokerForSingleArgument(Object target) {
		final AtomicReference<MethodmethodHolder = new AtomicReference<Method>();
		ReflectionUtils.doWithMethods(target.getClass(), new ReflectionUtils.MethodCallback() {
			public void doWith(Method methodthrows IllegalArgumentExceptionIllegalAccessException {
				if (method.getParameterTypes() == null || method.getParameterTypes().length != 1) {
					return;
				}
				if (method.getReturnType().equals(.) || ReflectionUtils.isEqualsMethod(method)) {
					return;
				}
				Assert.state(methodHolder.get() == null,
						"More than one non-void public method detected with single argument.");
				methodHolder.set(method);
			}
		});
		Method method = methodHolder.get();
		return new SimpleMethodInvoker(targetmethod);
	}
New to GrepCode? Check out our FAQ X