kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | f0d09a3f14f220997460ad61b7d613aeb5338ff458e977ca72836aa377888cc2 |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2016-06-21 17:30 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-07-22 09:47 -07:00 |
| Parents | 2f7c33606680 |
6 files changed
modified changelog/README.md +1 −0
| @@ -12,6 +12,7 @@ | ||
| 12 | 12 | - [improvement] JAVA-1192: Make EventDebouncer settings updatable at runtime. |
| 13 | 13 | - [new feature] JAVA-541: Add polymorphism support to object mapper. |
| 14 | 14 | - [new feature] JAVA-636: Allow @Column annotations on getters/setters as well as fields. |
| 15 | +- [new feature] JAVA-984: Allow non-void setters in object mapping. | |
| 15 | 16 | |
| 16 | 17 | Merged from 3.0.x branch: |
| 17 | 18 |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/AnnotationParser.java +2 −2
| @@ -114,7 +114,7 @@ | ||
| 114 | 114 | ? "col" + columnCounter.incrementAndGet() |
| 115 | 115 | : null; |
| 116 | 116 | |
| 117 | - PropertyMapper propertyMapper = new PropertyMapper(propertyName, alias, field, property); | |
| 117 | + PropertyMapper propertyMapper = new PropertyMapper(entityClass, propertyName, alias, field, property); | |
| 118 | 118 | |
| 119 | 119 | if (mappingManager.isCassandraV1 && propertyMapper.isComputed()) |
| 120 | 120 | throw new UnsupportedOperationException("Computed properties are not supported with native protocol v1"); |
| @@ -180,7 +180,7 @@ | ||
| 180 | 180 | java.lang.reflect.Field field = (java.lang.reflect.Field) entry.getValue()[0]; |
| 181 | 181 | PropertyDescriptor property = (PropertyDescriptor) entry.getValue()[1]; |
| 182 | 182 | |
| 183 | - PropertyMapper propertyMapper = new PropertyMapper(propertyName, null, field, property); | |
| 183 | + PropertyMapper propertyMapper = new PropertyMapper(udtClass, propertyName, null, field, property); | |
| 184 | 184 | |
| 185 | 185 | AnnotationChecks.validateAnnotations(propertyMapper, VALID_FIELD_ANNOTATIONS); |
| 186 | 186 |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/PropertyMapper.java +2 −2
| @@ -51,12 +51,12 @@ | ||
| 51 | 51 | private final Method setter; |
| 52 | 52 | private final Map<Class<? extends Annotation>, Annotation> annotations; |
| 53 | 53 | |
| 54 | - PropertyMapper(String propertyName, String alias, Field field, PropertyDescriptor property) { | |
| 54 | + PropertyMapper(Class<?> baseClass, String propertyName, String alias, Field field, PropertyDescriptor property) { | |
| 55 | 55 | this.propertyName = propertyName; |
| 56 | 56 | this.alias = alias; |
| 57 | 57 | this.field = field; |
| 58 | 58 | getter = ReflectionUtils.findGetter(property); |
| 59 | - setter = ReflectionUtils.findSetter(property); | |
| 59 | + setter = ReflectionUtils.findSetter(baseClass, property); | |
| 60 | 60 | annotations = ReflectionUtils.scanPropertyAnnotations(field, property); |
| 61 | 61 | if (field != null) |
| 62 | 62 | ReflectionUtils.tryMakeAccessible(field); |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/ReflectionUtils.java +16 −5
| @@ -75,7 +75,7 @@ | ||
| 75 | 75 | HashMap<String, Field> fields = new HashMap<String, Field>(); |
| 76 | 76 | for (Class<?> clazz = baseClass; !clazz.equals(Object.class); clazz = clazz.getSuperclass()) { |
| 77 | 77 | for (Field field : clazz.getDeclaredFields()) { |
| 78 | - if (field.getName().equals("class") || field.isSynthetic() || (field.getModifiers() & Modifier.STATIC) == Modifier.STATIC) | |
| 78 | + if (field.getName().equals("class") || field.isSynthetic() || Modifier.isStatic(field.getModifiers())) | |
| 79 | 79 | continue; |
| 80 | 80 | // never override a more specific field masking another one declared in a superclass |
| 81 | 81 | if (!fields.containsKey(field.getName())) |
| @@ -160,13 +160,24 @@ | ||
| 160 | 160 | return getter; |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | - static Method findSetter(PropertyDescriptor property) { | |
| 163 | + static Method findSetter(Class<?> baseClass, PropertyDescriptor property) { | |
| 164 | 164 | if (property == null) |
| 165 | 165 | return null; |
| 166 | 166 | Method setter = property.getWriteMethod(); |
| 167 | - if (setter == null) | |
| 168 | - return null; | |
| 169 | - return setter; | |
| 167 | + if (setter != null) | |
| 168 | + return setter; | |
| 169 | + String propertyName = property.getName(); | |
| 170 | + String setterName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); | |
| 171 | + // JAVA-984: look for a "relaxed" setter, ie. a setter whose return type may be anything | |
| 172 | + try { | |
| 173 | + setter = baseClass.getMethod(setterName, property.getPropertyType()); | |
| 174 | + if (!Modifier.isStatic(setter.getModifiers())) { | |
| 175 | + return setter; | |
| 176 | + } | |
| 177 | + } catch (NoSuchMethodException e) { | |
| 178 | + // ok | |
| 179 | + } | |
| 180 | + return null; | |
| 170 | 181 | } |
| 171 | 182 | |
| 172 | 183 | static void tryMakeAccessible(AccessibleObject object) { |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperPolymorphismTest.java +11 −1
| @@ -243,8 +243,11 @@ | ||
| 243 | 243 | return _radius; |
| 244 | 244 | } |
| 245 | 245 | |
| 246 | - public void setRadius(double radius) { | |
| 246 | + // builder-style setter; because the field isn't named in a standard way, | |
| 247 | + // if this setter is not detected the test would fail | |
| 248 | + public Circle setRadius(double radius) { | |
| 247 | 249 | this._radius = radius; |
| 250 | + return this; | |
| 248 | 251 | } |
| 249 | 252 | |
| 250 | 253 | @Override |
| @@ -430,6 +433,13 @@ | ||
| 430 | 433 | this.center = center; |
| 431 | 434 | } |
| 432 | 435 | |
| 436 | + // overridden builder-style setter | |
| 437 | + @Override | |
| 438 | + public Sphere setRadius(double radius) { | |
| 439 | + super.setRadius(radius); | |
| 440 | + return this; | |
| 441 | + } | |
| 442 | + | |
| 433 | 443 | @Override |
| 434 | 444 | public boolean equals(Object o) { |
| 435 | 445 | if (this == o) return true; |
modified manual/object_mapper/creating/README.md +30 −0
| @@ -119,6 +119,36 @@ | ||
| 119 | 119 | 1. Reads and writes are done through its public getters and setters; or |
| 120 | 120 | 2. The security manager, if any, grants the mapper access to it via [reflection][set-accessible]. |
| 121 | 121 | |
| 122 | +Note that, according to the [Java Beans specification][java-beans], a setter | |
| 123 | +must have a `void` return type; the driver, however, will consider as a setter any public method | |
| 124 | +having a matching signature (i.e., name and parameter types match those expected), | |
| 125 | +but having a different return type. This allows the usage of "fluent" setters that can | |
| 126 | +be chained together in a builder-pattern style: | |
| 127 | + | |
| 128 | +```java | |
| 129 | +@Table(name = "users") | |
| 130 | +public static class User { | |
| 131 | + | |
| 132 | + // fluent setters | |
| 133 | + | |
| 134 | + public User setId(UUID id) { | |
| 135 | + this.id = id; | |
| 136 | + return this; | |
| 137 | + } | |
| 138 | + | |
| 139 | + public User setName(String name) { | |
| 140 | + this.name = name; | |
| 141 | + return this; | |
| 142 | + } | |
| 143 | + | |
| 144 | +} | |
| 145 | + | |
| 146 | +// chained calls | |
| 147 | +User user = new User() | |
| 148 | + .setId(UUIDs.random()) | |
| 149 | + .setName("John Doe"); | |
| 150 | +``` | |
| 151 | + | |
| 122 | 152 | [table]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/mapping/annotations/Table.html |
| 123 | 153 | [case-sensitive]:http://docs.datastax.com/en/cql/3.3/cql/cql_reference/ucase-lcase_r.html |
| 124 | 154 | [consistency level]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/ConsistencyLevel.html |