Skip to content

Commit e84e6db

Browse files
philwebbsnicoll
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 spring-projectsgh-21697
1 parent 71e76c8 commit e84e6db

35 files changed

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

0 commit comments

Comments
 (0)