Skip to content

Commit 4972d85

Browse files
philwebbjhoeller
authored andcommitted
Create a new API for handling merged annotations
Add new `MergedAnnotations` and `MergedAnnotation` interfaces that attempt to provide a uniform way for dealing with merged annotations. Specifically, the new API provides alternatives for the static methods in `AnnotationUtils` and `AnnotatedElementUtils` and supports Spring's comprehensive annotation attribute `@AliasFor` features. The interfaces also open the possibility of the same API being exposed from the `AnnotationMetadata` interface. Additional utility classes for collecting, filtering and selecting annotations have also been added. Typical usage for the new API would be something like: MergedAnnotations.from(Example.class) .stream(MyAnnotation.class) .map(a -> a.getString("value")) .forEach(System.out::println); Closes gh-21697
1 parent fdacda8 commit 4972d85

35 files changed

+13436
-0
lines changed
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
/*
2+
* Copyright 2002-2019 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.annotation;
18+
19+
import java.lang.annotation.Annotation;
20+
import java.lang.reflect.Array;
21+
import java.util.Map;
22+
import java.util.NoSuchElementException;
23+
import java.util.Optional;
24+
import java.util.function.Predicate;
25+
26+
import org.springframework.lang.Nullable;
27+
import org.springframework.util.Assert;
28+
29+
/**
30+
* Abstract base class for {@link MergedAnnotation} implementations.
31+
*
32+
* @author Phillip Webb
33+
* @since 5.2
34+
* @param <A> the annotation type
35+
*/
36+
abstract class AbstractMergedAnnotation<A extends Annotation>
37+
implements MergedAnnotation<A> {
38+
39+
@Nullable
40+
private volatile A synthesizedAnnotation;
41+
42+
43+
@Override
44+
public boolean isDirectlyPresent() {
45+
return isPresent() && getDepth() == 0;
46+
}
47+
48+
@Override
49+
public boolean isMetaPresent() {
50+
return isPresent() && getDepth() > 0;
51+
}
52+
53+
@Override
54+
public boolean hasNonDefaultValue(String attributeName) {
55+
return !hasDefaultValue(attributeName);
56+
}
57+
58+
public byte getByte(String attributeName) {
59+
return getRequiredAttributeValue(attributeName, Byte.class);
60+
}
61+
62+
public byte[] getByteArray(String attributeName) {
63+
return getRequiredAttributeValue(attributeName, byte[].class);
64+
}
65+
66+
public boolean getBoolean(String attributeName) {
67+
return getRequiredAttributeValue(attributeName, Boolean.class);
68+
}
69+
70+
public boolean[] getBooleanArray(String attributeName) {
71+
return getRequiredAttributeValue(attributeName, boolean[].class);
72+
}
73+
74+
public char getChar(String attributeName) {
75+
return getRequiredAttributeValue(attributeName, Character.class);
76+
}
77+
78+
public char[] getCharArray(String attributeName) {
79+
return getRequiredAttributeValue(attributeName, char[].class);
80+
}
81+
82+
public short getShort(String attributeName) {
83+
return getRequiredAttributeValue(attributeName, Short.class);
84+
}
85+
86+
public short[] getShortArray(String attributeName) {
87+
return getRequiredAttributeValue(attributeName, short[].class);
88+
}
89+
90+
public int getInt(String attributeName) {
91+
return getRequiredAttributeValue(attributeName, Integer.class);
92+
}
93+
94+
public int[] getIntArray(String attributeName) {
95+
return getRequiredAttributeValue(attributeName, int[].class);
96+
}
97+
98+
public long getLong(String attributeName) {
99+
return getRequiredAttributeValue(attributeName, Long.class);
100+
}
101+
102+
public long[] getLongArray(String attributeName) {
103+
return getRequiredAttributeValue(attributeName, long[].class);
104+
}
105+
106+
public double getDouble(String attributeName) {
107+
return getRequiredAttributeValue(attributeName, Double.class);
108+
}
109+
110+
public double[] getDoubleArray(String attributeName) {
111+
return getRequiredAttributeValue(attributeName, double[].class);
112+
}
113+
114+
public float getFloat(String attributeName) {
115+
return getRequiredAttributeValue(attributeName, Float.class);
116+
}
117+
118+
public float[] getFloatArray(String attributeName) {
119+
return getRequiredAttributeValue(attributeName, float[].class);
120+
}
121+
122+
public String getString(String attributeName) {
123+
return getRequiredAttributeValue(attributeName, String.class);
124+
}
125+
126+
public String[] getStringArray(String attributeName) {
127+
return getRequiredAttributeValue(attributeName, String[].class);
128+
}
129+
130+
public Class<?> getClass(String attributeName) {
131+
return getRequiredAttributeValue(attributeName, Class.class);
132+
}
133+
134+
public Class<?>[] getClassArray(String attributeName) {
135+
return getRequiredAttributeValue(attributeName, Class[].class);
136+
}
137+
138+
public <E extends Enum<E>> E getEnum(String attributeName, Class<E> type) {
139+
Assert.notNull(type, "Type must not be null");
140+
return getRequiredAttributeValue(attributeName, type);
141+
}
142+
143+
@SuppressWarnings("unchecked")
144+
public <E extends Enum<E>> E[] getEnumArray(String attributeName, Class<E> type) {
145+
Assert.notNull(type, "Type must not be null");
146+
Class<?> arrayType = Array.newInstance(type, 0).getClass();
147+
return (E[]) getRequiredAttributeValue(attributeName, arrayType);
148+
}
149+
150+
@Override
151+
public Optional<Object> getValue(String attributeName) {
152+
return getValue(attributeName, Object.class);
153+
}
154+
155+
@Override
156+
public <T> Optional<T> getValue(String attributeName, Class<T> type) {
157+
return Optional.ofNullable(getAttributeValue(attributeName, type));
158+
}
159+
160+
@Override
161+
public Optional<Object> getDefaultValue(String attributeName) {
162+
return getDefaultValue(attributeName, Object.class);
163+
}
164+
165+
@Override
166+
public MergedAnnotation<A> filterDefaultValues() {
167+
return filterAttributes(this::hasNonDefaultValue);
168+
}
169+
170+
@Override
171+
public Map<String, Object> asMap(MapValues... options) {
172+
return asMap(null, options);
173+
}
174+
175+
@Override
176+
public Optional<A> synthesize(
177+
@Nullable Predicate<? super MergedAnnotation<A>> condition)
178+
throws NoSuchElementException {
179+
180+
if (condition == null || condition.test(this)) {
181+
return Optional.of(synthesize());
182+
}
183+
return Optional.empty();
184+
}
185+
186+
@Override
187+
public A synthesize() {
188+
if (!isPresent()) {
189+
throw new NoSuchElementException("Unable to synthesize missing annotation");
190+
}
191+
A synthesized = this.synthesizedAnnotation;
192+
if (synthesized == null) {
193+
synthesized = createSynthesized();
194+
this.synthesizedAnnotation = synthesized;
195+
}
196+
return synthesized;
197+
}
198+
199+
private <T> T getRequiredAttributeValue(String attributeName, Class<T> type) {
200+
T value = getAttributeValue(attributeName, type);
201+
if (value == null) {
202+
throw new NoSuchElementException("No attribute named '" + attributeName +
203+
"' present in merged annotation " + getType());
204+
}
205+
return value;
206+
}
207+
208+
/**
209+
* Get the underlying attribute value.
210+
* @param attributeName the attribute name
211+
* @param type the type to return (see {@link MergedAnnotation} class
212+
* documentation for details).
213+
* @return the attribute value or {@code null} if the value is not found and
214+
* is not required
215+
* @throws IllegalArgumentException if the source type is not compatible
216+
* @throws NoSuchElementException if the value is required but not found
217+
*/
218+
@Nullable
219+
protected abstract <T> T getAttributeValue(String attributeName, Class<T> type);
220+
221+
/**
222+
* Factory method used to create the synthesized annotation.
223+
*/
224+
protected abstract A createSynthesized();
225+
226+
}

0 commit comments

Comments
 (0)