DATAKV-129 - Allow usage of @AliasFor with @KeySpace.
We now allow using Spring's meta-annotation programming model. Own meta-annotations can be composed using @KeySpace and @AliasFor annotation.
Example for a custom meta-annotation corresponding to @KeySpace("sessions") when it's used with @SessionsKeyspace class MyEntity {…}:
@Persistent
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
@KeySpace
public @interface SessionsKeyspace {
@AliasFor(annotation = KeySpace.class, attribute = "value")
String name() default "sessions";
}
Original pull request: #19.
This commit is contained in:
committed by
Mark Paluch
parent
2cce00f1fe
commit
070be6e47d
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 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.
|
||||
* 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.data.keyvalue;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
import org.springframework.data.annotation.Persistent;
|
||||
import org.springframework.data.keyvalue.annotation.KeySpace;
|
||||
|
||||
/**
|
||||
* Custom composed {@link Persistent} annotation using {@link AliasFor} on name attribute.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@Persistent
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.TYPE })
|
||||
@KeySpace
|
||||
public @interface CustomKeySpaceAnnotationWithAliasFor {
|
||||
|
||||
@AliasFor(annotation = KeySpace.class, attribute = "value")
|
||||
String name() default "";
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright 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.
|
||||
* 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.data.keyvalue;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.annotation.Persistent;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* A {@link Persistent} type with {@link CustomKeySpaceAnnotationWithAliasFor}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@CustomKeySpaceAnnotationWithAliasFor(name = "aliased")
|
||||
public class TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor {
|
||||
|
||||
@Id String id;
|
||||
String name;
|
||||
|
||||
public TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ObjectUtils.nullSafeHashCode(this.id);
|
||||
result = prime * result + ObjectUtils.nullSafeHashCode(this.name);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (!(obj instanceof TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor)) {
|
||||
return false;
|
||||
}
|
||||
TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor other = (TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor) obj;
|
||||
if (!ObjectUtils.nullSafeEquals(this.id, other.id)) {
|
||||
return false;
|
||||
}
|
||||
if (!ObjectUtils.nullSafeEquals(this.name, other.name)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2015 the original author or authors.
|
||||
* Copyright 2014-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.
|
||||
@@ -42,6 +42,7 @@ import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.keyvalue.SubclassOfTypeWithCustomComposedKeySpaceAnnotation;
|
||||
import org.springframework.data.keyvalue.TypeWithCustomComposedKeySpaceAnnotation;
|
||||
import org.springframework.data.keyvalue.TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor;
|
||||
import org.springframework.data.keyvalue.core.event.KeyValueEvent;
|
||||
import org.springframework.data.keyvalue.core.event.KeyValueEvent.AfterDeleteEvent;
|
||||
import org.springframework.data.keyvalue.core.event.KeyValueEvent.AfterDropKeySpaceEvent;
|
||||
@@ -69,6 +70,8 @@ public class KeyValueTemplateUnitTests {
|
||||
private static final Foo FOO_TWO = new Foo("two");
|
||||
private static final TypeWithCustomComposedKeySpaceAnnotation ALIASED = new TypeWithCustomComposedKeySpaceAnnotation(
|
||||
"super");
|
||||
private static final TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor ALIASED_USING_ALIAS_FOR = new TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor(
|
||||
"super");
|
||||
private static final SubclassOfTypeWithCustomComposedKeySpaceAnnotation SUBCLASS_OF_ALIASED = new SubclassOfTypeWithCustomComposedKeySpaceAnnotation(
|
||||
"sub");
|
||||
private static final KeyValueQuery<String> STRING_QUERY = new KeyValueQuery<String>("foo == 'two'");
|
||||
@@ -697,6 +700,17 @@ public class KeyValueTemplateUnitTests {
|
||||
assertThat(captor.getValue().getKeyspace(), is(Foo.class.getName()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAKV-129
|
||||
*/
|
||||
@Test
|
||||
public void insertShouldRespectTypeAliasUsingAliasFor() {
|
||||
|
||||
template.insert("1", ALIASED_USING_ALIAS_FOR);
|
||||
|
||||
verify(adapterMock, times(1)).put("1", ALIASED_USING_ALIAS_FOR, "aliased");
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
private void setEventsToPublish(Class<? extends KeyValueEvent>... events) {
|
||||
template.setEventTypesToPublish(new HashSet<Class<? extends KeyValueEvent>>(Arrays.asList(events)));
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
* Copyright 2014-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.
|
||||
@@ -25,6 +25,7 @@ import java.lang.annotation.Target;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
import org.springframework.data.annotation.Persistent;
|
||||
import org.springframework.data.keyvalue.TypeWithDirectKeySpaceAnnotation;
|
||||
import org.springframework.data.keyvalue.TypeWithInhteritedPersistentAnnotationNotHavingKeySpace;
|
||||
@@ -102,6 +103,22 @@ public class AnnotationBasedKeySpaceResolverUnitTests {
|
||||
assertThat(resolver.resolveKeySpace(TypeWithDirectKeySpaceAnnotation.class), is("rhaegar"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAKV-129
|
||||
*/
|
||||
@Test
|
||||
public void shouldResolveKeySpaceUsingAliasForCorrectly() {
|
||||
assertThat(resolver.resolveKeySpace(EntityWithSetKeySpaceUsingAliasFor.class), is("viserys"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAKV-129
|
||||
*/
|
||||
@Test
|
||||
public void shouldResolveKeySpaceUsingAliasForCorrectlyOnSubClass() {
|
||||
assertThat(resolver.resolveKeySpace(EntityWithInheritedKeySpaceUsingAliasFor.class), is("viserys"));
|
||||
}
|
||||
|
||||
@PersistentAnnotationWithExplicitKeySpace
|
||||
static class EntityWithDefaultKeySpace {
|
||||
|
||||
@@ -116,6 +133,15 @@ public class AnnotationBasedKeySpaceResolverUnitTests {
|
||||
|
||||
}
|
||||
|
||||
@PersistentAnnotationWithExplicitKeySpace(firstname = "viserys")
|
||||
static class EntityWithSetKeySpaceUsingAliasFor {
|
||||
|
||||
}
|
||||
|
||||
static class EntityWithInheritedKeySpaceUsingAliasFor extends EntityWithSetKeySpaceUsingAliasFor {
|
||||
|
||||
}
|
||||
|
||||
@Persistent
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.TYPE })
|
||||
@@ -127,6 +153,19 @@ public class AnnotationBasedKeySpaceResolverUnitTests {
|
||||
String lastnamne() default "targaryen";
|
||||
}
|
||||
|
||||
@Persistent
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.TYPE })
|
||||
@KeySpace
|
||||
static @interface PersistentAnnotationWithExplicitKeySpaceUsingAliasFor {
|
||||
|
||||
@AliasFor(annotation = KeySpace.class, attribute = "value")
|
||||
String firstname() default "daenerys";
|
||||
|
||||
String lastnamne() default "targaryen";
|
||||
|
||||
}
|
||||
|
||||
static class TypeWithoutKeySpace {
|
||||
|
||||
String foo;
|
||||
|
||||
Reference in New Issue
Block a user