Skip to content

Commit e9da1f1

Browse files
Alexandre Dutraolim7t
authored andcommitted
Reject unreadable or unwritable properties.
1 parent 8a324c2 commit e9da1f1

2 files changed

Lines changed: 41 additions & 6 deletions

File tree

driver-mapping/src/main/java/com/datastax/driver/mapping/PropertyMapper.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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 @@ class PropertyMapper {
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 @@ private String inferColumnName() {
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

driver-mapping/src/test/java/com/datastax/driver/mapping/MapperInvalidAnnotationsTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,36 @@ public void should_not_allow_PartitionKey_with_wrong_order() throws Exception {
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
}

0 commit comments

Comments
 (0)