|
| 1 | +/* |
| 2 | + * Copyright 2002-2013 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 | + |
| 17 | +package org.springframework.core.type; |
| 18 | + |
| 19 | +import java.lang.annotation.Annotation; |
| 20 | +import java.lang.reflect.AnnotatedElement; |
| 21 | +import java.util.HashSet; |
| 22 | +import java.util.LinkedHashSet; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Set; |
| 25 | + |
| 26 | +import org.springframework.core.annotation.AnnotationUtils; |
| 27 | +import org.springframework.util.LinkedMultiValueMap; |
| 28 | +import org.springframework.util.MultiValueMap; |
| 29 | + |
| 30 | +/** |
| 31 | + * Internal utility class used to collect all annotation values including those declared |
| 32 | + * on meta-annotations. |
| 33 | + * |
| 34 | + * @author Phillip Webb |
| 35 | + * @since 4.0 |
| 36 | + */ |
| 37 | +class AnnotatedElementUtils { |
| 38 | + |
| 39 | + public static Set<String> getMetaAnnotationTypes(AnnotatedElement element, |
| 40 | + String annotationType) { |
| 41 | + final Set<String> types = new LinkedHashSet<String>(); |
| 42 | + process(element, annotationType, new Processor<Object>() { |
| 43 | + public Object process(Annotation annotation, int depth) { |
| 44 | + if (depth > 0) { |
| 45 | + types.add(annotation.annotationType().getName()); |
| 46 | + } |
| 47 | + return null; |
| 48 | + } |
| 49 | + }); |
| 50 | + return (types.size() == 0 ? null : types); |
| 51 | + } |
| 52 | + |
| 53 | + public static boolean hasMetaAnnotationTypes(AnnotatedElement element, |
| 54 | + String annotationType) { |
| 55 | + return Boolean.TRUE.equals( |
| 56 | + process(element, annotationType, new Processor<Boolean>() { |
| 57 | + public Boolean process(Annotation annotation, int depth) { |
| 58 | + if (depth > 0) { |
| 59 | + return true; |
| 60 | + } |
| 61 | + return null; |
| 62 | + } |
| 63 | + })); |
| 64 | + } |
| 65 | + |
| 66 | + public static boolean isAnnotated(AnnotatedElement element, String annotationType) { |
| 67 | + return Boolean.TRUE.equals( |
| 68 | + process(element, annotationType, new Processor<Boolean>() { |
| 69 | + public Boolean process(Annotation annotation, int depth) { |
| 70 | + return true; |
| 71 | + } |
| 72 | + })); |
| 73 | + } |
| 74 | + |
| 75 | + public static Map<String, Object> getAnnotationAttributes(AnnotatedElement element, |
| 76 | + String annotationType, final boolean classValuesAsString, |
| 77 | + final boolean nestedAnnotationsAsMap) { |
| 78 | + return process(element, annotationType, new Processor<Map<String, Object>>() { |
| 79 | + public Map<String, Object> process(Annotation annotation, int depth) { |
| 80 | + return AnnotationUtils.getAnnotationAttributes(annotation, |
| 81 | + classValuesAsString, nestedAnnotationsAsMap); |
| 82 | + } |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + public static MultiValueMap<String, Object> getAllAnnotationAttributes( |
| 87 | + AnnotatedElement element, final String annotationType, |
| 88 | + final boolean classValuesAsString, final boolean nestedAnnotationsAsMap) { |
| 89 | + final MultiValueMap<String, Object> attributes = new LinkedMultiValueMap<String, Object>(); |
| 90 | + process(element, annotationType, new Processor<Object>() { |
| 91 | + public Object process(Annotation annotation, int depth) { |
| 92 | + if (annotation.annotationType().getName().equals(annotationType)) { |
| 93 | + for (Map.Entry<String, Object> entry : AnnotationUtils.getAnnotationAttributes( |
| 94 | + annotation, classValuesAsString, nestedAnnotationsAsMap).entrySet()) { |
| 95 | + attributes.add(entry.getKey(), entry.getValue()); |
| 96 | + } |
| 97 | + } |
| 98 | + return null; |
| 99 | + } |
| 100 | + }); |
| 101 | + return (attributes.size() == 0 ? null : attributes); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Process all annotations of the specified annotation type and recursively all |
| 106 | + * meta-annotations on the specified type. |
| 107 | + * @param element the annotated element |
| 108 | + * @param annotationType the annotation type to find. Only items of the specified type |
| 109 | + * or meta-annotations of the specified type will be processed |
| 110 | + * @param processor the processor |
| 111 | + * @return the result of the processor |
| 112 | + */ |
| 113 | + private static <T> T process(AnnotatedElement element, String annotationType, |
| 114 | + Processor<T> processor) { |
| 115 | + return recursivelyProcess(element, annotationType, processor, |
| 116 | + new HashSet<AnnotatedElement>(), 0); |
| 117 | + } |
| 118 | + |
| 119 | + private static <T> T recursivelyProcess(AnnotatedElement element, |
| 120 | + String annotationType, Processor<T> processor, Set<AnnotatedElement> visited, |
| 121 | + int depth) { |
| 122 | + T result = null; |
| 123 | + if (visited.add(element)) { |
| 124 | + for (Annotation annotation : element.getAnnotations()) { |
| 125 | + if (annotation.annotationType().getName().equals(annotationType) || depth > 0) { |
| 126 | + result = result != null ? result : |
| 127 | + processor.process(annotation, depth); |
| 128 | + result = result != null ? result : |
| 129 | + recursivelyProcess(annotation.annotationType(), annotationType, |
| 130 | + processor, visited, depth + 1); |
| 131 | + } |
| 132 | + } |
| 133 | + for (Annotation annotation : element.getAnnotations()) { |
| 134 | + result = result != null ? result : |
| 135 | + recursivelyProcess(annotation.annotationType(), annotationType, |
| 136 | + processor, visited, depth); |
| 137 | + } |
| 138 | + |
| 139 | + } |
| 140 | + return result; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Callback interface used to process an annotation. |
| 145 | + * @param <T> the result type |
| 146 | + */ |
| 147 | + private static interface Processor<T> { |
| 148 | + |
| 149 | + /** |
| 150 | + * Called to process the annotation. |
| 151 | + * @param annotation the annotation to process |
| 152 | + * @param depth the depth of the annotation relative to the initial match. For |
| 153 | + * example a matched annotation will have a depth of 0, a meta-annotation 1 |
| 154 | + * and a meta-meta-annotation 2 |
| 155 | + * @return the result of the processing or {@code null} to continue |
| 156 | + */ |
| 157 | + T process(Annotation annotation, int depth); |
| 158 | + } |
| 159 | + |
| 160 | +} |
0 commit comments