kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 270ab72db05a49e508398839dc9c05e2ddb84a96527dc36fde2b1f151812d4e1 |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2016-06-01 11:36 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-07-22 09:47 -07:00 |
| Parents | 0518208fb53c |
2 files changed
modified driver-mapping/src/main/java/com/datastax/driver/mapping/PropertyMapper.java +9 −6
| @@ -28,6 +28,8 @@ | ||
| 28 | 28 | import java.util.Collection; |
| 29 | 29 | import java.util.Map; |
| 30 | 30 | |
| 31 | +import static com.google.common.base.Preconditions.checkState; | |
| 32 | + | |
| 31 | 33 | /** |
| 32 | 34 | * Maps a Java bean property to a table column or a UDT field. |
| 33 | 35 | * <p> |
| @@ -56,9 +58,13 @@ | ||
| 56 | 58 | getter = ReflectionUtils.findGetter(property); |
| 57 | 59 | setter = ReflectionUtils.findSetter(property); |
| 58 | 60 | annotations = ReflectionUtils.scanPropertyAnnotations(field, property); |
| 61 | + if (!isTransient()) { | |
| 62 | + checkState(field != null || getter != null, "Property '%s' is not readable", propertyName); | |
| 63 | + checkState(field != null || setter != null, "Property '%s' is not writable", propertyName); | |
| 64 | + } | |
| 59 | 65 | columnName = inferColumnName(); |
| 60 | 66 | position = inferPosition(); |
| 61 | - javaType = inferJavaType(property); | |
| 67 | + javaType = inferJavaType(); | |
| 62 | 68 | customCodec = createCustomCodec(); |
| 63 | 69 | } |
| 64 | 70 | |
| @@ -135,15 +141,12 @@ | ||
| 135 | 141 | } |
| 136 | 142 | |
| 137 | 143 | @SuppressWarnings("unchecked") |
| 138 | - private TypeToken<Object> inferJavaType(PropertyDescriptor property) { | |
| 144 | + private TypeToken<Object> inferJavaType() { | |
| 139 | 145 | Type type; |
| 140 | 146 | if (getter != null) |
| 141 | 147 | type = getter.getGenericReturnType(); |
| 142 | - else if (field != null) | |
| 143 | - type = field.getGenericType(); | |
| 144 | 148 | else |
| 145 | - // this will not work for generic types | |
| 146 | - type = property.getPropertyType(); | |
| 149 | + type = field.getGenericType(); | |
| 147 | 150 | return (TypeToken<Object>) TypeToken.of(type); |
| 148 | 151 | } |
| 149 | 152 |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperInvalidAnnotationsTest.java +32 −0
| @@ -253,4 +253,36 @@ | ||
| 253 | 253 | AnnotationParser.parseEntity(Invalid13.class, mappingManager); |
| 254 | 254 | } |
| 255 | 255 | |
| 256 | + @Table(name = "foo", keyspace = "ks") | |
| 257 | + static class Invalid14 { | |
| 258 | + | |
| 259 | + public void setNotReadable(int i) { | |
| 260 | + } | |
| 261 | + | |
| 262 | + } | |
| 263 | + | |
| 264 | + @Test(groups = "unit", expectedExceptions = IllegalStateException.class, | |
| 265 | + expectedExceptionsMessageRegExp = | |
| 266 | + "Property 'notReadable' is not readable") | |
| 267 | + public void should_not_allow_unreadable_property() throws Exception { | |
| 268 | + AnnotationParser.parseEntity(Invalid14.class, mappingManager); | |
| 269 | + } | |
| 270 | + | |
| 271 | + @Table(name = "foo", keyspace = "ks") | |
| 272 | + static class Invalid15 { | |
| 273 | + | |
| 274 | + public int getNotWritable() { | |
| 275 | + return 0; | |
| 276 | + } | |
| 277 | + | |
| 278 | + } | |
| 279 | + | |
| 280 | + @Test(groups = "unit", expectedExceptions = IllegalStateException.class, | |
| 281 | + expectedExceptionsMessageRegExp = | |
| 282 | + "Property 'notWritable' is not writable") | |
| 283 | + public void should_not_allow_unwritable_property() throws Exception { | |
| 284 | + AnnotationParser.parseEntity(Invalid15.class, mappingManager); | |
| 285 | + } | |
| 286 | + | |
| 287 | + | |
| 256 | 288 | } |