org.springframework.core.annotation.MergedAnnotations.SearchStrategy Java Examples

The following examples show how to use org.springframework.core.annotation.MergedAnnotations.SearchStrategy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AnnotationsScannerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private Stream<String> scan(AnnotatedElement element, SearchStrategy searchStrategy) {
	List<String> result = new ArrayList<>();
	AnnotationsScanner.scan(this, element, searchStrategy,
			(criteria, aggregateIndex, source, annotations) -> {
				for (Annotation annotation : annotations) {
					if (annotation != null) {
						String name = ClassUtils.getShortName(
								annotation.annotationType());
						name = name.substring(name.lastIndexOf(".") + 1);
						result.add(aggregateIndex + ":" + name);
					}
				}
				return null;
			});
	return result.stream();
}
 
Example #2
Source File: AnnotationJmxAttributeSource.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
@Nullable
public org.springframework.jmx.export.metadata.ManagedAttribute getManagedAttribute(Method method) throws InvalidMetadataException {
	MergedAnnotation<ManagedAttribute> ann = MergedAnnotations.from(method, SearchStrategy.EXHAUSTIVE)
			.get(ManagedAttribute.class).withNonMergedAttributes();
	if (!ann.isPresent()) {
		return null;
	}

	org.springframework.jmx.export.metadata.ManagedAttribute bean = new org.springframework.jmx.export.metadata.ManagedAttribute();
	Map<String, Object> map = ann.asMap();
	MutablePropertyValues pvs = new MutablePropertyValues(map);
	pvs.removePropertyValue("defaultValue");
	PropertyAccessorFactory.forBeanPropertyAccess(bean).setPropertyValues(pvs);
	String defaultValue = (String) map.get("defaultValue");
	if (defaultValue.length() > 0) {
		bean.setDefaultValue(defaultValue);
	}
	return bean;
}
 
Example #3
Source File: MergedAnnotationsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void getWithInheritedAnnotationsFromNonInheritedAnnotationInterface() {
	MergedAnnotation<?> annotation = MergedAnnotations.from(
			NonInheritedAnnotationInterface.class,
			SearchStrategy.INHERITED_ANNOTATIONS).get(Order.class);
	assertThat(annotation.isPresent()).isTrue();
}
 
Example #4
Source File: MergedAnnotationsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void getDirectRepeatablesDeclaredOnClass() {
	Class<?> element = MyRepeatableClass.class;
	String[] expectedValuesJava = { "A", "B", "C" };
	String[] expectedValuesSpring = { "A", "B", "C", "meta1" };
	testRepeatables(SearchStrategy.DIRECT, element, expectedValuesJava,
			expectedValuesSpring);
}
 
Example #5
Source File: MergedAnnotationsRepeatableAnnotationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void inheritedAnnotationsWhenComposedMixedWithContainerOnClassReturnsAnnotations() {
	Set<PeteRepeat> annotations = getAnnotations(null, PeteRepeat.class,
			SearchStrategy.INHERITED_ANNOTATIONS,
			ComposedRepeatableMixedWithContainerClass.class);
	assertThat(annotations.stream().map(PeteRepeat::value)).containsExactly("A", "B",
			"C");
}
 
Example #6
Source File: MergedAnnotationsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void getWithExhaustiveInheritedFromAbstractMethod()
		throws NoSuchMethodException {
	Method method = ConcreteClassWithInheritedAnnotation.class.getMethod("handle");
	MergedAnnotation<?> annotation = MergedAnnotations.from(method,
			SearchStrategy.EXHAUSTIVE).get(Transactional.class);
	assertThat(annotation.isPresent()).isTrue();
	assertThat(annotation.getAggregateIndex()).isEqualTo(1);
}
 
Example #7
Source File: MergedAnnotationsRepeatableAnnotationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private <A extends Annotation> Set<A> getAnnotations(
		Class<? extends Annotation> container, Class<A> repeatable,
		SearchStrategy searchStrategy, AnnotatedElement element) {
	RepeatableContainers containers = RepeatableContainers.of(repeatable, container);
	MergedAnnotations annotations = MergedAnnotations.from(element,
			searchStrategy, containers, AnnotationFilter.PLAIN);
	return annotations.stream(repeatable).collect(
			MergedAnnotationCollectors.toAnnotationSet());
}
 
Example #8
Source File: AnnotationsScannerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void inheritedAnnotationsStrategyOnClassWhenHasAnnotationOnBothClassesIncudesOnlyOne() {
	Class<?> source = WithSingleSuperclassAndDoubleInherited.class;
	assertThat(Arrays.stream(source.getAnnotations()).map(
			Annotation::annotationType).map(Class::getName)).containsExactly(
					TestInheritedAnnotation2.class.getName());
	assertThat(scan(source, SearchStrategy.INHERITED_ANNOTATIONS)).containsOnly(
			"0:TestInheritedAnnotation2");
}
 
Example #9
Source File: MergedAnnotationsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void getWithInheritedAnnotationsFavorsLocalComposedAnnotationOverInheritedAnnotation() {
	MergedAnnotation<?> annotation = MergedAnnotations.from(
			SubClassWithInheritedAnnotation.class,
			SearchStrategy.INHERITED_ANNOTATIONS).get(Transactional.class);
	assertThat(annotation.getBoolean("readOnly")).isTrue();
}
 
Example #10
Source File: MergedAnnotationsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void getDirectFromClassgetDirectFromClassMetaMetaAnnotatedClass() {
	MergedAnnotation<?> annotation = MergedAnnotations.from(
			MetaMetaAnnotatedClass.class, SearchStrategy.EXHAUSTIVE).get(
					Component.class);
	assertThat(annotation.getString("value")).isEqualTo("meta2");
}
 
Example #11
Source File: MergedAnnotationsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void getWithExhaustiveFromInheritedAnnotationInterface() {
	MergedAnnotation<?> annotation = MergedAnnotations.from(
			InheritedAnnotationInterface.class, SearchStrategy.EXHAUSTIVE).get(
					Transactional.class);
	assertThat(annotation.isPresent()).isTrue();
	assertThat(annotation.getAggregateIndex()).isEqualTo(0);
}
 
Example #12
Source File: MergedAnnotationsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void WithInheritedAnnotationsFromAliasedComposedAnnotation() {
	MergedAnnotation<?> annotation = MergedAnnotations.from(
			AliasedComposedContextConfigurationClass.class,
			SearchStrategy.INHERITED_ANNOTATIONS).get(ContextConfiguration.class);
	assertThat(annotation.getStringArray("value")).containsExactly("test.xml");
	assertThat(annotation.getStringArray("locations")).containsExactly("test.xml");
}
 
Example #13
Source File: MergedAnnotationsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void WithInheritedAnnotationsFromAliasedValueComposedAnnotation() {
	MergedAnnotation<?> annotation = MergedAnnotations.from(
			AliasedValueComposedContextConfigurationClass.class,
			SearchStrategy.INHERITED_ANNOTATIONS).get(ContextConfiguration.class);
	assertThat(annotation.getStringArray("value")).containsExactly("test.xml");
	assertThat(annotation.getStringArray("locations")).containsExactly("test.xml");
}
 
Example #14
Source File: MergedAnnotationsRepeatableAnnotationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void exhaustiveAnnotationsWhenNoninheritedComposedRepeatableOnClassReturnsAnnotations() {
	Set<Noninherited> annotations = getAnnotations(null, Noninherited.class,
			SearchStrategy.EXHAUSTIVE, NoninheritedRepeatableClass.class);
	assertThat(annotations.stream().map(Noninherited::value)).containsExactly("A",
			"B", "C");
}
 
Example #15
Source File: MergedAnnotationsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void testGetWithInherited(Class<?> element, String... expected) {
	MergedAnnotation<?> annotation = MergedAnnotations.from(element,
			SearchStrategy.INHERITED_ANNOTATIONS).get(ContextConfiguration.class);
	assertThat(annotation.getStringArray("locations")).isEqualTo(expected);
	assertThat(annotation.getStringArray("value")).isEqualTo(expected);
	assertThat(annotation.getClassArray("classes")).isEmpty();
}
 
Example #16
Source File: MergedAnnotationsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void getDirectFromClassWithSubSubNonInheritedAnnotationInterface() {
	MergedAnnotation<?> annotation = MergedAnnotations.from(
			SubSubNonInheritedAnnotationInterface.class,
			SearchStrategy.EXHAUSTIVE).get(Order.class);
	assertThat(annotation.getAggregateIndex()).isEqualTo(2);
}
 
Example #17
Source File: MergedAnnotationsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void getWithInheritedAnnotationsFromHalfConventionBasedAndHalfAliasedComposedAnnotation1() {
	// SPR-13554: convention mapping mixed with AlaisFor annotations
	// xmlConfigFiles can be used because it has an AlaisFor annotation
	MergedAnnotation<?> annotation = MergedAnnotations.from(
			HalfConventionBasedAndHalfAliasedComposedContextConfigurationClass1.class,
			SearchStrategy.INHERITED_ANNOTATIONS).get(ContextConfiguration.class);
	assertThat(annotation.getStringArray("locations")).containsExactly(
			"explicitDeclaration");
	assertThat(annotation.getStringArray("value")).containsExactly(
			"explicitDeclaration");
}
 
Example #18
Source File: MergedAnnotationsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test // SPR-16060
public void getFromMethodWithGenericInterface() throws Exception {
	Method method = ImplementsInterfaceWithGenericAnnotatedMethod.class.getMethod(
			"foo", String.class);
	assertThat(MergedAnnotations.from(method, SearchStrategy.EXHAUSTIVE).get(
			Order.class).getDepth()).isEqualTo(0);
}
 
Example #19
Source File: MergedAnnotationsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void getWithAnnotationOnRoot() throws Exception {
	Method method = Leaf.class.getMethod("annotatedOnRoot");
	assertThat(method.getAnnotation(Order.class)).isNotNull();
	assertThat(MergedAnnotations.from(method).get(Order.class).getDepth()).isEqualTo(
			0);
	assertThat(MergedAnnotations.from(method, SearchStrategy.EXHAUSTIVE).get(
			Order.class).getDepth()).isEqualTo(0);
}
 
Example #20
Source File: MergedAnnotationsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void getDirectFromClassWithInheritedAnnotationInterface() {
	MergedAnnotation<?> annotation = MergedAnnotations.from(
			InheritedAnnotationInterface.class, SearchStrategy.EXHAUSTIVE).get(
					Transactional.class);
	assertThat(annotation.getAggregateIndex()).isEqualTo(0);
}
 
Example #21
Source File: MergedAnnotationsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void collectMultiValueMapFavorsInheritedComposedAnnotationsOverMoreLocallyDeclaredComposedAnnotations() {
	MultiValueMap<String, Object> map = MergedAnnotations.from(
			SubSubClassWithInheritedComposedAnnotation.class,
			SearchStrategy.INHERITED_ANNOTATIONS).stream(Transactional.class).collect(
					MergedAnnotationCollectors.toMultiValueMap());
	assertThat(map).contains(entry("qualifier", Arrays.asList("composed1")));
}
 
Example #22
Source File: MergedAnnotationsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void collectMultiValueMapFavorsInheritedAnnotationsOverMoreLocallyDeclaredComposedAnnotations() {
	MultiValueMap<String, Object> map = MergedAnnotations.from(
			SubSubClassWithInheritedAnnotation.class,
			SearchStrategy.INHERITED_ANNOTATIONS).stream(Transactional.class).collect(
					MergedAnnotationCollectors.toMultiValueMap());
	assertThat(map).contains(entry("qualifier", Arrays.asList("transactionManager")));
}
 
Example #23
Source File: MergedAnnotationsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void testJavaRepeatables(SearchStrategy searchStrategy, Class<?> element,
		String[] expected) {
	MyRepeatable[] annotations = searchStrategy == SearchStrategy.DIRECT
			? element.getDeclaredAnnotationsByType(MyRepeatable.class)
			: element.getAnnotationsByType(MyRepeatable.class);
	assertThat(Arrays.stream(annotations).map(MyRepeatable::value)).containsExactly(
			expected);
}
 
Example #24
Source File: AnnotationsScannerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void exhaustiveStrategyOnBridgedMethodScansAnnotations() throws Exception {
	Method source = BridgedMethod.class.getDeclaredMethod("method", String.class);
	assertThat(source.isBridge()).isFalse();
	assertThat(scan(source, SearchStrategy.EXHAUSTIVE)).containsExactly(
			"0:TestAnnotation1", "1:TestAnnotation2");
}
 
Example #25
Source File: MergedAnnotationsComposedOnSingleAnnotatedElementTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void exhaustiveStrategyMultipleNoninheritedComposedAnnotationsOnSuperclass() {
	MergedAnnotations annotations = MergedAnnotations.from(
			SubMultipleNoninheritedComposedCachesClass.class,
			SearchStrategy.EXHAUSTIVE);
	assertThat(stream(annotations, "value")).containsExactly("noninheritedCache1",
			"noninheritedCache2");
}
 
Example #26
Source File: MergedAnnotationsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void getRepeatableDeclaredOnClassWithMissingAttributeAliasDeclaration() {
	RepeatableContainers containers = RepeatableContainers.of(
			BrokenContextConfiguration.class, BrokenHierarchy.class);
	assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy(() ->
			MergedAnnotations.from(BrokenHierarchyClass.class, SearchStrategy.EXHAUSTIVE, containers,
					AnnotationFilter.PLAIN).get(BrokenHierarchy.class))
		.withMessageStartingWith("Attribute 'value' in")
		.withMessageContaining(BrokenContextConfiguration.class.getName())
		.withMessageContaining("@AliasFor 'location'");
}
 
Example #27
Source File: MergedAnnotationsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void getWithExhaustiveFromSubSubNonInheritedAnnotationInterface() {
	MergedAnnotation<?> annotation = MergedAnnotations.from(
			SubSubNonInheritedAnnotationInterface.class,
			SearchStrategy.EXHAUSTIVE).get(Order.class);
	assertThat(annotation.isPresent()).isTrue();
	assertThat(annotation.getAggregateIndex()).isEqualTo(2);
}
 
Example #28
Source File: MergedAnnotationsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void getWithExhaustiveInheritedFromInterfaceMethod()
		throws NoSuchMethodException {
	Method method = ConcreteClassWithInheritedAnnotation.class.getMethod(
			"handleFromInterface");
	MergedAnnotation<?> annotation = MergedAnnotations.from(method,
			SearchStrategy.EXHAUSTIVE).get(Order.class);
	assertThat(annotation.isPresent()).isTrue();
	assertThat(annotation.getAggregateIndex()).isEqualTo(1);
}
 
Example #29
Source File: MergedAnnotationsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void getDirectFromClassWithMetaCycleAnnotatedClassWithMissingTargetMetaAnnotation() {
	MergedAnnotation<?> annotation = MergedAnnotations.from(
			MetaCycleAnnotatedClass.class, SearchStrategy.EXHAUSTIVE).get(
					Component.class);
	assertThat(annotation.isPresent()).isFalse();
}
 
Example #30
Source File: MergedAnnotationsComposedOnSingleAnnotatedElementTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void inheritedStrategyMultipleNoninheritedComposedAnnotationsOnClass() {
	MergedAnnotations annotations = MergedAnnotations.from(
			MultipleNoninheritedComposedCachesClass.class,
			SearchStrategy.INHERITED_ANNOTATIONS);
	assertThat(stream(annotations, "value")).containsExactly("noninheritedCache1",
			"noninheritedCache2");
}