Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Patch for Issue #31: Cannot add non-JOhm annotations to @Id field.

  • Loading branch information...
commit b63f97ab381c94eb9b7f4a616cf0ee0032664ec9 1 parent 8458442
@gsharma gsharma authored Jonathan Leibiusky committed
View
28 src/main/java/redis/clients/johm/JOhmUtils.java
@@ -1,5 +1,6 @@
package redis.clients.johm;
+import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
@@ -319,9 +320,18 @@ static Long checkValidId(final Object model) {
}
static void checkValidIdType(final Field field) {
- if (field.getAnnotations().length > 1) {
- throw new JOhmException(
- "Element annotated @Id cannot have any other annotations");
+ Annotation[] annotations = field.getAnnotations();
+ if (annotations.length > 1) {
+ for (Annotation annotation : annotations) {
+ Class<?> annotationType = annotation.annotationType();
+ if (annotationType.equals(Id.class)) {
+ continue;
+ }
+ if (JOHM_SUPPORTED_ANNOTATIONS.contains(annotationType)) {
+ throw new JOhmException(
+ "Element annotated @Id cannot have any other JOhm annotations");
+ }
+ }
}
Class<?> type = field.getType().getClass();
if (!type.isInstance(Long.class) || !type.isInstance(long.class)) {
@@ -448,6 +458,7 @@ public static boolean checkSupportedPrimitiveClazz(
}
private static final Set<Class<?>> JOHM_SUPPORTED_PRIMITIVES = new HashSet<Class<?>>();
+ private static final Set<Class<?>> JOHM_SUPPORTED_ANNOTATIONS = new HashSet<Class<?>>();
static {
JOHM_SUPPORTED_PRIMITIVES.add(String.class);
JOHM_SUPPORTED_PRIMITIVES.add(Byte.class);
@@ -468,5 +479,16 @@ public static boolean checkSupportedPrimitiveClazz(
JOHM_SUPPORTED_PRIMITIVES.add(boolean.class);
JOHM_SUPPORTED_PRIMITIVES.add(BigDecimal.class);
JOHM_SUPPORTED_PRIMITIVES.add(BigInteger.class);
+
+ JOHM_SUPPORTED_ANNOTATIONS.add(Array.class);
+ JOHM_SUPPORTED_ANNOTATIONS.add(Attribute.class);
+ JOHM_SUPPORTED_ANNOTATIONS.add(CollectionList.class);
+ JOHM_SUPPORTED_ANNOTATIONS.add(CollectionMap.class);
+ JOHM_SUPPORTED_ANNOTATIONS.add(CollectionSet.class);
+ JOHM_SUPPORTED_ANNOTATIONS.add(CollectionSortedSet.class);
+ JOHM_SUPPORTED_ANNOTATIONS.add(Id.class);
+ JOHM_SUPPORTED_ANNOTATIONS.add(Indexed.class);
+ JOHM_SUPPORTED_ANNOTATIONS.add(Model.class);
+ JOHM_SUPPORTED_ANNOTATIONS.add(Reference.class);
}
}
View
8 src/test/java/redis/clients/johm/BasicPersistenceTest.java
@@ -5,6 +5,7 @@
import org.junit.Test;
import redis.clients.johm.models.Country;
+import redis.clients.johm.models.FaultyModel;
import redis.clients.johm.models.Item;
import redis.clients.johm.models.User;
@@ -174,6 +175,13 @@ public void shouldNotPersistWithoutModel() {
JOhm.save(dummyNest);
}
+ @Test(expected = JOhmException.class)
+ public void shouldNotPersistModelWithOtherJOhmIdAnnotations() {
+ FaultyModel badModel = new FaultyModel();
+ badModel.setType("horribleId");
+ JOhm.save(badModel);
+ }
+
@Test
public void shouldHandleReferences() {
User user = new User();
View
1  src/test/java/redis/clients/johm/models/Country.java
@@ -7,6 +7,7 @@
@Model
public class Country {
@Id
+ @TestAnnotation
private Long id;
@Attribute
private String name;
View
27 src/test/java/redis/clients/johm/models/FaultyModel.java
@@ -0,0 +1,27 @@
+package redis.clients.johm.models;
+
+import redis.clients.johm.Array;
+import redis.clients.johm.Attribute;
+import redis.clients.johm.Id;
+import redis.clients.johm.Model;
+
+@Model
+public class FaultyModel {
+ @Id
+ @Array(of = long.class, length = 1)
+ private long id;
+ @Attribute
+ private String type;
+
+ public long getId() {
+ return id;
+ }
+
+ public void setType(String type) {
+ this.type = type;
+ }
+
+ public String getType() {
+ return type;
+ }
+}
View
11 src/test/java/redis/clients/johm/models/TestAnnotation.java
@@ -0,0 +1,11 @@
+package redis.clients.johm.models;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+public @interface TestAnnotation {
+}

0 comments on commit b63f97a

Please sign in to comment.
Something went wrong with that request. Please try again.