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;
 
 
MethodResolver implementation that finds a single Method on the given Class that contains the specified annotation type.

Author(s):
Mark Fisher
 
 public class AnnotationMethodResolver implements MethodResolver {
 
 	private Class<? extends AnnotationannotationType;


Create a MethodResolver for the specified Method-level annotation type
 
 	public AnnotationMethodResolver(Class<? extends AnnotationannotationType) {
 		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.");
 		this. = annotationType;
 	}


Find a single Method on the Class of the given candidate object that contains the annotation type for which this resolver is searching.

Parameters:
candidate the instance whose Class will be checked for the annotation
Returns:
a single matching Method instance or null if the candidate's Class contains no Methods with the specified annotation
Throws:
java.lang.IllegalArgumentException if more than one Method has the specified annotation
 
 	public Method findMethod(Object candidate) {
 		Assert.notNull(candidate"candidate object must not be null");
 		Class<?> targetClass = AopUtils.getTargetClass(candidate);
 		if (targetClass == null) {
 			targetClass = candidate.getClass();
 		}
 		return this.findMethod(targetClass);
 	}

Find a single Method on the given Class that contains the annotation type for which this resolver is searching.

Parameters:
clazz the Class instance to check for the annotation
Returns:
a single matching Method instance or null if the Class contains no Methods with the specified annotation
Throws:
java.lang.IllegalArgumentException if more than one Method has the specified annotation
 
 	public Method findMethod(final Class<?> clazz) {
 		Assert.notNull(clazz"class must not be null");
 		final AtomicReference<MethodannotatedMethod = new AtomicReference<Method>();
 		ReflectionUtils.doWithMethods(clazznew ReflectionUtils.MethodCallback() {
 			public void doWith(Method methodthrows IllegalArgumentExceptionIllegalAccessException {
 				Annotation annotation = AnnotationUtils.findAnnotation(method);
 				if (annotation != null) {
 					Assert.isNull(annotatedMethod.get(), "found more than one method on target class ["
 							+ clazz + "] with the annotation type [" +  + "]");
 					annotatedMethod.set(method);
 				}
 			}
		});
		return annotatedMethod.get();
	}
New to GrepCode? Check out our FAQ X