|
| 1 | +/* |
| 2 | + * Copyright 2002-2025 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 | + * https://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.annotation; |
| 18 | + |
| 19 | +import java.io.Serializable; |
| 20 | +import java.lang.annotation.Annotation; |
| 21 | +import java.lang.reflect.AnnotatedElement; |
| 22 | +import java.util.Arrays; |
| 23 | + |
| 24 | +import org.jspecify.annotations.Nullable; |
| 25 | + |
| 26 | +/** |
| 27 | + * Adapter for exposing a set of annotations as an {@link AnnotatedElement}, in |
| 28 | + * particular as input for various methods in {@link AnnotatedElementUtils}. |
| 29 | + * |
| 30 | + * @author Juergen Hoeller |
| 31 | + * @author Sam Brannen |
| 32 | + * @since 7.0 |
| 33 | + * @see #from(Annotation[]) |
| 34 | + * @see AnnotatedElementUtils#isAnnotated(AnnotatedElement, Class) |
| 35 | + * @see AnnotatedElementUtils#getMergedAnnotation(AnnotatedElement, Class) |
| 36 | + */ |
| 37 | +@SuppressWarnings("serial") |
| 38 | +public final class AnnotatedElementAdapter implements AnnotatedElement, Serializable { |
| 39 | + |
| 40 | + private static final AnnotatedElementAdapter EMPTY = new AnnotatedElementAdapter(new Annotation[0]); |
| 41 | + |
| 42 | + |
| 43 | + /** |
| 44 | + * Create an {@code AnnotatedElementAdapter} from the supplied annotations. |
| 45 | + * <p>The supplied annotations will be considered to be both <em>present</em> |
| 46 | + * and <em>directly present</em> with regard to the results returned from |
| 47 | + * methods such as {@link #getAnnotation(Class)}, |
| 48 | + * {@link #getDeclaredAnnotation(Class)}, etc. |
| 49 | + * <p>If the supplied annotations array is either {@code null} or empty, this |
| 50 | + * factory method will return an {@linkplain #isEmpty() empty} adapter. |
| 51 | + * @param annotations the annotations to expose via the {@link AnnotatedElement} |
| 52 | + * API |
| 53 | + * @return a new {@code AnnotatedElementAdapter} |
| 54 | + */ |
| 55 | + public static AnnotatedElementAdapter from(Annotation @Nullable [] annotations) { |
| 56 | + if (annotations == null || annotations.length == 0) { |
| 57 | + return EMPTY; |
| 58 | + } |
| 59 | + return new AnnotatedElementAdapter(annotations); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + private final Annotation[] annotations; |
| 64 | + |
| 65 | + |
| 66 | + private AnnotatedElementAdapter(Annotation[] annotations) { |
| 67 | + this.annotations = annotations; |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + @Override |
| 72 | + public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) { |
| 73 | + for (Annotation annotation : this.annotations) { |
| 74 | + if (annotation.annotationType() == annotationClass) { |
| 75 | + return true; |
| 76 | + } |
| 77 | + } |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public <A extends Annotation> @Nullable A getAnnotation(Class<A> annotationClass) { |
| 83 | + for (Annotation annotation : this.annotations) { |
| 84 | + if (annotation.annotationType() == annotationClass) { |
| 85 | + return annotationClass.cast(annotation); |
| 86 | + } |
| 87 | + } |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public Annotation[] getAnnotations() { |
| 93 | + return (isEmpty() ? this.annotations : this.annotations.clone()); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public <A extends Annotation> @Nullable A getDeclaredAnnotation(Class<A> annotationClass) { |
| 98 | + return getAnnotation(annotationClass); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public Annotation[] getDeclaredAnnotations() { |
| 103 | + return getAnnotations(); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Determine if this {@code AnnotatedElementAdapter} is empty. |
| 108 | + * @return {@code true} if this adapter contains no annotations |
| 109 | + */ |
| 110 | + public boolean isEmpty() { |
| 111 | + return (this == EMPTY); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public boolean equals(@Nullable Object other) { |
| 116 | + return (this == other || (other instanceof AnnotatedElementAdapter that && |
| 117 | + Arrays.equals(this.annotations, that.annotations))); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public int hashCode() { |
| 122 | + return Arrays.hashCode(this.annotations); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public String toString() { |
| 127 | + return Arrays.toString(this.annotations); |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments