EMMA Coverage Report (generated Thu Jan 24 13:37:04 CST 2013)
[all classes][org.springframework.batch.support]

COVERAGE SUMMARY FOR SOURCE FILE [AnnotationMethodResolver.java]

nameclass, %method, %block, %line, %
AnnotationMethodResolver.java100% (2/2)83%  (5/6)86%  (95/110)75%  (15/20)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class AnnotationMethodResolver100% (1/1)75%  (3/4)77%  (49/64)67%  (10/15)
findMethod (Object): Method 0%   (0/1)0%   (0/15)0%   (0/5)
AnnotationMethodResolver (Class): void 100% (1/1)100% (27/27)100% (5/5)
access$000 (AnnotationMethodResolver): Class 100% (1/1)100% (3/3)100% (1/1)
findMethod (Class): Method 100% (1/1)100% (19/19)100% (4/4)
     
class AnnotationMethodResolver$1100% (1/1)100% (2/2)100% (46/46)100% (6/6)
AnnotationMethodResolver$1 (AnnotationMethodResolver, AtomicReference, Class)... 100% (1/1)100% (12/12)100% (1/1)
doWith (Method): void 100% (1/1)100% (34/34)100% (5/5)

1/*
2 * Copyright 2002-2008 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17package org.springframework.batch.support;
18 
19import java.lang.annotation.Annotation;
20import java.lang.annotation.ElementType;
21import java.lang.annotation.Target;
22import java.lang.reflect.Method;
23import java.util.concurrent.atomic.AtomicReference;
24 
25import org.springframework.aop.support.AopUtils;
26import org.springframework.core.annotation.AnnotationUtils;
27import org.springframework.util.Assert;
28import org.springframework.util.ObjectUtils;
29import org.springframework.util.ReflectionUtils;
30 
31/**
32 * MethodResolver implementation that finds a <em>single</em> Method on the
33 * given Class that contains the specified annotation type.
34 * 
35 * @author Mark Fisher
36 */
37public class AnnotationMethodResolver implements MethodResolver {
38 
39        private Class<? extends Annotation> annotationType;
40 
41 
42        /**
43         * Create a MethodResolver for the specified Method-level annotation type
44         */
45        public AnnotationMethodResolver(Class<? extends Annotation> annotationType) {
46                Assert.notNull(annotationType, "annotationType must not be null");
47                Assert.isTrue(ObjectUtils.containsElement(
48                                annotationType.getAnnotation(Target.class).value(), ElementType.METHOD),
49                                "Annotation [" + annotationType + "] is not a Method-level annotation.");
50                this.annotationType = annotationType;
51        }
52 
53 
54        /**
55         * Find a <em>single</em> Method on the Class of the given candidate object
56         * that contains the annotation type for which this resolver is searching.
57         * 
58         * @param candidate the instance whose Class will be checked for the
59         * annotation
60         * 
61         * @return a single matching Method instance or <code>null</code> if the
62         * candidate's Class contains no Methods with the specified annotation
63         * 
64         * @throws IllegalArgumentException if more than one Method has the
65         * specified annotation
66         */
67        public Method findMethod(Object candidate) {
68                Assert.notNull(candidate, "candidate object must not be null");
69                Class<?> targetClass = AopUtils.getTargetClass(candidate);
70                if (targetClass == null) {
71                        targetClass = candidate.getClass();
72                }
73                return this.findMethod(targetClass);
74        }
75 
76        /**
77         * Find a <em>single</em> Method on the given Class that contains the
78         * annotation type for which this resolver is searching.
79         * 
80         * @param clazz the Class instance to check for the annotation
81         * 
82         * @return a single matching Method instance or <code>null</code> if the
83         * Class contains no Methods with the specified annotation
84         * 
85         * @throws IllegalArgumentException if more than one Method has the
86         * specified annotation
87         */
88        public Method findMethod(final Class<?> clazz) {
89                Assert.notNull(clazz, "class must not be null");
90                final AtomicReference<Method> annotatedMethod = new AtomicReference<Method>();
91                ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() {
92                        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
93                                Annotation annotation = AnnotationUtils.findAnnotation(method, annotationType);
94                                if (annotation != null) {
95                                        Assert.isNull(annotatedMethod.get(), "found more than one method on target class ["
96                                                        + clazz + "] with the annotation type [" + annotationType + "]");
97                                        annotatedMethod.set(method);
98                                }
99                        }
100                });
101                return annotatedMethod.get();
102        }
103 
104}

[all classes][org.springframework.batch.support]
EMMA 2.0.5312 (C) Vladimir Roubtsov