Elffuss BraidHub

kikocis/java-driver public

Reject unreadable or unwritable properties.
Changeset270ab72db05a49e508398839dc9c05e2ddb84a96527dc36fde2b1f151812d4e1
AuthorAlexandre Dutra <alexandre.dutra@datastax.com> · 2016-06-01 11:36 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2016-07-22 09:47 -07:00
Parents0518208fb53c

Browse the files at this changeset

2 files changed

modified driver-mapping/src/main/java/com/datastax/driver/mapping/PropertyMapper.java +9 −6
@@ -28,6 +28,8 @@
2828 import java.util.Collection;
2929 import java.util.Map;
3030
31+import static com.google.common.base.Preconditions.checkState;
32+
3133 /**
3234 * Maps a Java bean property to a table column or a UDT field.
3335 * <p>
@@ -56,9 +58,13 @@
5658 getter = ReflectionUtils.findGetter(property);
5759 setter = ReflectionUtils.findSetter(property);
5860 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+ }
5965 columnName = inferColumnName();
6066 position = inferPosition();
61- javaType = inferJavaType(property);
67+ javaType = inferJavaType();
6268 customCodec = createCustomCodec();
6369 }
6470
@@ -135,15 +141,12 @@
135141 }
136142
137143 @SuppressWarnings("unchecked")
138- private TypeToken<Object> inferJavaType(PropertyDescriptor property) {
144+ private TypeToken<Object> inferJavaType() {
139145 Type type;
140146 if (getter != null)
141147 type = getter.getGenericReturnType();
142- else if (field != null)
143- type = field.getGenericType();
144148 else
145- // this will not work for generic types
146- type = property.getPropertyType();
149+ type = field.getGenericType();
147150 return (TypeToken<Object>) TypeToken.of(type);
148151 }
149152
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperInvalidAnnotationsTest.java +32 −0
@@ -253,4 +253,36 @@
253253 AnnotationParser.parseEntity(Invalid13.class, mappingManager);
254254 }
255255
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+
256288 }