Skip to content

Commit eee1bbe

Browse files
Merge pull request #46 from fastnsilver/update-extractor
Update assertions to be more clear (and correct)
2 parents ef10db9 + 34d3db8 commit eee1bbe

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

src/main/java/org/socialsignin/spring/data/dynamodb/repository/support/DynamoDBHashAndRangeKeyMethodExtractorImpl.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ public class DynamoDBHashAndRangeKeyMethodExtractorImpl<T> implements DynamoDBHa
3737

3838
private Field hashKeyField;
3939
private Field rangeKeyField;
40-
40+
4141
/**
4242
* Creates a new {@link DefaultJpaEntityMetadata} for the given domain type.
43-
*
43+
*
4444
* @param domainType
4545
* must not be {@literal null}.
4646
*/
@@ -49,7 +49,8 @@ public DynamoDBHashAndRangeKeyMethodExtractorImpl(final Class<T> idType) {
4949
Assert.notNull(idType, "Id type must not be null!");
5050
this.idType = idType;
5151
ReflectionUtils.doWithMethods(idType, new MethodCallback() {
52-
public void doWith(Method method) {
52+
@Override
53+
public void doWith(Method method) {
5354
if (method.getAnnotation(DynamoDBHashKey.class) != null) {
5455
Assert.isNull(hashKeyMethod, "Multiple methods annotated by @DynamoDBHashKey within type " + idType.getName()
5556
+ "!");
@@ -59,7 +60,8 @@ public void doWith(Method method) {
5960
}
6061
});
6162
ReflectionUtils.doWithFields(idType, new FieldCallback() {
62-
public void doWith(Field field) {
63+
@Override
64+
public void doWith(Field field) {
6365
if (field.getAnnotation(DynamoDBHashKey.class) != null) {
6466
Assert.isNull(field, "Multiple fields annotated by @DynamoDBHashKey within type " + idType.getName()
6567
+ "!");
@@ -70,7 +72,8 @@ public void doWith(Field field) {
7072
}
7173
});
7274
ReflectionUtils.doWithMethods(idType, new MethodCallback() {
73-
public void doWith(Method method) {
75+
@Override
76+
public void doWith(Method method) {
7477
if (method.getAnnotation(DynamoDBRangeKey.class) != null) {
7578
Assert.isNull(rangeKeyMethod,
7679
"Multiple methods annotated by @DynamoDBRangeKey within type " + idType.getName() + "!");
@@ -80,7 +83,8 @@ public void doWith(Method method) {
8083
}
8184
});
8285
ReflectionUtils.doWithFields(idType, new FieldCallback() {
83-
public void doWith(Field field) {
86+
@Override
87+
public void doWith(Field field) {
8488
if (field.getAnnotation(DynamoDBRangeKey.class) != null) {
8589
Assert.isNull(rangeKeyMethod,
8690
"Multiple methods annotated by @DynamoDBRangeKey within type " + idType.getName() + "!");
@@ -89,11 +93,18 @@ public void doWith(Field field) {
8993
}
9094
}
9195
});
92-
Assert.isTrue(hashKeyMethod != null || hashKeyField != null, "No method or field annotated by @DynamoDBHashKey within type " + idType.getName() + "!");
93-
Assert.isTrue(rangeKeyMethod != null || hashKeyField != null, "No method or field annotated by @DynamoDBRangeKey within type " + idType.getName() + "!");
94-
Assert.isTrue(hashKeyMethod == null || hashKeyField == null, "Both method and field annotated by @DynamoDBHashKey within type " + idType.getName() + "!");
95-
Assert.isTrue(rangeKeyMethod == null || hashKeyField == null, "Both method and field annotated by @DynamoDBRangeKey within type " + idType.getName() + "!");
96-
96+
if (hashKeyMethod == null && hashKeyField == null) {
97+
throw new IllegalArgumentException("No method or field annotated by @DynamoDBHashKey within type " + idType.getName() + "!");
98+
}
99+
if (rangeKeyMethod == null && hashKeyField == null) {
100+
throw new IllegalArgumentException("No method or field annotated by @DynamoDBRangeKey within type " + idType.getName() + "!");
101+
}
102+
if (hashKeyMethod != null && hashKeyField != null) {
103+
throw new IllegalArgumentException("Both method and field annotated by @DynamoDBHashKey within type " + idType.getName() + "!");
104+
}
105+
if(rangeKeyMethod != null && hashKeyField != null) {
106+
throw new IllegalArgumentException("Both method and field annotated by @DynamoDBRangeKey within type " + idType.getName() + "!");
107+
}
97108
}
98109

99110
@Override
@@ -111,7 +122,7 @@ public Method getHashKeyMethod() {
111122
public Method getRangeKeyMethod() {
112123
return rangeKeyMethod;
113124
}
114-
125+
115126
@Override
116127
public Field getHashKeyField() {
117128

0 commit comments

Comments
 (0)