DATACMNS-825 - Allow usage of composed annotations using @AliasFor.
We now resolve composed annotation values using @AliasFor within AnnotationBasedPersistentProperty and BasicPersistentEntity. Nevertheless it is up to the individual store implementation to make use of this. Original pull request: #156.
This commit is contained in:
committed by
Oliver Gierke
parent
ae96301ab0
commit
a40e247c25
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2014 the original author or authors.
|
||||
* Copyright 2013-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -29,6 +29,8 @@ import javax.annotation.Nullable;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.data.annotation.AccessType;
|
||||
import org.springframework.data.annotation.AccessType.Type;
|
||||
import org.springframework.data.annotation.Id;
|
||||
@@ -220,6 +222,37 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase
|
||||
assertThat(field.get(MyAnnotation.class), is(nullValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-825
|
||||
*/
|
||||
@Test
|
||||
public void composedAnnotationWithAliasForGetCachedCorrectly() {
|
||||
|
||||
SamplePersistentProperty property = entity.getPersistentProperty("metaAliased");
|
||||
|
||||
// Assert direct annotations are cached on construction
|
||||
Map<Class<? extends Annotation>, Annotation> cache = getAnnotationCache(property);
|
||||
assertThat(cache.containsKey(MyComposedAnnotationUsingAliasFor.class), is(true));
|
||||
assertThat(cache.containsKey(MyAnnotation.class), is(false));
|
||||
|
||||
// Assert meta annotation is found and cached
|
||||
MyAnnotation annotation = property.findAnnotation(MyAnnotation.class);
|
||||
assertThat(annotation, is(notNullValue()));
|
||||
assertThat(cache.containsKey(MyAnnotation.class), is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-825
|
||||
*/
|
||||
@Test
|
||||
public void composedAnnotationWithAliasShouldHaveSynthesizedAttributeValues() {
|
||||
|
||||
SamplePersistentProperty property = entity.getPersistentProperty("metaAliased");
|
||||
|
||||
MyAnnotation annotation = property.findAnnotation(MyAnnotation.class);
|
||||
assertThat(AnnotationUtils.getValue(annotation), is((Object) "spring"));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Map<Class<? extends Annotation>, Annotation> getAnnotationCache(SamplePersistentProperty property) {
|
||||
return (Map<Class<? extends Annotation>, Annotation>) ReflectionTestUtils.getField(property, "annotationCache");
|
||||
@@ -248,6 +281,8 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase
|
||||
|
||||
@MyAnnotationAsMeta String meta;
|
||||
|
||||
@MyComposedAnnotationUsingAliasFor String metaAliased;
|
||||
|
||||
@MyAnnotation
|
||||
public String getGetter() {
|
||||
return getter;
|
||||
@@ -314,6 +349,15 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase
|
||||
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(value = { FIELD, METHOD })
|
||||
@MyAnnotation
|
||||
public static @interface MyComposedAnnotationUsingAliasFor {
|
||||
|
||||
@AliasFor(annotation = MyAnnotation.class, attribute = "value")
|
||||
String name() default "spring";
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(value = { FIELD, METHOD, ANNOTATION_TYPE })
|
||||
@Id
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2015 the original author or authors.
|
||||
* Copyright 2011-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,6 +19,8 @@ import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
@@ -30,6 +32,7 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
import org.springframework.data.annotation.CreatedBy;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
import org.springframework.data.annotation.LastModifiedBy;
|
||||
@@ -224,6 +227,17 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
|
||||
assertThat(entity.getPropertyAccessor(new Subtype()), is(notNullValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-825
|
||||
*/
|
||||
@Test
|
||||
public void returnsTypeAliasIfAnnotatedUsingComposedAnnotation() {
|
||||
|
||||
PersistentEntity<AliasEntityUsingComposedAnnotation, T> entity = createEntity(
|
||||
AliasEntityUsingComposedAnnotation.class);
|
||||
assertThat(entity.getTypeAlias(), is((Object) "bar"));
|
||||
}
|
||||
|
||||
private <S> BasicPersistentEntity<S, T> createEntity(Class<S> type) {
|
||||
return createEntity(type, null);
|
||||
}
|
||||
@@ -251,5 +265,18 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
|
||||
}
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@TypeAlias("foo")
|
||||
static @interface ComposedTypeAlias {
|
||||
|
||||
@AliasFor(annotation = TypeAlias.class, attribute = "value")
|
||||
String name() default "bar";
|
||||
}
|
||||
|
||||
@ComposedTypeAlias
|
||||
static class AliasEntityUsingComposedAnnotation {
|
||||
|
||||
}
|
||||
|
||||
static class Subtype extends Entity {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user