AnnotationUtilsTests.java 59.7 KB
Newer Older
A
Arjen Poutsma 已提交
1
/*
2
 * Copyright 2002-2015 the original author or authors.
A
Arjen Poutsma 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.core.annotation;

19
import java.lang.annotation.Annotation;
20
import java.lang.annotation.Inherited;
21
import java.lang.annotation.Repeatable;
22 23
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
24
import java.lang.annotation.Target;
A
Arjen Poutsma 已提交
25
import java.lang.reflect.Method;
26
import java.util.Arrays;
27
import java.util.Collections;
28
import java.util.HashMap;
29
import java.util.List;
30
import java.util.Map;
31
import java.util.Set;
32

33
import org.junit.Rule;
34
import org.junit.Test;
35
import org.junit.rules.ExpectedException;
J
Juergen Hoeller 已提交
36

A
Arjen Poutsma 已提交
37
import org.springframework.core.Ordered;
38
import org.springframework.core.annotation.subpackage.NonPublicAnnotatedClass;
39
import org.springframework.stereotype.Component;
40
import org.springframework.util.ClassUtils;
A
Arjen Poutsma 已提交
41

42
import static java.util.Arrays.*;
S
Sam Brannen 已提交
43
import static java.util.stream.Collectors.*;
44
import static org.hamcrest.Matchers.*;
45 46 47
import static org.junit.Assert.*;
import static org.springframework.core.annotation.AnnotationUtils.*;

A
Arjen Poutsma 已提交
48
/**
J
Juergen Hoeller 已提交
49 50
 * Unit tests for {@link AnnotationUtils}.
 *
A
Arjen Poutsma 已提交
51 52 53
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @author Sam Brannen
54
 * @author Chris Beams
55
 * @author Phillip Webb
A
Arjen Poutsma 已提交
56
 */
57
public class AnnotationUtilsTests {
A
Arjen Poutsma 已提交
58

59 60
	private static final Map<String, Object> EMPTY_ATTRS = Collections.emptyMap();

61 62 63
	@Rule
	public final ExpectedException exception = ExpectedException.none();

64
	@Test
65
	public void findMethodAnnotationOnLeaf() throws Exception {
66
		Method m = Leaf.class.getMethod("annotatedOnLeaf");
A
Arjen Poutsma 已提交
67 68 69 70 71
		assertNotNull(m.getAnnotation(Order.class));
		assertNotNull(getAnnotation(m, Order.class));
		assertNotNull(findAnnotation(m, Order.class));
	}

72 73 74 75 76 77 78 79 80 81 82 83 84
	/** @since 4.2 */
	@Test
	public void findMethodAnnotationWithAnnotationOnMethodInInterface() throws Exception {
		Method m = Leaf.class.getMethod("fromInterfaceImplementedByRoot");
		// @Order is not @Inherited
		assertNull(m.getAnnotation(Order.class));
		// getAnnotation() does not search on interfaces
		assertNull(getAnnotation(m, Order.class));
		// findAnnotation() does search on interfaces
		assertNotNull(findAnnotation(m, Order.class));
	}

	/** @since 4.2 */
85 86 87 88 89 90 91 92
	@Test
	public void findMethodAnnotationWithMetaAnnotationOnLeaf() throws Exception {
		Method m = Leaf.class.getMethod("metaAnnotatedOnLeaf");
		assertNull(m.getAnnotation(Order.class));
		assertNotNull(getAnnotation(m, Order.class));
		assertNotNull(findAnnotation(m, Order.class));
	}

93
	/** @since 4.2 */
94 95 96 97 98 99 100 101
	@Test
	public void findMethodAnnotationWithMetaMetaAnnotationOnLeaf() throws Exception {
		Method m = Leaf.class.getMethod("metaMetaAnnotatedOnLeaf");
		assertNull(m.getAnnotation(Component.class));
		assertNull(getAnnotation(m, Component.class));
		assertNotNull(findAnnotation(m, Component.class));
	}

102
	@Test
103
	public void findMethodAnnotationOnRoot() throws Exception {
104
		Method m = Leaf.class.getMethod("annotatedOnRoot");
A
Arjen Poutsma 已提交
105 106 107 108 109
		assertNotNull(m.getAnnotation(Order.class));
		assertNotNull(getAnnotation(m, Order.class));
		assertNotNull(findAnnotation(m, Order.class));
	}

110
	/** @since 4.2 */
111 112 113 114 115 116 117 118
	@Test
	public void findMethodAnnotationWithMetaAnnotationOnRoot() throws Exception {
		Method m = Leaf.class.getMethod("metaAnnotatedOnRoot");
		assertNull(m.getAnnotation(Order.class));
		assertNotNull(getAnnotation(m, Order.class));
		assertNotNull(findAnnotation(m, Order.class));
	}

119
	@Test
120
	public void findMethodAnnotationOnRootButOverridden() throws Exception {
121
		Method m = Leaf.class.getMethod("overrideWithoutNewAnnotation");
A
Arjen Poutsma 已提交
122 123 124 125 126
		assertNull(m.getAnnotation(Order.class));
		assertNull(getAnnotation(m, Order.class));
		assertNotNull(findAnnotation(m, Order.class));
	}

127
	@Test
128
	public void findMethodAnnotationNotAnnotated() throws Exception {
129
		Method m = Leaf.class.getMethod("notAnnotated");
A
Arjen Poutsma 已提交
130 131 132
		assertNull(findAnnotation(m, Order.class));
	}

133
	@Test
134
	public void findMethodAnnotationOnBridgeMethod() throws Exception {
135
		Method m = SimpleFoo.class.getMethod("something", Object.class);
A
Arjen Poutsma 已提交
136 137 138 139
		assertTrue(m.isBridge());
		assertNull(m.getAnnotation(Order.class));
		assertNull(getAnnotation(m, Order.class));
		assertNotNull(findAnnotation(m, Order.class));
140
		// TODO: getAnnotation() on bridge method actually found on OpenJDK 8 b99 and higher!
141
		// assertNull(m.getAnnotation(Transactional.class));
A
Arjen Poutsma 已提交
142 143 144 145
		assertNotNull(getAnnotation(m, Transactional.class));
		assertNotNull(findAnnotation(m, Transactional.class));
	}

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
	@Test
	public void findMethodAnnotationFromInterface() throws Exception {
		Method method = ImplementsInterfaceWithAnnotatedMethod.class.getMethod("foo");
		Order order = findAnnotation(method, Order.class);
		assertNotNull(order);
	}

	@Test
	public void findMethodAnnotationFromInterfaceOnSuper() throws Exception {
		Method method = SubOfImplementsInterfaceWithAnnotatedMethod.class.getMethod("foo");
		Order order = findAnnotation(method, Order.class);
		assertNotNull(order);
	}

	@Test
	public void findMethodAnnotationFromInterfaceWhenSuperDoesNotImplementMethod() throws Exception {
		Method method = SubOfAbstractImplementsInterfaceWithAnnotatedMethod.class.getMethod("foo");
		Order order = findAnnotation(method, Order.class);
		assertNotNull(order);
	}
A
Arjen Poutsma 已提交
166

167
	/** @since 4.1.2 */
168
	@Test
169
	public void findClassAnnotationFavorsMoreLocallyDeclaredComposedAnnotationsOverAnnotationsOnInterfaces() {
170 171
		Component component = findAnnotation(ClassWithLocalMetaAnnotationAndMetaAnnotatedInterface.class,
			Component.class);
172
		assertNotNull(component);
173
		assertEquals("meta2", component.value());
174 175
	}

176
	/** @since 4.0.3 */
177
	@Test
178
	public void findClassAnnotationFavorsMoreLocallyDeclaredComposedAnnotationsOverInheritedAnnotations() {
179
		Transactional transactional = findAnnotation(SubSubClassWithInheritedAnnotation.class, Transactional.class);
180
		assertNotNull(transactional);
181
		assertTrue("readOnly flag for SubSubClassWithInheritedAnnotation", transactional.readOnly());
182 183
	}

184
	/** @since 4.0.3 */
185
	@Test
186
	public void findClassAnnotationFavorsMoreLocallyDeclaredComposedAnnotationsOverInheritedComposedAnnotations() {
187
		Component component = findAnnotation(SubSubClassWithInheritedMetaAnnotation.class, Component.class);
188
		assertNotNull(component);
189
		assertEquals("meta2", component.value());
190 191
	}

192
	@Test
193
	public void findClassAnnotationOnMetaMetaAnnotatedClass() {
194
		Component component = findAnnotation(MetaMetaAnnotatedClass.class, Component.class);
195 196 197 198 199
		assertNotNull("Should find meta-annotation on composed annotation on class", component);
		assertEquals("meta2", component.value());
	}

	@Test
200
	public void findClassAnnotationOnMetaMetaMetaAnnotatedClass() {
201
		Component component = findAnnotation(MetaMetaMetaAnnotatedClass.class, Component.class);
202 203 204 205 206
		assertNotNull("Should find meta-annotation on meta-annotation on composed annotation on class", component);
		assertEquals("meta2", component.value());
	}

	@Test
207
	public void findClassAnnotationOnAnnotatedClassWithMissingTargetMetaAnnotation() {
208
		// TransactionalClass is NOT annotated or meta-annotated with @Component
209
		Component component = findAnnotation(TransactionalClass.class, Component.class);
210 211 212 213
		assertNull("Should not find @Component on TransactionalClass", component);
	}

	@Test
214
	public void findClassAnnotationOnMetaCycleAnnotatedClassWithMissingTargetMetaAnnotation() {
215
		Component component = findAnnotation(MetaCycleAnnotatedClass.class, Component.class);
216 217 218
		assertNull("Should not find @Component on MetaCycleAnnotatedClass", component);
	}

219 220 221
	/** @since 4.2 */
	@Test
	public void findClassAnnotationOnInheritedAnnotationInterface() {
222
		Transactional tx = findAnnotation(InheritedAnnotationInterface.class, Transactional.class);
223 224 225 226 227 228
		assertNotNull("Should find @Transactional on InheritedAnnotationInterface", tx);
	}

	/** @since 4.2 */
	@Test
	public void findClassAnnotationOnSubInheritedAnnotationInterface() {
229
		Transactional tx = findAnnotation(SubInheritedAnnotationInterface.class, Transactional.class);
230 231 232 233 234 235
		assertNotNull("Should find @Transactional on SubInheritedAnnotationInterface", tx);
	}

	/** @since 4.2 */
	@Test
	public void findClassAnnotationOnSubSubInheritedAnnotationInterface() {
236
		Transactional tx = findAnnotation(SubSubInheritedAnnotationInterface.class, Transactional.class);
237 238 239 240 241 242
		assertNotNull("Should find @Transactional on SubSubInheritedAnnotationInterface", tx);
	}

	/** @since 4.2 */
	@Test
	public void findClassAnnotationOnNonInheritedAnnotationInterface() {
243
		Order order = findAnnotation(NonInheritedAnnotationInterface.class, Order.class);
244 245 246 247 248 249
		assertNotNull("Should find @Order on NonInheritedAnnotationInterface", order);
	}

	/** @since 4.2 */
	@Test
	public void findClassAnnotationOnSubNonInheritedAnnotationInterface() {
250
		Order order = findAnnotation(SubNonInheritedAnnotationInterface.class, Order.class);
251 252 253 254 255 256
		assertNotNull("Should find @Order on SubNonInheritedAnnotationInterface", order);
	}

	/** @since 4.2 */
	@Test
	public void findClassAnnotationOnSubSubNonInheritedAnnotationInterface() {
257
		Order order = findAnnotation(SubSubNonInheritedAnnotationInterface.class, Order.class);
258 259 260
		assertNotNull("Should find @Order on SubSubNonInheritedAnnotationInterface", order);
	}

261
	@Test
262
	public void findAnnotationDeclaringClassForAllScenarios() throws Exception {
A
Arjen Poutsma 已提交
263 264 265 266 267
		// no class-level annotation
		assertNull(findAnnotationDeclaringClass(Transactional.class, NonAnnotatedInterface.class));
		assertNull(findAnnotationDeclaringClass(Transactional.class, NonAnnotatedClass.class));

		// inherited class-level annotation; note: @Transactional is inherited
268 269
		assertEquals(InheritedAnnotationInterface.class,
			findAnnotationDeclaringClass(Transactional.class, InheritedAnnotationInterface.class));
A
Arjen Poutsma 已提交
270
		assertNull(findAnnotationDeclaringClass(Transactional.class, SubInheritedAnnotationInterface.class));
271 272 273 274
		assertEquals(InheritedAnnotationClass.class,
			findAnnotationDeclaringClass(Transactional.class, InheritedAnnotationClass.class));
		assertEquals(InheritedAnnotationClass.class,
			findAnnotationDeclaringClass(Transactional.class, SubInheritedAnnotationClass.class));
A
Arjen Poutsma 已提交
275 276

		// non-inherited class-level annotation; note: @Order is not inherited,
277 278 279
		// but findAnnotationDeclaringClass() should still find it on classes.
		assertEquals(NonInheritedAnnotationInterface.class,
			findAnnotationDeclaringClass(Order.class, NonInheritedAnnotationInterface.class));
A
Arjen Poutsma 已提交
280
		assertNull(findAnnotationDeclaringClass(Order.class, SubNonInheritedAnnotationInterface.class));
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
		assertEquals(NonInheritedAnnotationClass.class,
			findAnnotationDeclaringClass(Order.class, NonInheritedAnnotationClass.class));
		assertEquals(NonInheritedAnnotationClass.class,
			findAnnotationDeclaringClass(Order.class, SubNonInheritedAnnotationClass.class));
	}

	@Test
	public void findAnnotationDeclaringClassForTypesWithSingleCandidateType() {
		// no class-level annotation
		List<Class<? extends Annotation>> transactionalCandidateList = Arrays.<Class<? extends Annotation>> asList(Transactional.class);
		assertNull(findAnnotationDeclaringClassForTypes(transactionalCandidateList, NonAnnotatedInterface.class));
		assertNull(findAnnotationDeclaringClassForTypes(transactionalCandidateList, NonAnnotatedClass.class));

		// inherited class-level annotation; note: @Transactional is inherited
		assertEquals(InheritedAnnotationInterface.class,
			findAnnotationDeclaringClassForTypes(transactionalCandidateList, InheritedAnnotationInterface.class));
J
Juergen Hoeller 已提交
297
		assertNull(findAnnotationDeclaringClassForTypes(transactionalCandidateList, SubInheritedAnnotationInterface.class));
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
		assertEquals(InheritedAnnotationClass.class,
			findAnnotationDeclaringClassForTypes(transactionalCandidateList, InheritedAnnotationClass.class));
		assertEquals(InheritedAnnotationClass.class,
			findAnnotationDeclaringClassForTypes(transactionalCandidateList, SubInheritedAnnotationClass.class));

		// non-inherited class-level annotation; note: @Order is not inherited,
		// but findAnnotationDeclaringClassForTypes() should still find it on classes.
		List<Class<? extends Annotation>> orderCandidateList = Arrays.<Class<? extends Annotation>> asList(Order.class);
		assertEquals(NonInheritedAnnotationInterface.class,
			findAnnotationDeclaringClassForTypes(orderCandidateList, NonInheritedAnnotationInterface.class));
		assertNull(findAnnotationDeclaringClassForTypes(orderCandidateList, SubNonInheritedAnnotationInterface.class));
		assertEquals(NonInheritedAnnotationClass.class,
			findAnnotationDeclaringClassForTypes(orderCandidateList, NonInheritedAnnotationClass.class));
		assertEquals(NonInheritedAnnotationClass.class,
			findAnnotationDeclaringClassForTypes(orderCandidateList, SubNonInheritedAnnotationClass.class));
	}

	@Test
	public void findAnnotationDeclaringClassForTypesWithMultipleCandidateTypes() {
317
		List<Class<? extends Annotation>> candidates = Arrays.<Class<? extends Annotation>> asList(Transactional.class, Order.class);
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348

		// no class-level annotation
		assertNull(findAnnotationDeclaringClassForTypes(candidates, NonAnnotatedInterface.class));
		assertNull(findAnnotationDeclaringClassForTypes(candidates, NonAnnotatedClass.class));

		// inherited class-level annotation; note: @Transactional is inherited
		assertEquals(InheritedAnnotationInterface.class,
			findAnnotationDeclaringClassForTypes(candidates, InheritedAnnotationInterface.class));
		assertNull(findAnnotationDeclaringClassForTypes(candidates, SubInheritedAnnotationInterface.class));
		assertEquals(InheritedAnnotationClass.class,
			findAnnotationDeclaringClassForTypes(candidates, InheritedAnnotationClass.class));
		assertEquals(InheritedAnnotationClass.class,
			findAnnotationDeclaringClassForTypes(candidates, SubInheritedAnnotationClass.class));

		// non-inherited class-level annotation; note: @Order is not inherited,
		// but findAnnotationDeclaringClassForTypes() should still find it on classes.
		assertEquals(NonInheritedAnnotationInterface.class,
			findAnnotationDeclaringClassForTypes(candidates, NonInheritedAnnotationInterface.class));
		assertNull(findAnnotationDeclaringClassForTypes(candidates, SubNonInheritedAnnotationInterface.class));
		assertEquals(NonInheritedAnnotationClass.class,
			findAnnotationDeclaringClassForTypes(candidates, NonInheritedAnnotationClass.class));
		assertEquals(NonInheritedAnnotationClass.class,
			findAnnotationDeclaringClassForTypes(candidates, SubNonInheritedAnnotationClass.class));

		// class hierarchy mixed with @Transactional and @Order declarations
		assertEquals(TransactionalClass.class,
			findAnnotationDeclaringClassForTypes(candidates, TransactionalClass.class));
		assertEquals(TransactionalAndOrderedClass.class,
			findAnnotationDeclaringClassForTypes(candidates, TransactionalAndOrderedClass.class));
		assertEquals(TransactionalAndOrderedClass.class,
			findAnnotationDeclaringClassForTypes(candidates, SubTransactionalAndOrderedClass.class));
A
Arjen Poutsma 已提交
349 350
	}

351
	@Test
352
	public void isAnnotationDeclaredLocallyForAllScenarios() throws Exception {
A
Arjen Poutsma 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
		// no class-level annotation
		assertFalse(isAnnotationDeclaredLocally(Transactional.class, NonAnnotatedInterface.class));
		assertFalse(isAnnotationDeclaredLocally(Transactional.class, NonAnnotatedClass.class));

		// inherited class-level annotation; note: @Transactional is inherited
		assertTrue(isAnnotationDeclaredLocally(Transactional.class, InheritedAnnotationInterface.class));
		assertFalse(isAnnotationDeclaredLocally(Transactional.class, SubInheritedAnnotationInterface.class));
		assertTrue(isAnnotationDeclaredLocally(Transactional.class, InheritedAnnotationClass.class));
		assertFalse(isAnnotationDeclaredLocally(Transactional.class, SubInheritedAnnotationClass.class));

		// non-inherited class-level annotation; note: @Order is not inherited
		assertTrue(isAnnotationDeclaredLocally(Order.class, NonInheritedAnnotationInterface.class));
		assertFalse(isAnnotationDeclaredLocally(Order.class, SubNonInheritedAnnotationInterface.class));
		assertTrue(isAnnotationDeclaredLocally(Order.class, NonInheritedAnnotationClass.class));
		assertFalse(isAnnotationDeclaredLocally(Order.class, SubNonInheritedAnnotationClass.class));
	}

370
	@Test
371
	public void isAnnotationInheritedForAllScenarios() throws Exception {
A
Arjen Poutsma 已提交
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
		// no class-level annotation
		assertFalse(isAnnotationInherited(Transactional.class, NonAnnotatedInterface.class));
		assertFalse(isAnnotationInherited(Transactional.class, NonAnnotatedClass.class));

		// inherited class-level annotation; note: @Transactional is inherited
		assertFalse(isAnnotationInherited(Transactional.class, InheritedAnnotationInterface.class));
		// isAnnotationInherited() does not currently traverse interface
		// hierarchies. Thus the following, though perhaps counter intuitive,
		// must be false:
		assertFalse(isAnnotationInherited(Transactional.class, SubInheritedAnnotationInterface.class));
		assertFalse(isAnnotationInherited(Transactional.class, InheritedAnnotationClass.class));
		assertTrue(isAnnotationInherited(Transactional.class, SubInheritedAnnotationClass.class));

		// non-inherited class-level annotation; note: @Order is not inherited
		assertFalse(isAnnotationInherited(Order.class, NonInheritedAnnotationInterface.class));
		assertFalse(isAnnotationInherited(Order.class, SubNonInheritedAnnotationInterface.class));
		assertFalse(isAnnotationInherited(Order.class, NonInheritedAnnotationClass.class));
		assertFalse(isAnnotationInherited(Order.class, SubNonInheritedAnnotationClass.class));
	}

392 393 394 395 396 397 398 399 400 401 402
	@Test
	public void getAnnotationAttributesWithoutAttributeAliases() {
		Component component = WebController.class.getAnnotation(Component.class);
		assertNotNull(component);

		AnnotationAttributes attributes = (AnnotationAttributes) getAnnotationAttributes(component);
		assertNotNull(attributes);
		assertEquals("value attribute: ", "webController", attributes.getString(VALUE));
		assertEquals(Component.class, attributes.annotationType());
	}

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
	@Test
	public void getAnnotationAttributesWithNestedAnnotations() {
		ComponentScan componentScan = ComponentScanClass.class.getAnnotation(ComponentScan.class);
		assertNotNull(componentScan);

		AnnotationAttributes attributes = getAnnotationAttributes(ComponentScanClass.class, componentScan);
		assertNotNull(attributes);
		assertEquals(ComponentScan.class, attributes.annotationType());

		Filter[] filters = attributes.getAnnotationArray("excludeFilters", Filter.class);
		assertNotNull(filters);

		List<String> patterns = stream(filters).map(Filter::pattern).collect(toList());
		assertEquals(asList("*Foo", "*Bar"), patterns);
	}

419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
	@Test
	public void getAnnotationAttributesWithAttributeAliases() throws Exception {
		Method method = WebController.class.getMethod("handleMappedWithValueAttribute");
		WebMapping webMapping = method.getAnnotation(WebMapping.class);
		AnnotationAttributes attributes = (AnnotationAttributes) getAnnotationAttributes(webMapping);
		assertNotNull(attributes);
		assertEquals(WebMapping.class, attributes.annotationType());
		assertEquals("name attribute: ", "foo", attributes.getString("name"));
		assertEquals("value attribute: ", "/test", attributes.getString(VALUE));
		assertEquals("path attribute: ", "/test", attributes.getString("path"));

		method = WebController.class.getMethod("handleMappedWithPathAttribute");
		webMapping = method.getAnnotation(WebMapping.class);
		attributes = (AnnotationAttributes) getAnnotationAttributes(webMapping);
		assertNotNull(attributes);
		assertEquals(WebMapping.class, attributes.annotationType());
		assertEquals("name attribute: ", "bar", attributes.getString("name"));
		assertEquals("value attribute: ", "/test", attributes.getString(VALUE));
		assertEquals("path attribute: ", "/test", attributes.getString("path"));

439
		method = WebController.class.getMethod("handleMappedWithDifferentPathAndValueAttributes");
440 441 442 443
		webMapping = method.getAnnotation(WebMapping.class);
		exception.expect(AnnotationConfigurationException.class);
		exception.expectMessage(containsString("attribute [value] and its alias [path]"));
		exception.expectMessage(containsString("values of [/enigma] and [/test]"));
444
		exception.expectMessage(containsString("but only one is permitted"));
445 446 447
		getAnnotationAttributes(webMapping);
	}

448
	@Test
449
	public void getValueFromAnnotation() throws Exception {
450 451
		Method method = SimpleFoo.class.getMethod("something", Object.class);
		Order order = findAnnotation(method, Order.class);
A
Arjen Poutsma 已提交
452

453 454
		assertEquals(1, getValue(order, VALUE));
		assertEquals(1, getValue(order));
A
Arjen Poutsma 已提交
455 456
	}

457
	@Test
458 459 460 461 462 463
	public void getValueFromNonPublicAnnotation() throws Exception {
		Annotation[] declaredAnnotations = NonPublicAnnotatedClass.class.getDeclaredAnnotations();
		assertEquals(1, declaredAnnotations.length);
		Annotation annotation = declaredAnnotations[0];
		assertNotNull(annotation);
		assertEquals("NonPublicAnnotation", annotation.annotationType().getSimpleName());
464 465
		assertEquals(42, getValue(annotation, VALUE));
		assertEquals(42, getValue(annotation));
466 467 468 469
	}

	@Test
	public void getDefaultValueFromAnnotation() throws Exception {
470 471
		Method method = SimpleFoo.class.getMethod("something", Object.class);
		Order order = findAnnotation(method, Order.class);
A
Arjen Poutsma 已提交
472

473 474
		assertEquals(Ordered.LOWEST_PRECEDENCE, getDefaultValue(order, VALUE));
		assertEquals(Ordered.LOWEST_PRECEDENCE, getDefaultValue(order));
A
Arjen Poutsma 已提交
475 476
	}

477
	@Test
478 479 480 481 482 483
	public void getDefaultValueFromNonPublicAnnotation() throws Exception {
		Annotation[] declaredAnnotations = NonPublicAnnotatedClass.class.getDeclaredAnnotations();
		assertEquals(1, declaredAnnotations.length);
		Annotation annotation = declaredAnnotations[0];
		assertNotNull(annotation);
		assertEquals("NonPublicAnnotation", annotation.annotationType().getSimpleName());
484 485
		assertEquals(-1, getDefaultValue(annotation, VALUE));
		assertEquals(-1, getDefaultValue(annotation));
486 487 488 489
	}

	@Test
	public void getDefaultValueFromAnnotationType() throws Exception {
490 491
		assertEquals(Ordered.LOWEST_PRECEDENCE, getDefaultValue(Order.class, VALUE));
		assertEquals(Ordered.LOWEST_PRECEDENCE, getDefaultValue(Order.class));
A
Arjen Poutsma 已提交
492 493
	}

494 495
	@Test
	public void findRepeatableAnnotationOnComposedAnnotation() {
496
		Repeatable repeatable = findAnnotation(MyRepeatableMeta1.class, Repeatable.class);
497 498 499 500
		assertNotNull(repeatable);
		assertEquals(MyRepeatableContainer.class, repeatable.value());
	}

501
	@Test
502
	public void getRepeatableAnnotationsDeclaredOnMethod() throws Exception {
503
		Method method = InterfaceWithRepeated.class.getMethod("foo");
S
Sam Brannen 已提交
504
		Set<MyRepeatable> annotations = getRepeatableAnnotations(method, MyRepeatable.class, MyRepeatableContainer.class);
505
		assertNotNull(annotations);
S
Sam Brannen 已提交
506
		List<String> values = annotations.stream().map(MyRepeatable::value).collect(toList());
507
		assertThat(values, is(Arrays.asList("A", "B", "C", "meta1")));
508 509
	}

510
	@Test
511
	public void getRepeatableAnnotationsDeclaredOnClassWithMissingAttributeAliasDeclaration() throws Exception {
512 513 514 515
		exception.expect(AnnotationConfigurationException.class);
		exception.expectMessage(containsString("Attribute [value] in"));
		exception.expectMessage(containsString(BrokenContextConfig.class.getName()));
		exception.expectMessage(containsString("must be declared as an @AliasFor [locations]"));
S
Sam Brannen 已提交
516
		getRepeatableAnnotations(BrokenConfigHierarchyTestCase.class, BrokenContextConfig.class, BrokenHierarchy.class);
517 518
	}

519
	@Test
520 521 522
	public void getRepeatableAnnotationsDeclaredOnClassWithAttributeAliases() throws Exception {
		final List<String> expectedLocations = Arrays.asList("A", "B");

S
Sam Brannen 已提交
523
		Set<ContextConfig> annotations = getRepeatableAnnotations(ConfigHierarchyTestCase.class, ContextConfig.class, Hierarchy.class);
524 525
		assertNotNull(annotations);

S
Sam Brannen 已提交
526
		List<String> locations = annotations.stream().map(ContextConfig::locations).collect(toList());
527
		assertThat(locations, is(expectedLocations));
528

S
Sam Brannen 已提交
529
		List<String> values = annotations.stream().map(ContextConfig::value).collect(toList());
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
		assertThat(values, is(expectedLocations));
	}

	@Test
	public void getRepeatableAnnotationsDeclaredOnClass() {
		final List<String> expectedValuesJava = Arrays.asList("A", "B", "C");
		final List<String> expectedValuesSpring = Arrays.asList("A", "B", "C", "meta1");

		// Java 8
		MyRepeatable[] array = MyRepeatableClass.class.getAnnotationsByType(MyRepeatable.class);
		assertNotNull(array);
		List<String> values = stream(array).map(MyRepeatable::value).collect(toList());
		assertThat(values, is(expectedValuesJava));

		// Spring
S
Sam Brannen 已提交
545
		Set<MyRepeatable> set = getRepeatableAnnotations(MyRepeatableClass.class, MyRepeatable.class, MyRepeatableContainer.class);
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
		assertNotNull(set);
		values = set.stream().map(MyRepeatable::value).collect(toList());
		assertThat(values, is(expectedValuesSpring));
	}

	@Test
	public void getRepeatableAnnotationsDeclaredOnSuperclass() {
		final Class<?> clazz = SubMyRepeatableClass.class;
		final List<String> expectedValuesJava = Arrays.asList("A", "B", "C");
		final List<String> expectedValuesSpring = Arrays.asList("A", "B", "C", "meta1");

		// Java 8
		MyRepeatable[] array = clazz.getAnnotationsByType(MyRepeatable.class);
		assertNotNull(array);
		List<String> values = stream(array).map(MyRepeatable::value).collect(toList());
		assertThat(values, is(expectedValuesJava));

		// Spring
S
Sam Brannen 已提交
564
		Set<MyRepeatable> set = getRepeatableAnnotations(clazz, MyRepeatable.class, MyRepeatableContainer.class);
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
		assertNotNull(set);
		values = set.stream().map(MyRepeatable::value).collect(toList());
		assertThat(values, is(expectedValuesSpring));
	}

	@Test
	public void getRepeatableAnnotationsDeclaredOnClassAndSuperclass() {
		final Class<?> clazz = SubMyRepeatableWithAdditionalLocalDeclarationsClass.class;
		final List<String> expectedValuesJava = Arrays.asList("X", "Y", "Z");
		final List<String> expectedValuesSpring = Arrays.asList("X", "Y", "Z", "meta2");

		// Java 8
		MyRepeatable[] array = clazz.getAnnotationsByType(MyRepeatable.class);
		assertNotNull(array);
		List<String> values = stream(array).map(MyRepeatable::value).collect(toList());
		assertThat(values, is(expectedValuesJava));

		// Spring
S
Sam Brannen 已提交
583
		Set<MyRepeatable> set = getRepeatableAnnotations(clazz, MyRepeatable.class, MyRepeatableContainer.class);
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
		assertNotNull(set);
		values = set.stream().map(MyRepeatable::value).collect(toList());
		assertThat(values, is(expectedValuesSpring));
	}

	@Test
	public void getDeclaredRepeatableAnnotationsDeclaredOnClass() {
		final List<String> expectedValuesJava = Arrays.asList("A", "B", "C");
		final List<String> expectedValuesSpring = Arrays.asList("A", "B", "C", "meta1");

		// Java 8
		MyRepeatable[] array = MyRepeatableClass.class.getDeclaredAnnotationsByType(MyRepeatable.class);
		assertNotNull(array);
		List<String> values = stream(array).map(MyRepeatable::value).collect(toList());
		assertThat(values, is(expectedValuesJava));

		// Spring
S
Sam Brannen 已提交
601
		Set<MyRepeatable> set = getDeclaredRepeatableAnnotations(MyRepeatableClass.class, MyRepeatable.class, MyRepeatableContainer.class);
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
		assertNotNull(set);
		values = set.stream().map(MyRepeatable::value).collect(toList());
		assertThat(values, is(expectedValuesSpring));
	}

	@Test
	public void getDeclaredRepeatableAnnotationsDeclaredOnSuperclass() {
		final Class<?> clazz = SubMyRepeatableClass.class;

		// Java 8
		MyRepeatable[] array = clazz.getDeclaredAnnotationsByType(MyRepeatable.class);
		assertNotNull(array);
		assertThat(array.length, is(0));

		// Spring
S
Sam Brannen 已提交
617
		Set<MyRepeatable> set = getDeclaredRepeatableAnnotations(clazz, MyRepeatable.class, MyRepeatableContainer.class);
618 619
		assertNotNull(set);
		assertThat(set.size(), is(0));
620 621 622 623 624 625 626 627 628 629
	}

	@Test
	public void getAliasedAttributeNameFromAliasedComposedAnnotation() throws Exception {
		Method attribute = AliasedComposedContextConfig.class.getDeclaredMethod("xmlConfigFile");
		assertEquals("locations", getAliasedAttributeName(attribute, ContextConfig.class));
	}

	@Test
	public void synthesizeAnnotationWithoutAttributeAliases() throws Exception {
630
		Component component = WebController.class.getAnnotation(Component.class);
631 632 633 634 635 636 637
		assertNotNull(component);
		Component synthesizedComponent = synthesizeAnnotation(component);
		assertNotNull(synthesizedComponent);
		assertSame(component, synthesizedComponent);
		assertEquals("value attribute: ", "webController", synthesizedComponent.value());
	}

638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
	@Test
	public void synthesizeAnnotationsFromNullSources() throws Exception {
		assertNull("null annotation", synthesizeAnnotation(null, null));
		assertNull("null map", synthesizeAnnotation(null, WebMapping.class, null));
	}

	@Test
	public void synthesizeAlreadySynthesizedAnnotation() throws Exception {
		Method method = WebController.class.getMethod("handleMappedWithValueAttribute");
		WebMapping webMapping = method.getAnnotation(WebMapping.class);
		assertNotNull(webMapping);
		WebMapping synthesizedWebMapping = synthesizeAnnotation(webMapping);
		assertNotSame(webMapping, synthesizedWebMapping);
		WebMapping synthesizedAgainWebMapping = synthesizeAnnotation(synthesizedWebMapping);
		assertSame(synthesizedWebMapping, synthesizedAgainWebMapping);
		assertThat(synthesizedAgainWebMapping, instanceOf(SynthesizedAnnotation.class));

		assertNotNull(synthesizedAgainWebMapping);
		assertEquals("name attribute: ", "foo", synthesizedAgainWebMapping.name());
		assertEquals("aliased path attribute: ", "/test", synthesizedAgainWebMapping.path());
		assertEquals("actual value attribute: ", "/test", synthesizedAgainWebMapping.value());
	}

661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
	@Test
	public void synthesizeAnnotationWithAttributeAliasForNonexistentAttribute() throws Exception {
		AliasForNonexistentAttribute annotation = AliasForNonexistentAttributeClass.class.getAnnotation(AliasForNonexistentAttribute.class);
		exception.expect(AnnotationConfigurationException.class);
		exception.expectMessage(containsString("Attribute [foo] in"));
		exception.expectMessage(containsString(AliasForNonexistentAttribute.class.getName()));
		exception.expectMessage(containsString("is declared as an @AliasFor nonexistent attribute [bar]"));
		synthesizeAnnotation(annotation);
	}

	@Test
	public void synthesizeAnnotationWithAttributeAliasWithoutMirroredAliasFor() throws Exception {
		AliasForWithoutMirroredAliasFor annotation = AliasForWithoutMirroredAliasForClass.class.getAnnotation(AliasForWithoutMirroredAliasFor.class);
		exception.expect(AnnotationConfigurationException.class);
		exception.expectMessage(containsString("Attribute [bar] in"));
		exception.expectMessage(containsString(AliasForWithoutMirroredAliasFor.class.getName()));
		exception.expectMessage(containsString("must be declared as an @AliasFor [foo]"));
		synthesizeAnnotation(annotation);
	}

	@Test
	public void synthesizeAnnotationWithAttributeAliasWithMirroredAliasForWrongAttribute() throws Exception {
		AliasForWithMirroredAliasForWrongAttribute annotation = AliasForWithMirroredAliasForWrongAttributeClass.class.getAnnotation(AliasForWithMirroredAliasForWrongAttribute.class);

		// Since JDK 7+ does not guarantee consistent ordering of methods returned using
		// reflection, we cannot make the test dependent on any specific ordering.
		//
		// In other words, we can't be certain which type of exception message we'll get,
		// so we allow for both possibilities.
		exception.expect(AnnotationConfigurationException.class);
		exception.expectMessage(containsString("Attribute [bar] in"));
		exception.expectMessage(containsString(AliasForWithMirroredAliasForWrongAttribute.class.getName()));
		exception.expectMessage(either(containsString("must be declared as an @AliasFor [foo], not [quux]")).
			or(containsString("is declared as an @AliasFor nonexistent attribute [quux]")));
		synthesizeAnnotation(annotation);
	}

	@Test
	public void synthesizeAnnotationWithAttributeAliasForAttributeOfDifferentType() throws Exception {
		AliasForAttributeOfDifferentType annotation = AliasForAttributeOfDifferentTypeClass.class.getAnnotation(AliasForAttributeOfDifferentType.class);
		exception.expect(AnnotationConfigurationException.class);
		exception.expectMessage(startsWith("Misconfigured aliases"));
		exception.expectMessage(containsString(AliasForAttributeOfDifferentType.class.getName()));
		// Since JDK 7+ does not guarantee consistent ordering of methods returned using
		// reflection, we cannot make the test dependent on any specific ordering.
		//
		// In other words, we don't know if "foo" or "bar" will come first.
		exception.expectMessage(containsString("attribute [foo]"));
		exception.expectMessage(containsString("attribute [bar]"));
		exception.expectMessage(containsString("must declare the same return type"));
		synthesizeAnnotation(annotation);
	}

	@Test
	public void synthesizeAnnotationWithAttributeAliasForWithMissingDefaultValues() throws Exception {
		AliasForWithMissingDefaultValues annotation = AliasForWithMissingDefaultValuesClass.class.getAnnotation(AliasForWithMissingDefaultValues.class);
		exception.expectMessage(startsWith("Misconfigured aliases"));
		exception.expectMessage(containsString(AliasForWithMissingDefaultValues.class.getName()));
		// Since JDK 7+ does not guarantee consistent ordering of methods returned using
		// reflection, we cannot make the test dependent on any specific ordering.
		//
		// In other words, we don't know if "foo" or "bar" will come first.
		exception.expectMessage(containsString("attribute [foo]"));
		exception.expectMessage(containsString("attribute [bar]"));
		exception.expectMessage(containsString("must declare default values"));
		synthesizeAnnotation(annotation);
	}

	@Test
	public void synthesizeAnnotationWithAttributeAliasForAttributeWithDifferentDefaultValue() throws Exception {
		AliasForAttributeWithDifferentDefaultValue annotation = AliasForAttributeWithDifferentDefaultValueClass.class.getAnnotation(AliasForAttributeWithDifferentDefaultValue.class);
		exception.expectMessage(startsWith("Misconfigured aliases"));
		exception.expectMessage(containsString(AliasForAttributeWithDifferentDefaultValue.class.getName()));
		// Since JDK 7+ does not guarantee consistent ordering of methods returned using
		// reflection, we cannot make the test dependent on any specific ordering.
		//
		// In other words, we don't know if "foo" or "bar" will come first.
		exception.expectMessage(containsString("attribute [foo]"));
		exception.expectMessage(containsString("attribute [bar]"));
		exception.expectMessage(containsString("must declare the same default value"));
		synthesizeAnnotation(annotation);
	}

	@Test
	public void synthesizeAnnotationWithAttributeAliases() throws Exception {
		Method method = WebController.class.getMethod("handleMappedWithValueAttribute");
		WebMapping webMapping = method.getAnnotation(WebMapping.class);
		assertNotNull(webMapping);

750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
		WebMapping synthesizedWebMapping1 = synthesizeAnnotation(webMapping);
		assertNotNull(synthesizedWebMapping1);
		assertNotSame(webMapping, synthesizedWebMapping1);
		assertThat(synthesizedWebMapping1, instanceOf(SynthesizedAnnotation.class));

		assertEquals("name attribute: ", "foo", synthesizedWebMapping1.name());
		assertEquals("aliased path attribute: ", "/test", synthesizedWebMapping1.path());
		assertEquals("actual value attribute: ", "/test", synthesizedWebMapping1.value());

		WebMapping synthesizedWebMapping2 = synthesizeAnnotation(webMapping);
		assertNotNull(synthesizedWebMapping2);
		assertNotSame(webMapping, synthesizedWebMapping2);
		assertThat(synthesizedWebMapping2, instanceOf(SynthesizedAnnotation.class));

		assertEquals("name attribute: ", "foo", synthesizedWebMapping2.name());
		assertEquals("aliased path attribute: ", "/test", synthesizedWebMapping2.path());
		assertEquals("actual value attribute: ", "/test", synthesizedWebMapping2.value());
	}

769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
	@Test
	public void synthesizeAnnotationFromMapWithoutAttributeAliases() throws Exception {
		Component component = WebController.class.getAnnotation(Component.class);
		assertNotNull(component);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put(VALUE, "webController");
		Component synthesizedComponent = synthesizeAnnotation(map, Component.class, WebController.class);
		assertNotNull(synthesizedComponent);

		assertNotSame(component, synthesizedComponent);
		assertEquals("value from component: ", "webController", component.value());
		assertEquals("value from synthesized component: ", "webController", synthesizedComponent.value());
	}

784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
	@Test
	public void synthesizeAnnotationFromMapWithEmptyAttributesWithDefaultsWithoutAttributeAliases() throws Exception {
		AnnotationWithDefaults annotationWithDefaults = synthesizeAnnotation(EMPTY_ATTRS, AnnotationWithDefaults.class, null);
		assertNotNull(annotationWithDefaults);
		assertEquals("text: ", "enigma", annotationWithDefaults.text());
		assertTrue("predicate: ", annotationWithDefaults.predicate());
		assertArrayEquals("characters: ", new char[] { 'a', 'b', 'c' }, annotationWithDefaults.characters());
	}

	@Test
	public void synthesizeAnnotationFromMapWithEmptyAttributesWithDefaultsWithAttributeAliases() throws Exception {
		ContextConfig contextConfig = synthesizeAnnotation(EMPTY_ATTRS, ContextConfig.class, null);
		assertNotNull(contextConfig);
		assertEquals("value: ", "", contextConfig.value());
		assertEquals("locations: ", "", contextConfig.locations());
	}

	@Test
	public void synthesizeAnnotationFromMapWithMinimalAttributesWithAttributeAliases() throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("locations", "test.xml");
		ContextConfig contextConfig = synthesizeAnnotation(map, ContextConfig.class, null);
		assertNotNull(contextConfig);
		assertEquals("value: ", "test.xml", contextConfig.value());
		assertEquals("locations: ", "test.xml", contextConfig.locations());
	}

811 812
	@Test
	public void synthesizeAnnotationFromMapWithMissingAttributeValue() throws Exception {
813
		assertMissingTextAttribute(EMPTY_ATTRS);
814 815 816 817 818
	}

	@Test
	public void synthesizeAnnotationFromMapWithNullAttributeValue() throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
819 820 821 822
		map.put("text", null);
		assertTrue(map.containsKey("text"));
		assertMissingTextAttribute(map);
	}
823

824
	private void assertMissingTextAttribute(Map<String, Object> attributes) {
825 826
		exception.expect(IllegalArgumentException.class);
		exception.expectMessage(startsWith("Attributes map"));
827 828 829
		exception.expectMessage(containsString("returned null for required attribute [text]"));
		exception.expectMessage(containsString("defined by annotation type [" + AnnotationWithoutDefaults.class.getName() + "]"));
		synthesizeAnnotation(attributes, AnnotationWithoutDefaults.class, null);
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
	}

	@Test
	public void synthesizeAnnotationFromMapWithAttributeOfIncorrectType() throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(VALUE, 42L);

		exception.expect(IllegalArgumentException.class);
		exception.expectMessage(startsWith("Attributes map"));
		exception.expectMessage(containsString("returned a value of type [java.lang.Long]"));
		exception.expectMessage(containsString("for attribute [value]"));
		exception.expectMessage(containsString("but a value of type [java.lang.String] is required"));
		exception.expectMessage(containsString("as defined by annotation type [" + Component.class.getName() + "]"));
		synthesizeAnnotation(map, Component.class, null);
	}

	@Test
	public void synthesizeAnnotationFromAnnotationAttributesWithoutAttributeAliases() throws Exception {

		// 1) Get an annotation
		Component component = WebController.class.getAnnotation(Component.class);
		assertNotNull(component);

		// 2) Convert the annotation into AnnotationAttributes
		AnnotationAttributes attributes = getAnnotationAttributes(WebController.class, component);
		assertNotNull(attributes);

		// 3) Synthesize the AnnotationAttributes back into an annotation
		Component synthesizedComponent = synthesizeAnnotation(attributes, Component.class, WebController.class);
		assertNotNull(synthesizedComponent);

		// 4) Verify that the original and synthesized annotations are equivalent
		assertNotSame(component, synthesizedComponent);
		assertEquals(component, synthesizedComponent);
		assertEquals("value from component: ", "webController", component.value());
		assertEquals("value from synthesized component: ", "webController", synthesizedComponent.value());
	}

868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
	@Test
	public void toStringForSynthesizedAnnotations() throws Exception {
		Method methodWithPath = WebController.class.getMethod("handleMappedWithPathAttribute");
		WebMapping webMappingWithAliases = methodWithPath.getAnnotation(WebMapping.class);
		assertNotNull(webMappingWithAliases);

		Method methodWithPathAndValue = WebController.class.getMethod("handleMappedWithSamePathAndValueAttributes");
		WebMapping webMappingWithPathAndValue = methodWithPathAndValue.getAnnotation(WebMapping.class);
		assertNotNull(webMappingWithPathAndValue);

		WebMapping synthesizedWebMapping1 = synthesizeAnnotation(webMappingWithAliases);
		assertNotNull(synthesizedWebMapping1);
		WebMapping synthesizedWebMapping2 = synthesizeAnnotation(webMappingWithAliases);
		assertNotNull(synthesizedWebMapping2);

		assertThat(webMappingWithAliases.toString(), is(not(synthesizedWebMapping1.toString())));

		// The unsynthesized annotation for handleMappedWithSamePathAndValueAttributes()
		// should produce the same toString() results as synthesized annotations for
		// handleMappedWithPathAttribute()
		assertToStringForWebMappingWithPathAndValue(webMappingWithPathAndValue);
		assertToStringForWebMappingWithPathAndValue(synthesizedWebMapping1);
		assertToStringForWebMappingWithPathAndValue(synthesizedWebMapping2);
	}

	private void assertToStringForWebMappingWithPathAndValue(WebMapping webMapping) {
		String string = webMapping.toString();
		assertThat(string, startsWith("@" + WebMapping.class.getName() + "("));
		assertThat(string, containsString("value=/test"));
		assertThat(string, containsString("path=/test"));
		assertThat(string, containsString("name=bar"));
		assertThat(string, containsString("method="));
900
		assertThat(string, containsString("[GET, POST]"));
901
		assertThat(string, endsWith(")"));
902 903
	}

904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
	@Test
	public void equalsForSynthesizedAnnotations() throws Exception {
		Method methodWithPath = WebController.class.getMethod("handleMappedWithPathAttribute");
		WebMapping webMappingWithAliases = methodWithPath.getAnnotation(WebMapping.class);
		assertNotNull(webMappingWithAliases);

		Method methodWithPathAndValue = WebController.class.getMethod("handleMappedWithSamePathAndValueAttributes");
		WebMapping webMappingWithPathAndValue = methodWithPathAndValue.getAnnotation(WebMapping.class);
		assertNotNull(webMappingWithPathAndValue);

		WebMapping synthesizedWebMapping1 = synthesizeAnnotation(webMappingWithAliases);
		assertNotNull(synthesizedWebMapping1);
		WebMapping synthesizedWebMapping2 = synthesizeAnnotation(webMappingWithAliases);
		assertNotNull(synthesizedWebMapping2);

		// Equality amongst standard annotations
		assertThat(webMappingWithAliases, is(webMappingWithAliases));
		assertThat(webMappingWithPathAndValue, is(webMappingWithPathAndValue));

		// Inequality amongst standard annotations
		assertThat(webMappingWithAliases, is(not(webMappingWithPathAndValue)));
		assertThat(webMappingWithPathAndValue, is(not(webMappingWithAliases)));

		// Equality amongst synthesized annotations
		assertThat(synthesizedWebMapping1, is(synthesizedWebMapping1));
		assertThat(synthesizedWebMapping2, is(synthesizedWebMapping2));
		assertThat(synthesizedWebMapping1, is(synthesizedWebMapping2));
		assertThat(synthesizedWebMapping2, is(synthesizedWebMapping1));

		// Equality between standard and synthesized annotations
		assertThat(synthesizedWebMapping1, is(webMappingWithPathAndValue));
		assertThat(webMappingWithPathAndValue, is(synthesizedWebMapping1));

		// Inequality between standard and synthesized annotations
		assertThat(synthesizedWebMapping1, is(not(webMappingWithAliases)));
		assertThat(webMappingWithAliases, is(not(synthesizedWebMapping1)));
	}

942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
	@Test
	public void hashCodeForSynthesizedAnnotations() throws Exception {
		Method methodWithPath = WebController.class.getMethod("handleMappedWithPathAttribute");
		WebMapping webMappingWithAliases = methodWithPath.getAnnotation(WebMapping.class);
		assertNotNull(webMappingWithAliases);

		Method methodWithPathAndValue = WebController.class.getMethod("handleMappedWithSamePathAndValueAttributes");
		WebMapping webMappingWithPathAndValue = methodWithPathAndValue.getAnnotation(WebMapping.class);
		assertNotNull(webMappingWithPathAndValue);

		WebMapping synthesizedWebMapping1 = synthesizeAnnotation(webMappingWithAliases);
		assertNotNull(synthesizedWebMapping1);
		WebMapping synthesizedWebMapping2 = synthesizeAnnotation(webMappingWithAliases);
		assertNotNull(synthesizedWebMapping2);

		// Equality amongst standard annotations
		assertThat(webMappingWithAliases.hashCode(), is(webMappingWithAliases.hashCode()));
		assertThat(webMappingWithPathAndValue.hashCode(), is(webMappingWithPathAndValue.hashCode()));

		// Inequality amongst standard annotations
		assertThat(webMappingWithAliases.hashCode(), is(not(webMappingWithPathAndValue.hashCode())));
		assertThat(webMappingWithPathAndValue.hashCode(), is(not(webMappingWithAliases.hashCode())));

		// Equality amongst synthesized annotations
		assertThat(synthesizedWebMapping1.hashCode(), is(synthesizedWebMapping1.hashCode()));
		assertThat(synthesizedWebMapping2.hashCode(), is(synthesizedWebMapping2.hashCode()));
		assertThat(synthesizedWebMapping1.hashCode(), is(synthesizedWebMapping2.hashCode()));
		assertThat(synthesizedWebMapping2.hashCode(), is(synthesizedWebMapping1.hashCode()));

		// Equality between standard and synthesized annotations
		assertThat(synthesizedWebMapping1.hashCode(), is(webMappingWithPathAndValue.hashCode()));
		assertThat(webMappingWithPathAndValue.hashCode(), is(synthesizedWebMapping1.hashCode()));

		// Inequality between standard and synthesized annotations
		assertThat(synthesizedWebMapping1.hashCode(), is(not(webMappingWithAliases.hashCode())));
		assertThat(webMappingWithAliases.hashCode(), is(not(synthesizedWebMapping1.hashCode())));
	}

980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
	/**
	 * Fully reflection-based test that verifies support for
	 * {@linkplain AnnotationUtils#synthesizeAnnotation synthesizing annotations}
	 * across packages with non-public visibility of user types (e.g., a non-public
	 * annotation that uses {@code @AliasFor}).
	 */
	@Test
	@SuppressWarnings("unchecked")
	public void synthesizeNonPublicAnnotationWithAttributeAliasesFromDifferentPackage() throws Exception {

		Class<?> clazz =
			ClassUtils.forName("org.springframework.core.annotation.subpackage.NonPublicAliasedAnnotatedClass", null);
		Class<? extends Annotation> annotationType = (Class<? extends Annotation>)
			ClassUtils.forName("org.springframework.core.annotation.subpackage.NonPublicAliasedAnnotation", null);

		Annotation annotation = clazz.getAnnotation(annotationType);
		assertNotNull(annotation);
		Annotation synthesizedAnnotation = synthesizeAnnotation(annotation);
		assertNotSame(annotation, synthesizedAnnotation);

		assertNotNull(synthesizedAnnotation);
		assertEquals("name attribute: ", "test", getValue(synthesizedAnnotation, "name"));
		assertEquals("aliased path attribute: ", "/test", getValue(synthesizedAnnotation, "path"));
		assertEquals("aliased path attribute: ", "/test", getValue(synthesizedAnnotation, "value"));
	}

1006 1007
	@Test
	public void synthesizeAnnotationWithAttributeAliasesInNestedAnnotations() throws Exception {
1008 1009
		List<String> expectedLocations = Arrays.asList("A", "B");

1010
		Hierarchy hierarchy = ConfigHierarchyTestCase.class.getAnnotation(Hierarchy.class);
1011 1012 1013 1014 1015 1016 1017
		assertNotNull(hierarchy);
		Hierarchy synthesizedHierarchy = synthesizeAnnotation(hierarchy);
		assertNotSame(hierarchy, synthesizedHierarchy);
		assertThat(synthesizedHierarchy, instanceOf(SynthesizedAnnotation.class));

		ContextConfig[] configs = synthesizedHierarchy.value();
		assertNotNull(configs);
S
Sam Brannen 已提交
1018 1019
		assertTrue("nested annotations must be synthesized",
			Arrays.stream(configs).allMatch(c -> c instanceof SynthesizedAnnotation));
1020

S
Sam Brannen 已提交
1021
		List<String> locations = Arrays.stream(configs).map(ContextConfig::locations).collect(toList());
1022
		assertThat(locations, is(expectedLocations));
1023

S
Sam Brannen 已提交
1024
		List<String> values = Arrays.stream(configs).map(ContextConfig::value).collect(toList());
1025
		assertThat(values, is(expectedLocations));
1026 1027 1028
	}

	@Test
1029
	public void synthesizeAnnotationWithArrayOfAnnotations() throws Exception {
1030 1031
		List<String> expectedLocations = Arrays.asList("A", "B");

1032 1033 1034 1035
		Hierarchy hierarchy = ConfigHierarchyTestCase.class.getAnnotation(Hierarchy.class);
		assertNotNull(hierarchy);
		Hierarchy synthesizedHierarchy = synthesizeAnnotation(hierarchy);
		assertThat(synthesizedHierarchy, instanceOf(SynthesizedAnnotation.class));
1036

1037 1038 1039 1040 1041
		ContextConfig contextConfig = SimpleConfigTestCase.class.getAnnotation(ContextConfig.class);
		assertNotNull(contextConfig);

		ContextConfig[] configs = synthesizedHierarchy.value();
		List<String> locations = Arrays.stream(configs).map(ContextConfig::locations).collect(toList());
1042
		assertThat(locations, is(expectedLocations));
1043 1044 1045 1046 1047 1048 1049

		// Alter array returned from synthesized annotation
		configs[0] = contextConfig;

		// Re-retrieve the array from the synthesized annotation
		configs = synthesizedHierarchy.value();
		List<String> values = Arrays.stream(configs).map(ContextConfig::value).collect(toList());
1050
		assertThat(values, is(expectedLocations));
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
	}

	@Test
	public void synthesizeAnnotationWithArrayOfChars() throws Exception {
		CharsContainer charsContainer = GroupOfCharsClass.class.getAnnotation(CharsContainer.class);
		assertNotNull(charsContainer);
		CharsContainer synthesizedCharsContainer = synthesizeAnnotation(charsContainer);
		assertThat(synthesizedCharsContainer, instanceOf(SynthesizedAnnotation.class));

		char[] chars = synthesizedCharsContainer.chars();
		assertArrayEquals(new char[] { 'x', 'y', 'z' }, chars);

		// Alter array returned from synthesized annotation
		chars[0] = '?';

		// Re-retrieve the array from the synthesized annotation
		chars = synthesizedCharsContainer.chars();
		assertArrayEquals(new char[] { 'x', 'y', 'z' }, chars);
1069 1070
	}

1071

1072
	@Component("meta1")
1073
	@Order
1074
	@Retention(RetentionPolicy.RUNTIME)
1075
	@Inherited
1076 1077 1078
	@interface Meta1 {
	}

1079
	@Component("meta2")
1080
	@Transactional(readOnly = true)
1081 1082 1083 1084
	@Retention(RetentionPolicy.RUNTIME)
	@interface Meta2 {
	}

1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
	@Meta2
	@Retention(RetentionPolicy.RUNTIME)
	@interface MetaMeta {
	}

	@MetaMeta
	@Retention(RetentionPolicy.RUNTIME)
	@interface MetaMetaMeta {
	}

	@MetaCycle3
	@Retention(RetentionPolicy.RUNTIME)
	@interface MetaCycle1 {
	}

	@MetaCycle1
	@Retention(RetentionPolicy.RUNTIME)
	@interface MetaCycle2 {
	}

	@MetaCycle2
	@Retention(RetentionPolicy.RUNTIME)
	@interface MetaCycle3 {
	}

1110
	@Meta1
S
Polish  
Stephane Nicoll 已提交
1111
	interface InterfaceWithMetaAnnotation {
1112 1113
	}

1114
	@Meta2
1115
	static class ClassWithLocalMetaAnnotationAndMetaAnnotatedInterface implements InterfaceWithMetaAnnotation {
1116 1117
	}

1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
	@Meta1
	static class ClassWithInheritedMetaAnnotation {
	}

	@Meta2
	static class SubClassWithInheritedMetaAnnotation extends ClassWithInheritedMetaAnnotation {
	}

	static class SubSubClassWithInheritedMetaAnnotation extends SubClassWithInheritedMetaAnnotation {
	}

	@Transactional
	static class ClassWithInheritedAnnotation {
	}

	@Meta2
	static class SubClassWithInheritedAnnotation extends ClassWithInheritedAnnotation {
	}

	static class SubSubClassWithInheritedAnnotation extends SubClassWithInheritedAnnotation {
	}

1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
	@MetaMeta
	static class MetaMetaAnnotatedClass {
	}

	@MetaMetaMeta
	static class MetaMetaMetaAnnotatedClass {
	}

	@MetaCycle3
	static class MetaCycleAnnotatedClass {
	}
1151

S
Polish  
Stephane Nicoll 已提交
1152
	public interface AnnotatedInterface {
A
Arjen Poutsma 已提交
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163

		@Order(0)
		void fromInterfaceImplementedByRoot();
	}

	public static class Root implements AnnotatedInterface {

		@Order(27)
		public void annotatedOnRoot() {
		}

1164 1165 1166 1167
		@Meta1
		public void metaAnnotatedOnRoot() {
		}

A
Arjen Poutsma 已提交
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
		public void overrideToAnnotate() {
		}

		@Order(27)
		public void overrideWithoutNewAnnotation() {
		}

		public void notAnnotated() {
		}

1178
		@Override
A
Arjen Poutsma 已提交
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
		public void fromInterfaceImplementedByRoot() {
		}
	}

	public static class Leaf extends Root {

		@Order(25)
		public void annotatedOnLeaf() {
		}

1189 1190 1191 1192 1193 1194 1195 1196
		@Meta1
		public void metaAnnotatedOnLeaf() {
		}

		@MetaMeta
		public void metaMetaAnnotatedOnLeaf() {
		}

A
Arjen Poutsma 已提交
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
		@Override
		@Order(1)
		public void overrideToAnnotate() {
		}

		@Override
		public void overrideWithoutNewAnnotation() {
		}
	}

J
Juergen Hoeller 已提交
1207 1208 1209
	@Retention(RetentionPolicy.RUNTIME)
	@Inherited
	@interface Transactional {
1210 1211

		boolean readOnly() default false;
J
Juergen Hoeller 已提交
1212 1213
	}

A
Arjen Poutsma 已提交
1214 1215 1216 1217 1218 1219 1220 1221
	public static abstract class Foo<T> {

		@Order(1)
		public abstract void something(T arg);
	}

	public static class SimpleFoo extends Foo<String> {

1222
		@Override
A
Arjen Poutsma 已提交
1223 1224 1225 1226 1227 1228
		@Transactional
		public void something(final String arg) {
		}
	}

	@Transactional
S
Polish  
Stephane Nicoll 已提交
1229
	public interface InheritedAnnotationInterface {
A
Arjen Poutsma 已提交
1230 1231
	}

S
Polish  
Stephane Nicoll 已提交
1232
	public interface SubInheritedAnnotationInterface extends InheritedAnnotationInterface {
A
Arjen Poutsma 已提交
1233 1234
	}

S
Polish  
Stephane Nicoll 已提交
1235
	public interface SubSubInheritedAnnotationInterface extends SubInheritedAnnotationInterface {
1236 1237
	}

A
Arjen Poutsma 已提交
1238
	@Order
S
Polish  
Stephane Nicoll 已提交
1239
	public interface NonInheritedAnnotationInterface {
A
Arjen Poutsma 已提交
1240 1241
	}

S
Polish  
Stephane Nicoll 已提交
1242
	public interface SubNonInheritedAnnotationInterface extends NonInheritedAnnotationInterface {
A
Arjen Poutsma 已提交
1243 1244
	}

S
Polish  
Stephane Nicoll 已提交
1245
	public interface SubSubNonInheritedAnnotationInterface extends SubNonInheritedAnnotationInterface {
1246 1247
	}

A
Arjen Poutsma 已提交
1248 1249 1250
	public static class NonAnnotatedClass {
	}

S
Polish  
Stephane Nicoll 已提交
1251
	public interface NonAnnotatedInterface {
A
Arjen Poutsma 已提交
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
	}

	@Transactional
	public static class InheritedAnnotationClass {
	}

	public static class SubInheritedAnnotationClass extends InheritedAnnotationClass {
	}

	@Order
	public static class NonInheritedAnnotationClass {
	}

	public static class SubNonInheritedAnnotationClass extends NonInheritedAnnotationClass {
	}

1268 1269 1270 1271 1272
	@Transactional
	public static class TransactionalClass {
	}

	@Order
1273
	public static class TransactionalAndOrderedClass extends TransactionalClass {
1274 1275 1276 1277
	}

	public static class SubTransactionalAndOrderedClass extends TransactionalAndOrderedClass {
	}
1278

S
Polish  
Stephane Nicoll 已提交
1279
	public interface InterfaceWithAnnotatedMethod {
1280 1281 1282 1283 1284 1285 1286

		@Order
		void foo();
	}

	public static class ImplementsInterfaceWithAnnotatedMethod implements InterfaceWithAnnotatedMethod {

1287
		@Override
1288 1289 1290 1291 1292 1293
		public void foo() {
		}
	}

	public static class SubOfImplementsInterfaceWithAnnotatedMethod extends ImplementsInterfaceWithAnnotatedMethod {

1294
		@Override
1295 1296 1297 1298
		public void foo() {
		}
	}

J
Juergen Hoeller 已提交
1299 1300
	public abstract static class AbstractDoesNotImplementInterfaceWithAnnotatedMethod
			implements InterfaceWithAnnotatedMethod {
1301 1302
	}

J
Juergen Hoeller 已提交
1303 1304
	public static class SubOfAbstractImplementsInterfaceWithAnnotatedMethod
			extends AbstractDoesNotImplementInterfaceWithAnnotatedMethod {
1305

1306
		@Override
1307 1308 1309 1310
		public void foo() {
		}
	}

J
Juergen Hoeller 已提交
1311 1312 1313
	@Retention(RetentionPolicy.RUNTIME)
	@Inherited
	@interface MyRepeatableContainer {
1314

J
Juergen Hoeller 已提交
1315
		MyRepeatable[] value();
1316 1317
	}

J
Juergen Hoeller 已提交
1318 1319 1320 1321
	@Retention(RetentionPolicy.RUNTIME)
	@Inherited
	@Repeatable(MyRepeatableContainer.class)
	@interface MyRepeatable {
1322

J
Juergen Hoeller 已提交
1323 1324
		String value();
	}
1325

J
Juergen Hoeller 已提交
1326 1327
	@Retention(RetentionPolicy.RUNTIME)
	@Inherited
1328 1329
	@MyRepeatable("meta1")
	@interface MyRepeatableMeta1 {
J
Juergen Hoeller 已提交
1330
	}
1331

1332 1333 1334 1335 1336 1337 1338
	@Retention(RetentionPolicy.RUNTIME)
	@Inherited
	@MyRepeatable("meta2")
	@interface MyRepeatableMeta2 {
	}

	interface InterfaceWithRepeated {
1339

1340 1341 1342
		@MyRepeatable("A")
		@MyRepeatableContainer({ @MyRepeatable("B"), @MyRepeatable("C") })
		@MyRepeatableMeta1
J
Juergen Hoeller 已提交
1343 1344
		void foo();
	}
1345

1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
	@MyRepeatable("A")
	@MyRepeatableContainer({ @MyRepeatable("B"), @MyRepeatable("C") })
	@MyRepeatableMeta1
	static class MyRepeatableClass {
	}

	static class SubMyRepeatableClass extends MyRepeatableClass {
	}

	@MyRepeatable("X")
	@MyRepeatableContainer({ @MyRepeatable("Y"), @MyRepeatable("Z") })
	@MyRepeatableMeta2
	static class SubMyRepeatableWithAdditionalLocalDeclarationsClass extends MyRepeatableClass {
	}

1361 1362 1363 1364
	enum RequestMethod {
		GET, POST
	}

1365
	/**
S
Polish  
Stephane Nicoll 已提交
1366
	 * Mock of {@code org.springframework.web.bind.annotation.RequestMapping}.
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
	 */
	@Retention(RetentionPolicy.RUNTIME)
	@interface WebMapping {

		String name();

		@AliasFor(attribute = "path")
		String value() default "";

		@AliasFor(attribute = "value")
		String path() default "";
1378 1379

		RequestMethod[] method() default {};
1380 1381 1382 1383 1384 1385 1386 1387 1388
	}

	@Component("webController")
	static class WebController {

		@WebMapping(value = "/test", name = "foo")
		public void handleMappedWithValueAttribute() {
		}

1389
		@WebMapping(path = "/test", name = "bar", method = { RequestMethod.GET, RequestMethod.POST })
1390 1391 1392
		public void handleMappedWithPathAttribute() {
		}

1393 1394 1395 1396 1397 1398 1399
		/**
		 * mapping is logically "equal" to handleMappedWithPathAttribute().
		 */
		@WebMapping(value = "/test", path = "/test", name = "bar", method = { RequestMethod.GET, RequestMethod.POST })
		public void handleMappedWithSamePathAndValueAttributes() {
		}

1400
		@WebMapping(value = "/enigma", path = "/test", name = "baz")
1401
		public void handleMappedWithDifferentPathAndValueAttributes() {
1402 1403 1404 1405
		}
	}

	/**
S
Polish  
Stephane Nicoll 已提交
1406
	 * Mock of {@code org.springframework.test.context.ContextConfiguration}.
1407 1408
	 */
	@Retention(RetentionPolicy.RUNTIME)
S
Polish  
Stephane Nicoll 已提交
1409
	@interface ContextConfig {
1410 1411 1412 1413 1414 1415 1416 1417

		@AliasFor(attribute = "locations")
		String value() default "";

		@AliasFor(attribute = "value")
		String locations() default "";
	}

1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
	@Retention(RetentionPolicy.RUNTIME)
	@interface BrokenContextConfig {

		// Intentionally missing:
		// @AliasFor(attribute = "locations")
		String value() default "";

		@AliasFor(attribute = "value")
		String locations() default "";
	}

1429
	/**
S
Polish  
Stephane Nicoll 已提交
1430
	 * Mock of {@code org.springframework.test.context.ContextHierarchy}.
1431 1432
	 */
	@Retention(RetentionPolicy.RUNTIME)
S
Polish  
Stephane Nicoll 已提交
1433
	@interface Hierarchy {
1434 1435 1436
		ContextConfig[] value();
	}

1437 1438 1439 1440 1441
	@Retention(RetentionPolicy.RUNTIME)
	@interface BrokenHierarchy {
		BrokenContextConfig[] value();
	}

1442
	@Hierarchy({ @ContextConfig("A"), @ContextConfig(locations = "B") })
1443
	static class ConfigHierarchyTestCase {
1444 1445
	}

1446 1447 1448 1449
	@BrokenHierarchy(@BrokenContextConfig)
	static class BrokenConfigHierarchyTestCase {
	}

1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468
	@ContextConfig("simple.xml")
	static class SimpleConfigTestCase {
	}

	@Retention(RetentionPolicy.RUNTIME)
	@interface CharsContainer {

		@AliasFor(attribute = "chars")
		char[] value() default {};

		@AliasFor(attribute = "value")
		char[] chars() default {};
	}

	@CharsContainer(chars = { 'x', 'y', 'z' })
	static class GroupOfCharsClass {
	}


1469
	@Retention(RetentionPolicy.RUNTIME)
S
Polish  
Stephane Nicoll 已提交
1470
	@interface AliasForNonexistentAttribute {
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480

		@AliasFor(attribute = "bar")
		String foo() default "";
	}

	@AliasForNonexistentAttribute
	static class AliasForNonexistentAttributeClass {
	}

	@Retention(RetentionPolicy.RUNTIME)
S
Polish  
Stephane Nicoll 已提交
1481
	@interface AliasForWithoutMirroredAliasFor {
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493

		@AliasFor(attribute = "bar")
		String foo() default "";

		String bar() default "";
	}

	@AliasForWithoutMirroredAliasFor
	static class AliasForWithoutMirroredAliasForClass {
	}

	@Retention(RetentionPolicy.RUNTIME)
S
Polish  
Stephane Nicoll 已提交
1494
	@interface AliasForWithMirroredAliasForWrongAttribute {
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507

		@AliasFor(attribute = "bar")
		String[] foo() default "";

		@AliasFor(attribute = "quux")
		String[] bar() default "";
	}

	@AliasForWithMirroredAliasForWrongAttribute
	static class AliasForWithMirroredAliasForWrongAttributeClass {
	}

	@Retention(RetentionPolicy.RUNTIME)
S
Polish  
Stephane Nicoll 已提交
1508
	@interface AliasForAttributeOfDifferentType {
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521

		@AliasFor(attribute = "bar")
		String[] foo() default "";

		@AliasFor(attribute = "foo")
		boolean bar() default true;
	}

	@AliasForAttributeOfDifferentType
	static class AliasForAttributeOfDifferentTypeClass {
	}

	@Retention(RetentionPolicy.RUNTIME)
S
Polish  
Stephane Nicoll 已提交
1522
	@interface AliasForWithMissingDefaultValues {
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535

		@AliasFor(attribute = "bar")
		String foo();

		@AliasFor(attribute = "foo")
		String bar();
	}

	@AliasForWithMissingDefaultValues(foo = "foo", bar = "bar")
	static class AliasForWithMissingDefaultValuesClass {
	}

	@Retention(RetentionPolicy.RUNTIME)
S
Polish  
Stephane Nicoll 已提交
1536
	@interface AliasForAttributeWithDifferentDefaultValue {
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550

		@AliasFor(attribute = "bar")
		String foo() default "X";

		@AliasFor(attribute = "foo")
		String bar() default "Z";
	}

	@AliasForAttributeWithDifferentDefaultValue
	static class AliasForAttributeWithDifferentDefaultValueClass {
	}

	@ContextConfig
	@Retention(RetentionPolicy.RUNTIME)
S
Polish  
Stephane Nicoll 已提交
1551
	@interface AliasedComposedContextConfig {
1552 1553 1554 1555 1556

		@AliasFor(annotation = ContextConfig.class, attribute = "locations")
		String xmlConfigFile();
	}

1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
	@Retention(RetentionPolicy.RUNTIME)
	@Target({})
	@interface Filter {
		String pattern();
	}

	/**
	 * Mock of {@code org.springframework.context.annotation.ComponentScan}
	 */
	@Retention(RetentionPolicy.RUNTIME)
	@interface ComponentScan {
		Filter[] excludeFilters() default {};
	}

	@ComponentScan(excludeFilters = { @Filter(pattern = "*Foo"), @Filter(pattern = "*Bar") })
	static class ComponentScanClass {
	}

1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
	@Retention(RetentionPolicy.RUNTIME)
	@interface AnnotationWithDefaults {
		String text() default "enigma";
		boolean predicate() default true;
		char[] characters() default {'a', 'b', 'c'};
	}

	@Retention(RetentionPolicy.RUNTIME)
	@interface AnnotationWithoutDefaults {
		String text();
	}

1587
}