kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 845bfcad348883d100073f0c47d56679d87194784d41e8d71b49622c12b998da |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2016-05-27 17:17 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-07-22 09:38 -07:00 |
| Parents | 4c920eab788b |
12 files changed
modified changelog/README.md +1 −0
| @@ -10,6 +10,7 @@ | ||
| 10 | 10 | - [new feature] JAVA-1157: Allow asynchronous paging of Mapper Result. |
| 11 | 11 | - [improvement] JAVA-1212: Don't retry non-idempotent statements by default. |
| 12 | 12 | - [improvement] JAVA-1192: Make EventDebouncer settings updatable at runtime. |
| 13 | +- [new feature] JAVA-541: Add polymorphism support to object mapper. | |
| 13 | 14 | |
| 14 | 15 | Merged from 3.0.x branch: |
| 15 | 16 |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/AnnotationChecks.java +48 −14
| @@ -15,11 +15,12 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.mapping; |
| 17 | 17 | |
| 18 | +import com.datastax.driver.mapping.annotations.Column; | |
| 18 | 19 | import com.datastax.driver.mapping.annotations.Computed; |
| 19 | 20 | import com.datastax.driver.mapping.annotations.Table; |
| 20 | 21 | |
| 21 | 22 | import java.lang.annotation.Annotation; |
| 22 | -import java.lang.reflect.Field; | |
| 23 | +import java.util.*; | |
| 23 | 24 | |
| 24 | 25 | /** |
| 25 | 26 | * Various checks on mapping annotations. |
| @@ -45,9 +46,12 @@ | ||
| 45 | 46 | return instance; |
| 46 | 47 | } |
| 47 | 48 | |
| 49 | + @SuppressWarnings("unchecked") | |
| 48 | 50 | private static void validateAnnotations(Class<?> clazz, Class<? extends Annotation> allowed) { |
| 49 | 51 | @SuppressWarnings("unchecked") |
| 50 | - Class<? extends Annotation> invalid = validateAnnotations(clazz.getAnnotations(), allowed); | |
| 52 | + Set<Annotation> classAnnotations = new HashSet<Annotation>(); | |
| 53 | + Collections.addAll(classAnnotations, clazz.getAnnotations()); | |
| 54 | + Class<? extends Annotation> invalid = validateAnnotations(classAnnotations, allowed); | |
| 51 | 55 | if (invalid != null) |
| 52 | 56 | throw new IllegalArgumentException(String.format("Cannot have both @%s and @%s on type %s", |
| 53 | 57 | allowed.getSimpleName(), invalid.getSimpleName(), |
| @@ -57,19 +61,18 @@ | ||
| 57 | 61 | /** |
| 58 | 62 | * Checks that a field is only annotated with the given mapping annotations, and that its "frozen" annotations are valid. |
| 59 | 63 | */ |
| 60 | - static void validateAnnotations(Field field, String classDescription, Class<? extends Annotation>... allowed) { | |
| 61 | - Class<? extends Annotation> invalid = validateAnnotations(field.getAnnotations(), allowed); | |
| 64 | + static void validateAnnotations(MappedProperty<?> property, Class<? extends Annotation>... allowed) { | |
| 65 | + Class<? extends Annotation> invalid = validateAnnotations(property.annotations(), allowed); | |
| 62 | 66 | if (invalid != null) |
| 63 | - throw new IllegalArgumentException(String.format("Annotation @%s is not allowed on field %s of %s %s", | |
| 67 | + throw new IllegalArgumentException(String.format("Annotation @%s is not allowed on property %s", | |
| 64 | 68 | invalid.getSimpleName(), |
| 65 | - field.getName(), classDescription, | |
| 66 | - field.getDeclaringClass().getName())); | |
| 67 | - | |
| 68 | - checkValidComputed(field); | |
| 69 | + property)); | |
| 70 | + checkValidPrimaryKey(property); | |
| 71 | + checkValidComputed(property); | |
| 69 | 72 | } |
| 70 | 73 | |
| 71 | 74 | // Returns the offending annotation if there is one |
| 72 | - private static Class<? extends Annotation> validateAnnotations(Annotation[] annotations, Class<? extends Annotation>... allowed) { | |
| 75 | + private static Class<? extends Annotation> validateAnnotations(Collection<Annotation> annotations, Class<? extends Annotation>... allowed) { | |
| 73 | 76 | for (Annotation annotation : annotations) { |
| 74 | 77 | Class<? extends Annotation> actual = annotation.annotationType(); |
| 75 | 78 | if (actual.getPackage().equals(MAPPING_PACKAGE) && !contains(allowed, actual)) |
| @@ -85,10 +88,41 @@ | ||
| 85 | 88 | return false; |
| 86 | 89 | } |
| 87 | 90 | |
| 88 | - static void checkValidComputed(Field field) { | |
| 89 | - Computed computed = field.getAnnotation(Computed.class); | |
| 90 | - if (computed != null && computed.value().isEmpty()) { | |
| 91 | - throw new IllegalArgumentException(String.format("Field %s: attribute 'value' of annotation @Computed is mandatory for computed fields", field.getName())); | |
| 91 | + private static void checkValidPrimaryKey(MappedProperty<?> property) { | |
| 92 | + if (property.isPartitionKey() && property.isClusteringColumn()) | |
| 93 | + throw new IllegalArgumentException("Property " + property.name() + " cannot have both the @PartitionKey and @ClusteringColumn annotations"); | |
| 94 | + } | |
| 95 | + | |
| 96 | + private static void checkValidComputed(MappedProperty<?> property) { | |
| 97 | + if (property.isComputed()) { | |
| 98 | + Computed computed = property.annotation(Computed.class); | |
| 99 | + if (computed.value().isEmpty()) { | |
| 100 | + throw new IllegalArgumentException(String.format("Property %s: attribute 'value' of annotation @Computed is mandatory for computed properties", property.name())); | |
| 101 | + } | |
| 102 | + if (property.hasAnnotation(Column.class)) { | |
| 103 | + throw new IllegalArgumentException("Cannot use @Column and @Computed on the same property"); | |
| 104 | + } | |
| 105 | + } | |
| 106 | + } | |
| 107 | + | |
| 108 | + static <T> void validatePrimaryKeyOnUDT(MappedProperty<T> property) { | |
| 109 | + switch (property.kind()) { | |
| 110 | + case PARTITION_KEY: | |
| 111 | + throw new IllegalArgumentException("Annotation @PartitionKey is not allowed in a class annotated by @UDT"); | |
| 112 | + case CLUSTERING_COLUMN: | |
| 113 | + throw new IllegalArgumentException("Annotation @ClusteringColumn is not allowed in a class annotated by @UDT"); | |
| 114 | + default: | |
| 115 | + break; | |
| 116 | + } | |
| 117 | + } | |
| 118 | + | |
| 119 | + static <T> void validateOrder(List<MappedProperty<T>> properties, String annotation) { | |
| 120 | + for (int i = 0; i < properties.size(); i++) { | |
| 121 | + MappedProperty<?> property = properties.get(i); | |
| 122 | + int pos = property.position(); | |
| 123 | + if (pos != i) | |
| 124 | + throw new IllegalArgumentException(String.format("Invalid ordering value %d for annotation %s of column %s, was expecting %d", | |
| 125 | + pos, annotation, property.name(), i)); | |
| 92 | 126 | } |
| 93 | 127 | } |
| 94 | 128 | } |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/AnnotationParser.java +68 −155
| @@ -25,30 +25,30 @@ | ||
| 25 | 25 | import com.google.common.collect.Maps; |
| 26 | 26 | import com.google.common.reflect.TypeToken; |
| 27 | 27 | |
| 28 | +import java.beans.BeanInfo; | |
| 29 | +import java.beans.PropertyDescriptor; | |
| 28 | 30 | import java.lang.annotation.Annotation; |
| 29 | -import java.lang.reflect.Field; | |
| 30 | -import java.lang.reflect.*; | |
| 31 | -import java.util.*; | |
| 31 | +import java.lang.reflect.Method; | |
| 32 | +import java.lang.reflect.ParameterizedType; | |
| 33 | +import java.lang.reflect.Type; | |
| 34 | +import java.util.ArrayList; | |
| 35 | +import java.util.Collections; | |
| 36 | +import java.util.List; | |
| 37 | +import java.util.Map; | |
| 32 | 38 | import java.util.concurrent.atomic.AtomicInteger; |
| 33 | 39 | |
| 34 | 40 | import static com.datastax.driver.core.Metadata.quote; |
| 35 | 41 | |
| 36 | 42 | /** |
| 37 | - * Static metods that facilitates parsing class annotations into the corresponding {@link EntityMapper}. | |
| 43 | + * Static methods that facilitates parsing class annotations into the corresponding {@link EntityMapper}. | |
| 38 | 44 | */ |
| 45 | +@SuppressWarnings("unchecked") | |
| 39 | 46 | class AnnotationParser { |
| 40 | 47 | |
| 41 | - private static final Comparator<Field> fieldComparator = new Comparator<Field>() { | |
| 42 | - @Override | |
| 43 | - public int compare(Field f1, Field f2) { | |
| 44 | - return position(f1) - position(f2); | |
| 45 | - } | |
| 46 | - }; | |
| 47 | - | |
| 48 | 48 | private AnnotationParser() { |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | - public static <T> EntityMapper<T> parseEntity(Class<T> entityClass, EntityMapper.Factory factory, MappingManager mappingManager) { | |
| 51 | + static <T> EntityMapper<T> parseEntity(final Class<T> entityClass, EntityMapper.Factory factory, MappingManager mappingManager) { | |
| 52 | 52 | Table table = AnnotationChecks.getTypeAnnotation(Table.class, entityClass); |
| 53 | 53 | |
| 54 | 54 | String ksName = table.caseSensitiveKeyspace() ? table.keyspace() : table.keyspace().toLowerCase(); |
| @@ -73,59 +73,64 @@ | ||
| 73 | 73 | if (tableMetadata == null) |
| 74 | 74 | throw new IllegalArgumentException(String.format("Table %s does not exist in keyspace %s", tableName, ksName)); |
| 75 | 75 | |
| 76 | - List<Field> pks = new ArrayList<Field>(); | |
| 77 | - List<Field> ccs = new ArrayList<Field>(); | |
| 78 | - List<Field> rgs = new ArrayList<Field>(); | |
| 76 | + List<MappedProperty<T>> pks = new ArrayList<MappedProperty<T>>(); | |
| 77 | + List<MappedProperty<T>> ccs = new ArrayList<MappedProperty<T>>(); | |
| 78 | + List<MappedProperty<T>> rgs = new ArrayList<MappedProperty<T>>(); | |
| 79 | + | |
| 80 | + BeanInfo beanInfo = ReflectionUtils.getBeanInfo(entityClass); | |
| 81 | + | |
| 82 | + for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) { | |
| 79 | 83 | |
| 80 | - for (Field field : entityClass.getDeclaredFields()) { | |
| 81 | - if (field.isSynthetic() || (field.getModifiers() & Modifier.STATIC) == Modifier.STATIC) | |
| 84 | + if (descriptor.getName().equals("class")) | |
| 82 | 85 | continue; |
| 83 | 86 | |
| 84 | - if (mappingManager.isCassandraV1 && field.getAnnotation(Computed.class) != null) | |
| 85 | - throw new UnsupportedOperationException("Computed fields are not supported with native protocol v1"); | |
| 87 | + MappedProperty<T> property = new MappedProperty<T>(descriptor, entityClass); | |
| 86 | 88 | |
| 87 | - AnnotationChecks.validateAnnotations(field, "entity", | |
| 89 | + if (mappingManager.isCassandraV1 && property.isComputed()) | |
| 90 | + throw new UnsupportedOperationException("Computed properties are not supported with native protocol v1"); | |
| 91 | + | |
| 92 | + AnnotationChecks.validateAnnotations(property, | |
| 88 | 93 | Column.class, ClusteringColumn.class, Frozen.class, FrozenKey.class, |
| 89 | 94 | FrozenValue.class, PartitionKey.class, Transient.class, Computed.class); |
| 90 | 95 | |
| 91 | - if (field.getAnnotation(Transient.class) != null) | |
| 96 | + if (property.isTransient()) | |
| 92 | 97 | continue; |
| 93 | 98 | |
| 94 | - switch (kind(field)) { | |
| 99 | + switch (property.kind()) { | |
| 95 | 100 | case PARTITION_KEY: |
| 96 | - pks.add(field); | |
| 101 | + pks.add(property); | |
| 97 | 102 | break; |
| 98 | 103 | case CLUSTERING_COLUMN: |
| 99 | - ccs.add(field); | |
| 104 | + ccs.add(property); | |
| 100 | 105 | break; |
| 101 | 106 | default: |
| 102 | - rgs.add(field); | |
| 107 | + rgs.add(property); | |
| 103 | 108 | break; |
| 104 | 109 | } |
| 105 | 110 | } |
| 106 | 111 | |
| 107 | 112 | AtomicInteger columnCounter = mappingManager.isCassandraV1 ? null : new AtomicInteger(0); |
| 108 | 113 | |
| 109 | - Collections.sort(pks, fieldComparator); | |
| 110 | - Collections.sort(ccs, fieldComparator); | |
| 114 | + Collections.sort(pks); | |
| 115 | + Collections.sort(ccs); | |
| 111 | 116 | |
| 112 | - validateOrder(pks, "@PartitionKey"); | |
| 113 | - validateOrder(ccs, "@ClusteringColumn"); | |
| 117 | + AnnotationChecks.validateOrder(pks, "@PartitionKey"); | |
| 118 | + AnnotationChecks.validateOrder(ccs, "@ClusteringColumn"); | |
| 114 | 119 | |
| 115 | 120 | mapper.addColumns( |
| 116 | - createColumnMappers(pks, factory, mapper.entityClass, mappingManager, columnCounter, tableMetadata, ksName, tableName), | |
| 117 | - createColumnMappers(ccs, factory, mapper.entityClass, mappingManager, columnCounter, tableMetadata, ksName, tableName), | |
| 118 | - createColumnMappers(rgs, factory, mapper.entityClass, mappingManager, columnCounter, tableMetadata, ksName, tableName)); | |
| 121 | + createColumnMappers(pks, factory, mappingManager, columnCounter, tableMetadata, ksName, tableName), | |
| 122 | + createColumnMappers(ccs, factory, mappingManager, columnCounter, tableMetadata, ksName, tableName), | |
| 123 | + createColumnMappers(rgs, factory, mappingManager, columnCounter, tableMetadata, ksName, tableName)); | |
| 119 | 124 | return mapper; |
| 120 | 125 | } |
| 121 | 126 | |
| 122 | - private static <T> List<ColumnMapper<T>> createColumnMappers(List<Field> fields, EntityMapper.Factory factory, Class<T> klass, MappingManager mappingManager, AtomicInteger columnCounter, TableMetadata tableMetadata, String ksName, String tableName) { | |
| 123 | - List<ColumnMapper<T>> mappers = new ArrayList<ColumnMapper<T>>(fields.size()); | |
| 124 | - for (int i = 0; i < fields.size(); i++) { | |
| 125 | - Field field = fields.get(i); | |
| 126 | - int pos = position(field); | |
| 127 | - ColumnMapper<T> columnMapper = factory.createColumnMapper(klass, field, pos < 0 ? i : pos, mappingManager, columnCounter); | |
| 128 | - if (columnMapper.kind == ColumnMapper.Kind.COMPUTED || tableMetadata.getColumn(columnMapper.getColumnName()) != null) | |
| 127 | + private static <T> List<ColumnMapper<T>> createColumnMappers(List<MappedProperty<T>> properties, EntityMapper.Factory factory, MappingManager mappingManager, AtomicInteger columnCounter, TableMetadata tableMetadata, String ksName, String tableName) { | |
| 128 | + List<ColumnMapper<T>> mappers = new ArrayList<ColumnMapper<T>>(properties.size()); | |
| 129 | + for (int i = 0; i < properties.size(); i++) { | |
| 130 | + MappedProperty<T> property = properties.get(i); | |
| 131 | + int pos = property.position(); | |
| 132 | + ColumnMapper<T> columnMapper = factory.createColumnMapper(property, pos < 0 ? i : pos, mappingManager, columnCounter); | |
| 133 | + if (property.isComputed() || tableMetadata.getColumn(columnMapper.getColumnName()) != null) | |
| 129 | 134 | mappers.add(columnMapper); |
| 130 | 135 | else |
| 131 | 136 | throw new IllegalArgumentException(String.format("Column %s does not exist in table %s.%s", |
| @@ -134,7 +139,7 @@ | ||
| 134 | 139 | return mappers; |
| 135 | 140 | } |
| 136 | 141 | |
| 137 | - public static <T> MappedUDTCodec<T> parseUDT(Class<T> udtClass, EntityMapper.Factory factory, MappingManager mappingManager) { | |
| 142 | + static <T> MappedUDTCodec<T> parseUDT(Class<T> udtClass, EntityMapper.Factory factory, MappingManager mappingManager) { | |
| 138 | 143 | UDT udt = AnnotationChecks.getTypeAnnotation(UDT.class, udtClass); |
| 139 | 144 | |
| 140 | 145 | String ksName = udt.caseSensitiveKeyspace() ? udt.keyspace() : udt.keyspace().toLowerCase(); |
| @@ -154,39 +159,37 @@ | ||
| 154 | 159 | if (userType == null) |
| 155 | 160 | throw new IllegalArgumentException(String.format("User type %s does not exist in keyspace %s", udtName, ksName)); |
| 156 | 161 | |
| 157 | - List<Field> columns = new ArrayList<Field>(); | |
| 162 | + List<MappedProperty<T>> columns = new ArrayList<MappedProperty<T>>(); | |
| 163 | + | |
| 164 | + BeanInfo beanInfo = ReflectionUtils.getBeanInfo(udtClass); | |
| 158 | 165 | |
| 159 | - for (Field field : udtClass.getDeclaredFields()) { | |
| 160 | - if (field.isSynthetic() || (field.getModifiers() & Modifier.STATIC) == Modifier.STATIC) | |
| 166 | + for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) { | |
| 167 | + | |
| 168 | + if (descriptor.getName().equals("class")) | |
| 161 | 169 | continue; |
| 162 | 170 | |
| 163 | - AnnotationChecks.validateAnnotations(field, "UDT", | |
| 164 | - com.datastax.driver.mapping.annotations.Field.class, Frozen.class, FrozenKey.class, | |
| 171 | + MappedProperty<T> property = new MappedProperty<T>(descriptor, udtClass); | |
| 172 | + | |
| 173 | + AnnotationChecks.validateAnnotations(property, | |
| 174 | + Field.class, Frozen.class, FrozenKey.class, | |
| 165 | 175 | FrozenValue.class, Transient.class); |
| 166 | 176 | |
| 167 | - if (field.getAnnotation(Transient.class) != null) | |
| 177 | + if (property.isTransient()) | |
| 168 | 178 | continue; |
| 169 | 179 | |
| 170 | - switch (kind(field)) { | |
| 171 | - case PARTITION_KEY: | |
| 172 | - throw new IllegalArgumentException("Annotation @PartitionKey is not allowed in a class annotated by @UDT"); | |
| 173 | - case CLUSTERING_COLUMN: | |
| 174 | - throw new IllegalArgumentException("Annotation @ClusteringColumn is not allowed in a class annotated by @UDT"); | |
| 175 | - default: | |
| 176 | - columns.add(field); | |
| 177 | - break; | |
| 178 | - } | |
| 180 | + AnnotationChecks.validatePrimaryKeyOnUDT(property); | |
| 181 | + columns.add(property); | |
| 179 | 182 | } |
| 180 | 183 | Map<String, ColumnMapper<T>> columnMappers = createFieldMappers(columns, factory, udtClass, mappingManager, userType, ksName); |
| 181 | 184 | return new MappedUDTCodec<T>(userType, udtClass, columnMappers, mappingManager); |
| 182 | 185 | } |
| 183 | 186 | |
| 184 | - private static <T> Map<String, ColumnMapper<T>> createFieldMappers(List<Field> fields, EntityMapper.Factory factory, Class<T> klass, MappingManager mappingManager, UserType userType, String ksName) { | |
| 185 | - Map<String, ColumnMapper<T>> mappers = Maps.newHashMapWithExpectedSize(fields.size()); | |
| 186 | - for (int i = 0; i < fields.size(); i++) { | |
| 187 | - Field field = fields.get(i); | |
| 188 | - int pos = position(field); | |
| 189 | - ColumnMapper<T> mapper = factory.createColumnMapper(klass, field, pos < 0 ? i : pos, mappingManager, null); | |
| 187 | + private static <T> Map<String, ColumnMapper<T>> createFieldMappers(List<MappedProperty<T>> properties, EntityMapper.Factory factory, Class<T> udtClass, MappingManager mappingManager, UserType userType, String ksName) { | |
| 188 | + Map<String, ColumnMapper<T>> mappers = Maps.newHashMapWithExpectedSize(properties.size()); | |
| 189 | + for (int i = 0; i < properties.size(); i++) { | |
| 190 | + MappedProperty<T> property = properties.get(i); | |
| 191 | + int pos = property.position(); | |
| 192 | + ColumnMapper<T> mapper = factory.createColumnMapper(property, pos < 0 ? i : pos, mappingManager, null); | |
| 190 | 193 | if (userType.contains(mapper.getColumnName())) |
| 191 | 194 | mappers.put(mapper.getColumnName(), mapper); |
| 192 | 195 | else |
| @@ -196,104 +199,14 @@ | ||
| 196 | 199 | return mappers; |
| 197 | 200 | } |
| 198 | 201 | |
| 199 | - private static void validateOrder(List<Field> fields, String annotation) { | |
| 200 | - for (int i = 0; i < fields.size(); i++) { | |
| 201 | - Field field = fields.get(i); | |
| 202 | - int pos = position(field); | |
| 203 | - if (pos != i) | |
| 204 | - throw new IllegalArgumentException(String.format("Invalid ordering value %d for annotation %s of column %s, was expecting %d", | |
| 205 | - pos, annotation, field.getName(), i)); | |
| 206 | - } | |
| 207 | - } | |
| 208 | - | |
| 209 | - private static int position(Field field) { | |
| 210 | - switch (kind(field)) { | |
| 211 | - case PARTITION_KEY: | |
| 212 | - return field.getAnnotation(PartitionKey.class).value(); | |
| 213 | - case CLUSTERING_COLUMN: | |
| 214 | - return field.getAnnotation(ClusteringColumn.class).value(); | |
| 215 | - default: | |
| 216 | - return -1; | |
| 217 | - } | |
| 218 | - } | |
| 219 | - | |
| 220 | - public static ColumnMapper.Kind kind(Field field) { | |
| 221 | - PartitionKey pk = field.getAnnotation(PartitionKey.class); | |
| 222 | - ClusteringColumn cc = field.getAnnotation(ClusteringColumn.class); | |
| 223 | - Computed comp = field.getAnnotation(Computed.class); | |
| 224 | - if (pk != null && cc != null) | |
| 225 | - throw new IllegalArgumentException("Field " + field.getName() + " cannot have both the @PartitionKey and @ClusteringColumn annotations"); | |
| 226 | - | |
| 227 | - if (pk != null) { | |
| 228 | - return ColumnMapper.Kind.PARTITION_KEY; | |
| 229 | - } | |
| 230 | - if (cc != null) { | |
| 231 | - return ColumnMapper.Kind.CLUSTERING_COLUMN; | |
| 232 | - } | |
| 233 | - if (comp != null) { | |
| 234 | - return ColumnMapper.Kind.COMPUTED; | |
| 235 | - } | |
| 236 | - return ColumnMapper.Kind.REGULAR; | |
| 237 | - } | |
| 238 | - | |
| 239 | - public static String columnName(Field field) { | |
| 240 | - Column column = field.getAnnotation(Column.class); | |
| 241 | - Computed computedField = field.getAnnotation(Computed.class); | |
| 242 | - if (column != null && !column.name().isEmpty()) { | |
| 243 | - if (computedField != null) { | |
| 244 | - throw new IllegalArgumentException("Cannot use @Column and @Computed on the same field"); | |
| 245 | - } | |
| 246 | - return column.caseSensitive() ? column.name() : column.name().toLowerCase(); | |
| 247 | - } | |
| 248 | - | |
| 249 | - com.datastax.driver.mapping.annotations.Field udtField = field.getAnnotation(com.datastax.driver.mapping.annotations.Field.class); | |
| 250 | - if (udtField != null && !udtField.name().isEmpty()) { | |
| 251 | - return udtField.caseSensitive() ? udtField.name() : udtField.name().toLowerCase(); | |
| 252 | - } | |
| 253 | 202 | |
| 254 | - if (computedField != null) { | |
| 255 | - return computedField.value(); | |
| 256 | - } | |
| 257 | - | |
| 258 | - return field.getName().toLowerCase(); | |
| 259 | - } | |
| 260 | - | |
| 261 | - public static String newAlias(Field field, int columnNumber) { | |
| 203 | + static String newAlias(int columnNumber) { | |
| 262 | 204 | return "col" + columnNumber; |
| 263 | 205 | |
| 264 | 206 | } |
| 265 | 207 | |
| 266 | - public static TypeCodec<Object> customCodec(Field field) { | |
| 267 | - Class<? extends TypeCodec<?>> codecClass = getCodecClass(field); | |
| 268 | - | |
| 269 | - if (codecClass.equals(Defaults.NoCodec.class)) | |
| 270 | - return null; | |
| 271 | - | |
| 272 | - try { | |
| 273 | - @SuppressWarnings("unchecked") | |
| 274 | - TypeCodec<Object> instance = (TypeCodec<Object>) codecClass.newInstance(); | |
| 275 | - return instance; | |
| 276 | - } catch (Exception e) { | |
| 277 | - throw new IllegalArgumentException(String.format( | |
| 278 | - "Cannot create an instance of custom codec %s for field %s", | |
| 279 | - codecClass, field | |
| 280 | - ), e); | |
| 281 | - } | |
| 282 | - } | |
| 283 | - | |
| 284 | - private static Class<? extends TypeCodec<?>> getCodecClass(Field field) { | |
| 285 | - Column column = field.getAnnotation(Column.class); | |
| 286 | - if (column != null) | |
| 287 | - return column.codec(); | |
| 288 | - | |
| 289 | - com.datastax.driver.mapping.annotations.Field udtField = field.getAnnotation(com.datastax.driver.mapping.annotations.Field.class); | |
| 290 | - if (udtField != null) | |
| 291 | - return udtField.codec(); | |
| 292 | - | |
| 293 | - return Defaults.NoCodec.class; | |
| 294 | - } | |
| 295 | 208 | |
| 296 | - public static <T> AccessorMapper<T> parseAccessor(Class<T> accClass, AccessorMapper.Factory factory, MappingManager mappingManager) { | |
| 209 | + static <T> AccessorMapper<T> parseAccessor(Class<T> accClass, AccessorMapper.Factory factory, MappingManager mappingManager) { | |
| 297 | 210 | if (!accClass.isInterface()) |
| 298 | 211 | throw new IllegalArgumentException("@Accessor annotation is only allowed on interfaces"); |
| 299 | 212 | |
| @@ -332,7 +245,7 @@ | ||
| 332 | 245 | else if (allParamsNamed != thisParamNamed) |
| 333 | 246 | throw new IllegalArgumentException(String.format("For method '%s', either all or none of the parameters must be named", m.getName())); |
| 334 | 247 | |
| 335 | - paramMappers[i] = newParamMapper(accClass.getName(), m.getName(), i, paramName, codecClass, paramTypes[i], paramAnnotations[i], mappingManager); | |
| 248 | + paramMappers[i] = newParamMapper(accClass.getName(), m.getName(), i, paramName, codecClass, paramTypes[i], mappingManager); | |
| 336 | 249 | } |
| 337 | 250 | |
| 338 | 251 | ConsistencyLevel cl = null; |
| @@ -358,7 +271,7 @@ | ||
| 358 | 271 | } |
| 359 | 272 | |
| 360 | 273 | @SuppressWarnings({"unchecked", "rawtypes"}) |
| 361 | - private static ParamMapper newParamMapper(String className, String methodName, int idx, String paramName, Class<? extends TypeCodec<?>> codecClass, Type paramType, Annotation[] paramAnnotations, MappingManager mappingManager) { | |
| 274 | + private static ParamMapper newParamMapper(String className, String methodName, int idx, String paramName, Class<? extends TypeCodec<?>> codecClass, Type paramType, MappingManager mappingManager) { | |
| 362 | 275 | if (paramType instanceof Class) { |
| 363 | 276 | Class<?> paramClass = (Class<?>) paramType; |
| 364 | 277 | if (TypeMappings.isMappedUDT(paramClass)) |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/ColumnMapper.java +12 −27
| @@ -15,39 +15,24 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.mapping; |
| 17 | 17 | |
| 18 | -import com.datastax.driver.core.Metadata; | |
| 19 | 18 | import com.datastax.driver.core.TypeCodec; |
| 20 | 19 | import com.google.common.reflect.TypeToken; |
| 21 | 20 | |
| 22 | -import java.lang.reflect.Field; | |
| 23 | 21 | import java.util.concurrent.atomic.AtomicInteger; |
| 24 | 22 | |
| 25 | 23 | abstract class ColumnMapper<T> { |
| 26 | 24 | |
| 27 | - enum Kind {PARTITION_KEY, CLUSTERING_COLUMN, REGULAR, COMPUTED} | |
| 28 | - | |
| 29 | - private final String columnName; | |
| 30 | - private final String alias; | |
| 31 | - protected final String fieldName; | |
| 32 | - /** | |
| 33 | - * The type of the Java field in the mapped class | |
| 34 | - */ | |
| 35 | - protected final TypeToken<Object> fieldType; | |
| 36 | - protected final Kind kind; | |
| 37 | - protected final int position; | |
| 38 | - protected final TypeCodec<Object> customCodec; | |
| 25 | + final MappedProperty<T> property; | |
| 26 | + final String alias; | |
| 27 | + final int position; | |
| 39 | 28 | |
| 40 | 29 | @SuppressWarnings("unchecked") |
| 41 | - protected ColumnMapper(Field field, int position, AtomicInteger columnCounter) { | |
| 42 | - this.columnName = AnnotationParser.columnName(field); | |
| 30 | + protected ColumnMapper(MappedProperty<T> property, int position, AtomicInteger columnCounter) { | |
| 31 | + this.property = property; | |
| 32 | + this.position = position; | |
| 43 | 33 | this.alias = (columnCounter != null) |
| 44 | - ? AnnotationParser.newAlias(field, columnCounter.incrementAndGet()) | |
| 34 | + ? AnnotationParser.newAlias(columnCounter.incrementAndGet()) | |
| 45 | 35 | : null; |
| 46 | - this.fieldName = field.getName(); | |
| 47 | - this.fieldType = (TypeToken<Object>) TypeToken.of(field.getGenericType()); | |
| 48 | - this.kind = AnnotationParser.kind(field); | |
| 49 | - this.position = position; | |
| 50 | - this.customCodec = AnnotationParser.customCodec(field); | |
| 51 | 36 | } |
| 52 | 37 | |
| 53 | 38 | abstract Object getValue(T entity); |
| @@ -55,9 +40,7 @@ | ||
| 55 | 40 | abstract void setValue(T entity, Object value); |
| 56 | 41 | |
| 57 | 42 | String getColumnName() { |
| 58 | - return kind == Kind.COMPUTED | |
| 59 | - ? columnName | |
| 60 | - : Metadata.quote(columnName); | |
| 43 | + return property.columnName(); | |
| 61 | 44 | } |
| 62 | 45 | |
| 63 | 46 | String getAlias() { |
| @@ -65,13 +48,15 @@ | ||
| 65 | 48 | } |
| 66 | 49 | |
| 67 | 50 | public TypeCodec<Object> getCustomCodec() { |
| 68 | - return customCodec; | |
| 51 | + return property.customCodec(); | |
| 69 | 52 | } |
| 70 | 53 | |
| 71 | 54 | /** |
| 72 | 55 | * The Java type of this column. |
| 73 | 56 | */ |
| 57 | + @SuppressWarnings("unchecked") | |
| 74 | 58 | public TypeToken<Object> getJavaType() { |
| 75 | - return fieldType; | |
| 59 | + return (TypeToken<Object>) property.type(); | |
| 76 | 60 | } |
| 61 | + | |
| 77 | 62 | } |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/EntityMapper.java +1 −2
| @@ -17,7 +17,6 @@ | ||
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.ConsistencyLevel; |
| 19 | 19 | |
| 20 | -import java.lang.reflect.Field; | |
| 21 | 20 | import java.util.ArrayList; |
| 22 | 21 | import java.util.List; |
| 23 | 22 | import java.util.concurrent.atomic.AtomicInteger; |
| @@ -87,6 +86,6 @@ | ||
| 87 | 86 | interface Factory { |
| 88 | 87 | public <T> EntityMapper<T> create(Class<T> entityClass, String keyspace, String table, ConsistencyLevel writeConsistency, ConsistencyLevel readConsistency); |
| 89 | 88 | |
| 90 | - public <T> ColumnMapper<T> createColumnMapper(Class<T> componentClass, Field field, int position, MappingManager mappingManager, AtomicInteger columnCounter); | |
| 89 | + public <T> ColumnMapper<T> createColumnMapper(MappedProperty<T> property, int position, MappingManager mappingManager, AtomicInteger columnCounter); | |
| 91 | 90 | } |
| 92 | 91 | } |
added driver-mapping/src/main/java/com/datastax/driver/mapping/MappedProperty.java +203 −0
| @@ -0,0 +1,203 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.mapping; | |
| 17 | + | |
| 18 | +import com.datastax.driver.core.Metadata; | |
| 19 | +import com.datastax.driver.core.TypeCodec; | |
| 20 | +import com.datastax.driver.mapping.annotations.*; | |
| 21 | +import com.google.common.collect.ComparisonChain; | |
| 22 | +import com.google.common.reflect.TypeToken; | |
| 23 | + | |
| 24 | +import java.beans.PropertyDescriptor; | |
| 25 | +import java.lang.annotation.Annotation; | |
| 26 | +import java.lang.reflect.Field; | |
| 27 | +import java.lang.reflect.Method; | |
| 28 | +import java.lang.reflect.Type; | |
| 29 | +import java.util.Collection; | |
| 30 | +import java.util.Map; | |
| 31 | + | |
| 32 | +/** | |
| 33 | + * A bean property mapped to a table column or a UDT field. | |
| 34 | + * | |
| 35 | + * @param <T> The component classe where this property belongs (either | |
| 36 | + * a {@link com.datastax.driver.mapping.annotations.Table @Table} | |
| 37 | + * or {@link com.datastax.driver.mapping.annotations.UDT @UDT} | |
| 38 | + * annotated class). | |
| 39 | + */ | |
| 40 | +class MappedProperty<T> implements Comparable<MappedProperty<T>> { | |
| 41 | + | |
| 42 | + enum Kind {PARTITION_KEY, CLUSTERING_COLUMN, REGULAR, COMPUTED} | |
| 43 | + | |
| 44 | + private final PropertyDescriptor property; | |
| 45 | + private final Field field; | |
| 46 | + private final Method getter; | |
| 47 | + private final Method setter; | |
| 48 | + private final Map<Class<? extends Annotation>, Annotation> annotations; | |
| 49 | + private final String columnName; | |
| 50 | + private final TypeToken<Object> type; | |
| 51 | + private final TypeCodec<Object> customCodec; | |
| 52 | + | |
| 53 | + MappedProperty(PropertyDescriptor property, Class<T> componentClass) { | |
| 54 | + this.property = property; | |
| 55 | + field = ReflectionUtils.findField(property.getName(), componentClass); | |
| 56 | + getter = ReflectionUtils.findGetter(property); | |
| 57 | + setter = ReflectionUtils.findSetter(property); | |
| 58 | + annotations = ReflectionUtils.findAnnotations(property, componentClass); | |
| 59 | + columnName = createColumnName(); | |
| 60 | + type = inferGenericType(); | |
| 61 | + customCodec = createCustomCodec(); | |
| 62 | + } | |
| 63 | + | |
| 64 | + boolean hasAnnotation(Class<? extends Annotation> annotationClass) { | |
| 65 | + return annotations.containsKey(annotationClass); | |
| 66 | + } | |
| 67 | + | |
| 68 | + Collection<Annotation> annotations() { | |
| 69 | + return annotations.values(); | |
| 70 | + } | |
| 71 | + | |
| 72 | + @SuppressWarnings("unchecked") | |
| 73 | + <A extends Annotation> A annotation(Class<A> annotationClass) { | |
| 74 | + return (A) annotations.get(annotationClass); | |
| 75 | + } | |
| 76 | + | |
| 77 | + boolean isComputed() { | |
| 78 | + return hasAnnotation(Computed.class); | |
| 79 | + } | |
| 80 | + | |
| 81 | + boolean isTransient() { | |
| 82 | + return hasAnnotation(Transient.class); | |
| 83 | + } | |
| 84 | + | |
| 85 | + boolean isPartitionKey() { | |
| 86 | + return hasAnnotation(PartitionKey.class); | |
| 87 | + } | |
| 88 | + | |
| 89 | + boolean isClusteringColumn() { | |
| 90 | + return hasAnnotation(ClusteringColumn.class); | |
| 91 | + } | |
| 92 | + | |
| 93 | + Method getter() { | |
| 94 | + return getter; | |
| 95 | + } | |
| 96 | + | |
| 97 | + Method setter() { | |
| 98 | + return setter; | |
| 99 | + } | |
| 100 | + | |
| 101 | + String name() { | |
| 102 | + return property.getName(); | |
| 103 | + } | |
| 104 | + | |
| 105 | + TypeToken<?> type() { | |
| 106 | + return type; | |
| 107 | + } | |
| 108 | + | |
| 109 | + String columnName() { | |
| 110 | + return columnName; | |
| 111 | + } | |
| 112 | + | |
| 113 | + TypeCodec<Object> customCodec() { | |
| 114 | + return customCodec; | |
| 115 | + } | |
| 116 | + | |
| 117 | + int position() { | |
| 118 | + if (isPartitionKey()) { | |
| 119 | + return annotation(PartitionKey.class).value(); | |
| 120 | + } | |
| 121 | + if (isClusteringColumn()) { | |
| 122 | + return annotation(ClusteringColumn.class).value(); | |
| 123 | + } | |
| 124 | + return -1; | |
| 125 | + } | |
| 126 | + | |
| 127 | + Kind kind() { | |
| 128 | + if (isPartitionKey()) { | |
| 129 | + return Kind.PARTITION_KEY; | |
| 130 | + } | |
| 131 | + if (isClusteringColumn()) { | |
| 132 | + return Kind.CLUSTERING_COLUMN; | |
| 133 | + } | |
| 134 | + if (isComputed()) { | |
| 135 | + return Kind.COMPUTED; | |
| 136 | + } | |
| 137 | + return Kind.REGULAR; | |
| 138 | + } | |
| 139 | + | |
| 140 | + private String createColumnName() { | |
| 141 | + Column column = annotation(Column.class); | |
| 142 | + if (column != null && !column.name().isEmpty()) { | |
| 143 | + return Metadata.quote(column.caseSensitive() ? column.name() : column.name().toLowerCase()); | |
| 144 | + } | |
| 145 | + com.datastax.driver.mapping.annotations.Field udtField = annotation(com.datastax.driver.mapping.annotations.Field.class); | |
| 146 | + if (udtField != null && !udtField.name().isEmpty()) { | |
| 147 | + return Metadata.quote(udtField.caseSensitive() ? udtField.name() : udtField.name().toLowerCase()); | |
| 148 | + } | |
| 149 | + if (isComputed()) { | |
| 150 | + return annotation(Computed.class).value(); | |
| 151 | + } | |
| 152 | + return Metadata.quote(name().toLowerCase()); | |
| 153 | + } | |
| 154 | + | |
| 155 | + @SuppressWarnings("unchecked") | |
| 156 | + private TypeToken<Object> inferGenericType() { | |
| 157 | + Type type; | |
| 158 | + if (field != null) | |
| 159 | + type = field.getGenericType(); | |
| 160 | + else if (getter != null) | |
| 161 | + type = getter.getGenericReturnType(); | |
| 162 | + else | |
| 163 | + // this will not work for generic types | |
| 164 | + type = property.getPropertyType(); | |
| 165 | + return (TypeToken<Object>) TypeToken.of(type); | |
| 166 | + } | |
| 167 | + | |
| 168 | + private TypeCodec<Object> createCustomCodec() { | |
| 169 | + Class<? extends TypeCodec<?>> codecClass = getCustomCodecClass(); | |
| 170 | + if (codecClass.equals(Defaults.NoCodec.class)) | |
| 171 | + return null; | |
| 172 | + try { | |
| 173 | + @SuppressWarnings("unchecked") | |
| 174 | + TypeCodec<Object> instance = (TypeCodec<Object>) codecClass.newInstance(); | |
| 175 | + return instance; | |
| 176 | + } catch (Exception e) { | |
| 177 | + throw new IllegalArgumentException(String.format( | |
| 178 | + "Cannot create an instance of custom codec %s for property %s", | |
| 179 | + codecClass, property | |
| 180 | + ), e); | |
| 181 | + } | |
| 182 | + } | |
| 183 | + | |
| 184 | + private Class<? extends TypeCodec<?>> getCustomCodecClass() { | |
| 185 | + Column column = annotation(Column.class); | |
| 186 | + if (column != null) | |
| 187 | + return column.codec(); | |
| 188 | + com.datastax.driver.mapping.annotations.Field udtField = annotation(com.datastax.driver.mapping.annotations.Field.class); | |
| 189 | + if (udtField != null) | |
| 190 | + return udtField.codec(); | |
| 191 | + return Defaults.NoCodec.class; | |
| 192 | + } | |
| 193 | + | |
| 194 | + @Override | |
| 195 | + public int compareTo(MappedProperty<T> that) { | |
| 196 | + return ComparisonChain.start() | |
| 197 | + .compare(this.kind(), that.kind()) | |
| 198 | + .compare(this.position(), that.position()) | |
| 199 | + .compare(this.name(), that.name()) | |
| 200 | + .result(); | |
| 201 | + } | |
| 202 | + | |
| 203 | +} |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/Mapper.java +1 −1
| @@ -218,7 +218,7 @@ | ||
| 218 | 218 | |
| 219 | 219 | for (ColumnMapper<T> cm : mapper.allColumns()) { |
| 220 | 220 | Object value = cm.getValue(entity); |
| 221 | - if (cm.kind != ColumnMapper.Kind.COMPUTED && (saveNullFields || value != null)) { | |
| 221 | + if (!cm.property.isComputed() && (saveNullFields || value != null)) { | |
| 222 | 222 | values.put(cm, value); |
| 223 | 223 | } |
| 224 | 224 | } |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/QueryType.java +2 −2
| @@ -63,7 +63,7 @@ | ||
| 63 | 63 | ? insertInto(mapper.getKeyspace(), mapper.getTable()) |
| 64 | 64 | : insertInto(table); |
| 65 | 65 | for (ColumnMapper<?> cm : columns) |
| 66 | - if (cm.kind != ColumnMapper.Kind.COMPUTED) | |
| 66 | + if (!cm.property.isComputed()) | |
| 67 | 67 | insert.value(cm.getColumnName(), bindMarker()); |
| 68 | 68 | |
| 69 | 69 | Insert.Options usings = insert.using(); |
| @@ -77,7 +77,7 @@ | ||
| 77 | 77 | case GET: { |
| 78 | 78 | Select.Selection selection = select(); |
| 79 | 79 | for (ColumnMapper cm : mapper.allColumns()) { |
| 80 | - Select.SelectionOrAlias column = (cm.kind == ColumnMapper.Kind.COMPUTED) | |
| 80 | + Select.SelectionOrAlias column = cm.property.isComputed() | |
| 81 | 81 | ? selection.raw(cm.getColumnName()) |
| 82 | 82 | : selection.column(cm.getColumnName()); |
| 83 | 83 |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/ReflectionMapper.java +13 −32
| @@ -17,10 +17,6 @@ | ||
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.ConsistencyLevel; |
| 19 | 19 | |
| 20 | -import java.beans.IntrospectionException; | |
| 21 | -import java.beans.PropertyDescriptor; | |
| 22 | -import java.lang.reflect.Field; | |
| 23 | -import java.lang.reflect.Method; | |
| 24 | 20 | import java.util.concurrent.atomic.AtomicInteger; |
| 25 | 21 | |
| 26 | 22 | /** |
| @@ -29,7 +25,7 @@ | ||
| 29 | 25 | */ |
| 30 | 26 | class ReflectionMapper<T> extends EntityMapper<T> { |
| 31 | 27 | |
| 32 | - private static ReflectionFactory factory = new ReflectionFactory(); | |
| 28 | + private static final ReflectionFactory factory = new ReflectionFactory(); | |
| 33 | 29 | |
| 34 | 30 | private ReflectionMapper(Class<T> entityClass, String keyspace, String table, ConsistencyLevel writeConsistency, ConsistencyLevel readConsistency) { |
| 35 | 31 | super(entityClass, keyspace, table, writeConsistency, readConsistency); |
| @@ -50,34 +46,29 @@ | ||
| 50 | 46 | |
| 51 | 47 | private static class LiteralMapper<T> extends ColumnMapper<T> { |
| 52 | 48 | |
| 53 | - private final Method readMethod; | |
| 54 | - private final Method writeMethod; | |
| 55 | - | |
| 56 | - private LiteralMapper(Field field, int position, PropertyDescriptor pd, AtomicInteger columnCounter) { | |
| 57 | - super(field, position, columnCounter); | |
| 58 | - this.readMethod = pd.getReadMethod(); | |
| 59 | - this.writeMethod = pd.getWriteMethod(); | |
| 49 | + private LiteralMapper(MappedProperty<T> property, int position, AtomicInteger columnCounter) { | |
| 50 | + super(property, position, columnCounter); | |
| 60 | 51 | } |
| 61 | 52 | |
| 62 | 53 | @Override |
| 63 | 54 | Object getValue(T entity) { |
| 64 | 55 | try { |
| 65 | - return readMethod.invoke(entity); | |
| 56 | + return property.getter().invoke(entity); | |
| 66 | 57 | } catch (IllegalArgumentException e) { |
| 67 | - throw new IllegalArgumentException("Could not get field '" + fieldName + "'"); | |
| 58 | + throw new IllegalArgumentException("Could not get field '" + property.name() + "'"); | |
| 68 | 59 | } catch (Exception e) { |
| 69 | - throw new IllegalStateException("Unable to access getter for '" + fieldName + "' in " + entity.getClass().getName(), e); | |
| 60 | + throw new IllegalStateException("Unable to access getter for '" + property.name() + "' in " + entity.getClass().getName(), e); | |
| 70 | 61 | } |
| 71 | 62 | } |
| 72 | 63 | |
| 73 | 64 | @Override |
| 74 | 65 | void setValue(Object entity, Object value) { |
| 75 | 66 | try { |
| 76 | - writeMethod.invoke(entity, value); | |
| 67 | + property.setter().invoke(entity, value); | |
| 77 | 68 | } catch (IllegalArgumentException e) { |
| 78 | - throw new IllegalArgumentException("Could not set field '" + fieldName + "' to value '" + value + "'"); | |
| 69 | + throw new IllegalArgumentException("Could not set field '" + property.name() + "' to value '" + value + "'"); | |
| 79 | 70 | } catch (Exception e) { |
| 80 | - throw new IllegalStateException("Unable to access setter for '" + fieldName + "' in " + entity.getClass().getName(), e); | |
| 71 | + throw new IllegalStateException("Unable to access setter for '" + property.name() + "' in " + entity.getClass().getName(), e); | |
| 81 | 72 | } |
| 82 | 73 | } |
| 83 | 74 | } |
| @@ -89,21 +80,11 @@ | ||
| 89 | 80 | return new ReflectionMapper<T>(entityClass, keyspace, table, writeConsistency, readConsistency); |
| 90 | 81 | } |
| 91 | 82 | |
| 92 | - @SuppressWarnings({"unchecked", "rawtypes"}) | |
| 93 | 83 | @Override |
| 94 | - public <T> ColumnMapper<T> createColumnMapper(Class<T> entityClass, Field field, int position, MappingManager mappingManager, AtomicInteger columnCounter) { | |
| 95 | - String fieldName = field.getName(); | |
| 96 | - try { | |
| 97 | - PropertyDescriptor pd = new PropertyDescriptor(fieldName, field.getDeclaringClass()); | |
| 98 | - | |
| 99 | - for (Class<?> udt : TypeMappings.findUDTs(field.getGenericType())) | |
| 100 | - mappingManager.getUDTCodec(udt); | |
| 101 | - | |
| 102 | - return new LiteralMapper<T>(field, position, pd, columnCounter); | |
| 103 | - | |
| 104 | - } catch (IntrospectionException e) { | |
| 105 | - throw new IllegalArgumentException("Cannot find matching getter and setter for field '" + fieldName + "'"); | |
| 106 | - } | |
| 84 | + public <T> ColumnMapper<T> createColumnMapper(MappedProperty<T> property, int position, MappingManager mappingManager, AtomicInteger columnCounter) { | |
| 85 | + for (Class<?> udt : TypeMappings.findUDTs(property.type().getType())) | |
| 86 | + mappingManager.getUDTCodec(udt); | |
| 87 | + return new LiteralMapper<T>(property, position, columnCounter); | |
| 107 | 88 | } |
| 108 | 89 | } |
| 109 | 90 | } |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/ReflectionUtils.java +71 −15
| @@ -15,26 +15,82 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.mapping; |
| 17 | 17 | |
| 18 | -import java.lang.reflect.ParameterizedType; | |
| 19 | -import java.lang.reflect.Type; | |
| 18 | +import com.google.common.base.Throwables; | |
| 19 | + | |
| 20 | +import java.beans.BeanInfo; | |
| 21 | +import java.beans.IntrospectionException; | |
| 22 | +import java.beans.Introspector; | |
| 23 | +import java.beans.PropertyDescriptor; | |
| 24 | +import java.lang.annotation.Annotation; | |
| 25 | +import java.lang.reflect.Field; | |
| 26 | +import java.lang.reflect.Method; | |
| 27 | +import java.lang.reflect.Modifier; | |
| 28 | +import java.util.HashMap; | |
| 29 | +import java.util.Map; | |
| 20 | 30 | |
| 21 | 31 | /** |
| 22 | 32 | * Utility methods related to reflection. |
| 23 | 33 | */ |
| 24 | 34 | class ReflectionUtils { |
| 25 | 35 | |
| 26 | - /** | |
| 27 | - * Gets a type argument from a parameterized type. | |
| 28 | - * | |
| 29 | - * @param pt the parameterized type. | |
| 30 | - * @param arg the index of the argument to retrieve. | |
| 31 | - * @param name the name of the element (field or method argument). | |
| 32 | - * @return the type argument. | |
| 33 | - */ | |
| 34 | - static Class<?> getParam(ParameterizedType pt, int arg, String name) { | |
| 35 | - Type ft = pt.getActualTypeArguments()[arg]; | |
| 36 | - if (!(ft instanceof Class)) | |
| 37 | - throw new IllegalArgumentException(String.format("Cannot map parameter of class %s for %s", pt, name)); | |
| 38 | - return (Class<?>) ft; | |
| 36 | + static <T> BeanInfo getBeanInfo(Class<T> udtClass) { | |
| 37 | + BeanInfo beanInfo; | |
| 38 | + try { | |
| 39 | + beanInfo = Introspector.getBeanInfo(udtClass); | |
| 40 | + } catch (IntrospectionException e) { | |
| 41 | + throw Throwables.propagate(e); | |
| 42 | + } | |
| 43 | + return beanInfo; | |
| 44 | + } | |
| 45 | + | |
| 46 | + static Map<Class<? extends Annotation>, Annotation> findAnnotations(PropertyDescriptor property, Class<?> baseClass) { | |
| 47 | + // try field | |
| 48 | + Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>(); | |
| 49 | + Field field = findField(property.getName(), baseClass); | |
| 50 | + if (field != null) { | |
| 51 | + for (Annotation annotation : field.getAnnotations()) { | |
| 52 | + annotations.put(annotation.annotationType(), annotation); | |
| 53 | + } | |
| 54 | + } | |
| 55 | + // try getter | |
| 56 | + Method getter = findGetter(property); | |
| 57 | + if (getter != null) { | |
| 58 | + for (Annotation annotation : getter.getAnnotations()) { | |
| 59 | + annotations.put(annotation.annotationType(), annotation); | |
| 60 | + } | |
| 61 | + } | |
| 62 | + return annotations; | |
| 63 | + } | |
| 64 | + | |
| 65 | + static Method findGetter(PropertyDescriptor property) { | |
| 66 | + Method getter = property.getReadMethod(); | |
| 67 | + if (getter == null) | |
| 68 | + return null; | |
| 69 | + getter.setAccessible(true); | |
| 70 | + return getter; | |
| 39 | 71 | } |
| 72 | + | |
| 73 | + static Method findSetter(PropertyDescriptor property) { | |
| 74 | + Method setter = property.getWriteMethod(); | |
| 75 | + if (setter == null) | |
| 76 | + return null; | |
| 77 | + setter.setAccessible(true); | |
| 78 | + return setter; | |
| 79 | + } | |
| 80 | + | |
| 81 | + static Field findField(String name, Class<?> baseClass) { | |
| 82 | + for (Class<?> clazz = baseClass; !clazz.equals(Object.class); clazz = clazz.getSuperclass()) { | |
| 83 | + try { | |
| 84 | + Field field = clazz.getDeclaredField(name); | |
| 85 | + if (field.isSynthetic() || (field.getModifiers() & Modifier.STATIC) == Modifier.STATIC) | |
| 86 | + continue; | |
| 87 | + field.setAccessible(true); | |
| 88 | + return field; | |
| 89 | + } catch (NoSuchFieldException e) { | |
| 90 | + //ok | |
| 91 | + } | |
| 92 | + } | |
| 93 | + return null; | |
| 94 | + } | |
| 95 | + | |
| 40 | 96 | } |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperComputedFieldsTest.java +1 −1
| @@ -91,7 +91,7 @@ | ||
| 91 | 91 | void should_add_aliases_for_fields_in_select_queries() { |
| 92 | 92 | BoundStatement bs = (BoundStatement) userMapper.getQuery("test"); |
| 93 | 93 | assertThat(bs.preparedStatement().getQueryString()) |
| 94 | - .contains("SELECT", "\"login\" AS col1", "\"name\" AS col2", "writetime(\"name\") AS col3"); | |
| 94 | + .contains("SELECT", "\"login\" AS col", "\"name\" AS col", "writetime(\"name\") AS col"); | |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | @Test(groups = "short") |
added driver-mapping/src/test/java/com/datastax/driver/mapping/MapperPolymorphismTest.java +327 −0
| @@ -0,0 +1,327 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.mapping; | |
| 17 | + | |
| 18 | +import com.datastax.driver.core.CCMTestsSupport; | |
| 19 | +import com.datastax.driver.mapping.annotations.Column; | |
| 20 | +import com.datastax.driver.mapping.annotations.PartitionKey; | |
| 21 | +import com.datastax.driver.mapping.annotations.Table; | |
| 22 | +import com.datastax.driver.mapping.annotations.UDT; | |
| 23 | +import com.google.common.base.Objects; | |
| 24 | +import org.testng.annotations.BeforeMethod; | |
| 25 | +import org.testng.annotations.Test; | |
| 26 | + | |
| 27 | +import java.util.UUID; | |
| 28 | + | |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 30 | + | |
| 31 | +@SuppressWarnings("unused") | |
| 32 | +public class MapperPolymorphismTest extends CCMTestsSupport { | |
| 33 | + | |
| 34 | + private final Circle circle = new Circle(new Point2D(11, 22), 12.34); | |
| 35 | + private final Rectangle rectangle = new Rectangle(new Point2D(20, 30), new Point2D(50, 60)); | |
| 36 | + private final Sphere sphere = new Sphere(new Point3D(11, 22, 33), 34.56); | |
| 37 | + private Mapper<Circle> circleMapper; | |
| 38 | + private Mapper<Rectangle> rectangleMapper; | |
| 39 | + private Mapper<Sphere> sphereMapper; | |
| 40 | + | |
| 41 | + @Override | |
| 42 | + public void onTestContextInitialized() { | |
| 43 | + execute( | |
| 44 | + "CREATE TYPE point2d (x int, y int)", | |
| 45 | + "CREATE TYPE point3d (x int, y int, z int)", | |
| 46 | + "CREATE TABLE circles (id uuid PRIMARY KEY, center frozen<point2d>, radius double)", | |
| 47 | + "CREATE TABLE rectangles (id uuid PRIMARY KEY, bottom_left frozen<point2d>, top_right frozen<point2d>)", | |
| 48 | + "CREATE TABLE spheres (id uuid PRIMARY KEY, center frozen<point3d>, radius double)"); | |
| 49 | + } | |
| 50 | + | |
| 51 | + @BeforeMethod(groups = "short") | |
| 52 | + public void clean() { | |
| 53 | + execute("TRUNCATE circles", "TRUNCATE rectangles", "TRUNCATE spheres"); | |
| 54 | + } | |
| 55 | + | |
| 56 | + @UDT(name = "point2d") | |
| 57 | + static class Point2D { | |
| 58 | + | |
| 59 | + private int x; | |
| 60 | + | |
| 61 | + private int y; | |
| 62 | + | |
| 63 | + public Point2D() { | |
| 64 | + } | |
| 65 | + | |
| 66 | + public Point2D(int x, int y) { | |
| 67 | + this.x = x; | |
| 68 | + this.y = y; | |
| 69 | + } | |
| 70 | + | |
| 71 | + public int getX() { | |
| 72 | + return x; | |
| 73 | + } | |
| 74 | + | |
| 75 | + public void setX(int x) { | |
| 76 | + this.x = x; | |
| 77 | + } | |
| 78 | + | |
| 79 | + public int getY() { | |
| 80 | + return y; | |
| 81 | + } | |
| 82 | + | |
| 83 | + public void setY(int y) { | |
| 84 | + this.y = y; | |
| 85 | + } | |
| 86 | + | |
| 87 | + @Override | |
| 88 | + public boolean equals(Object o) { | |
| 89 | + if (this == o) return true; | |
| 90 | + if (o == null || getClass() != o.getClass()) return false; | |
| 91 | + Point2D point2D = (Point2D) o; | |
| 92 | + return getX() == point2D.getX() && | |
| 93 | + getY() == point2D.getY(); | |
| 94 | + } | |
| 95 | + | |
| 96 | + @Override | |
| 97 | + public int hashCode() { | |
| 98 | + return Objects.hashCode(getX(), getY()); | |
| 99 | + } | |
| 100 | + } | |
| 101 | + | |
| 102 | + @UDT(name = "point3d") | |
| 103 | + static class Point3D extends Point2D { | |
| 104 | + | |
| 105 | + private int z; | |
| 106 | + | |
| 107 | + public Point3D() { | |
| 108 | + } | |
| 109 | + | |
| 110 | + public Point3D(int x, int y, int z) { | |
| 111 | + super(x, y); | |
| 112 | + this.z = z; | |
| 113 | + } | |
| 114 | + | |
| 115 | + public int getZ() { | |
| 116 | + return z; | |
| 117 | + } | |
| 118 | + | |
| 119 | + public void setZ(int z) { | |
| 120 | + this.z = z; | |
| 121 | + } | |
| 122 | + | |
| 123 | + @Override | |
| 124 | + public boolean equals(Object o) { | |
| 125 | + if (this == o) return true; | |
| 126 | + if (o == null || getClass() != o.getClass()) return false; | |
| 127 | + if (!super.equals(o)) return false; | |
| 128 | + Point3D point3D = (Point3D) o; | |
| 129 | + return getZ() == point3D.getZ(); | |
| 130 | + } | |
| 131 | + | |
| 132 | + @Override | |
| 133 | + public int hashCode() { | |
| 134 | + return Objects.hashCode(super.hashCode(), getZ()); | |
| 135 | + } | |
| 136 | + } | |
| 137 | + | |
| 138 | + static class Shape { | |
| 139 | + | |
| 140 | + @PartitionKey | |
| 141 | + private UUID id; | |
| 142 | + | |
| 143 | + public Shape() { | |
| 144 | + this.id = UUID.randomUUID(); | |
| 145 | + } | |
| 146 | + | |
| 147 | + public UUID getId() { | |
| 148 | + return id; | |
| 149 | + } | |
| 150 | + | |
| 151 | + public void setId(UUID id) { | |
| 152 | + this.id = id; | |
| 153 | + } | |
| 154 | + | |
| 155 | + @Override | |
| 156 | + public boolean equals(Object o) { | |
| 157 | + if (this == o) return true; | |
| 158 | + if (o == null || getClass() != o.getClass()) return false; | |
| 159 | + Shape shape = (Shape) o; | |
| 160 | + return Objects.equal(getId(), shape.getId()); | |
| 161 | + } | |
| 162 | + | |
| 163 | + @Override | |
| 164 | + public int hashCode() { | |
| 165 | + return Objects.hashCode(getId()); | |
| 166 | + } | |
| 167 | + } | |
| 168 | + | |
| 169 | + @Table(name = "circles") | |
| 170 | + static class Circle extends Shape { | |
| 171 | + | |
| 172 | + private Point2D center; | |
| 173 | + | |
| 174 | + private double radius; | |
| 175 | + | |
| 176 | + public Circle() { | |
| 177 | + } | |
| 178 | + | |
| 179 | + public Circle(Point2D center, double radius) { | |
| 180 | + this.center = center; | |
| 181 | + this.radius = radius; | |
| 182 | + } | |
| 183 | + | |
| 184 | + public Point2D getCenter() { | |
| 185 | + return center; | |
| 186 | + } | |
| 187 | + | |
| 188 | + public void setCenter(Point2D center) { | |
| 189 | + this.center = center; | |
| 190 | + } | |
| 191 | + | |
| 192 | + public double getRadius() { | |
| 193 | + return radius; | |
| 194 | + } | |
| 195 | + | |
| 196 | + public void setRadius(double radius) { | |
| 197 | + this.radius = radius; | |
| 198 | + } | |
| 199 | + | |
| 200 | + @Override | |
| 201 | + public boolean equals(Object o) { | |
| 202 | + if (this == o) return true; | |
| 203 | + if (o == null || getClass() != o.getClass()) return false; | |
| 204 | + if (!super.equals(o)) return false; | |
| 205 | + Circle circle = (Circle) o; | |
| 206 | + return Double.compare(circle.getRadius(), getRadius()) == 0 && | |
| 207 | + Objects.equal(getCenter(), circle.getCenter()); | |
| 208 | + } | |
| 209 | + | |
| 210 | + @Override | |
| 211 | + public int hashCode() { | |
| 212 | + return Objects.hashCode(super.hashCode(), getCenter(), getRadius()); | |
| 213 | + } | |
| 214 | + } | |
| 215 | + | |
| 216 | + @Table(name = "rectangles") | |
| 217 | + static class Rectangle extends Shape { | |
| 218 | + | |
| 219 | + @Column(name = "bottom_left") | |
| 220 | + private Point2D bottomLeft; | |
| 221 | + | |
| 222 | + @Column(name = "top_right") | |
| 223 | + private Point2D topRight; | |
| 224 | + | |
| 225 | + public Rectangle() { | |
| 226 | + } | |
| 227 | + | |
| 228 | + public Rectangle(Point2D bottomLeft, Point2D topRight) { | |
| 229 | + this.bottomLeft = bottomLeft; | |
| 230 | + this.topRight = topRight; | |
| 231 | + } | |
| 232 | + | |
| 233 | + public Point2D getBottomLeft() { | |
| 234 | + return bottomLeft; | |
| 235 | + } | |
| 236 | + | |
| 237 | + public void setBottomLeft(Point2D bottomLeft) { | |
| 238 | + this.bottomLeft = bottomLeft; | |
| 239 | + } | |
| 240 | + | |
| 241 | + public Point2D getTopRight() { | |
| 242 | + return topRight; | |
| 243 | + } | |
| 244 | + | |
| 245 | + public void setTopRight(Point2D topRight) { | |
| 246 | + this.topRight = topRight; | |
| 247 | + } | |
| 248 | + | |
| 249 | + @Override | |
| 250 | + public boolean equals(Object o) { | |
| 251 | + if (this == o) return true; | |
| 252 | + if (o == null || getClass() != o.getClass()) return false; | |
| 253 | + if (!super.equals(o)) return false; | |
| 254 | + Rectangle rectangle = (Rectangle) o; | |
| 255 | + return Objects.equal(getBottomLeft(), rectangle.getBottomLeft()) && | |
| 256 | + Objects.equal(getTopRight(), rectangle.getTopRight()); | |
| 257 | + } | |
| 258 | + | |
| 259 | + @Override | |
| 260 | + public int hashCode() { | |
| 261 | + return Objects.hashCode(super.hashCode(), getBottomLeft(), getTopRight()); | |
| 262 | + } | |
| 263 | + } | |
| 264 | + | |
| 265 | + @Table(name = "spheres") | |
| 266 | + static class Sphere extends Circle { | |
| 267 | + | |
| 268 | + private Point3D center; | |
| 269 | + | |
| 270 | + public Sphere() { | |
| 271 | + } | |
| 272 | + | |
| 273 | + public Sphere(Point3D center, double radius) { | |
| 274 | + this.center = center; | |
| 275 | + } | |
| 276 | + | |
| 277 | + @Override | |
| 278 | + public Point3D getCenter() { | |
| 279 | + return center; | |
| 280 | + } | |
| 281 | + | |
| 282 | + public void setCenter(Point3D center) { | |
| 283 | + this.center = center; | |
| 284 | + } | |
| 285 | + | |
| 286 | + @Override | |
| 287 | + public boolean equals(Object o) { | |
| 288 | + if (this == o) return true; | |
| 289 | + if (o == null || getClass() != o.getClass()) return false; | |
| 290 | + if (!super.equals(o)) return false; | |
| 291 | + Sphere sphere = (Sphere) o; | |
| 292 | + return Objects.equal(getCenter(), sphere.getCenter()); | |
| 293 | + } | |
| 294 | + | |
| 295 | + @Override | |
| 296 | + public int hashCode() { | |
| 297 | + return Objects.hashCode(super.hashCode(), getCenter()); | |
| 298 | + } | |
| 299 | + } | |
| 300 | + | |
| 301 | + @BeforeMethod(groups = "short") | |
| 302 | + public void createMappers() throws Exception { | |
| 303 | + MappingManager mappingManager = new MappingManager(session()); | |
| 304 | + circleMapper = mappingManager.mapper(Circle.class); | |
| 305 | + rectangleMapper = mappingManager.mapper(Rectangle.class); | |
| 306 | + sphereMapper = mappingManager.mapper(Sphere.class); | |
| 307 | + } | |
| 308 | + | |
| 309 | + @Test(groups = "short") | |
| 310 | + public void should_save_and_retrieve_circle() throws Exception { | |
| 311 | + circleMapper.save(circle); | |
| 312 | + assertThat(circleMapper.get(circle.getId())).isEqualTo(circle); | |
| 313 | + } | |
| 314 | + | |
| 315 | + @Test(groups = "short") | |
| 316 | + public void should_save_and_retrieve_rectangle() throws Exception { | |
| 317 | + rectangleMapper.save(rectangle); | |
| 318 | + assertThat(rectangleMapper.get(rectangle.getId())).isEqualTo(rectangle); | |
| 319 | + } | |
| 320 | + | |
| 321 | + @Test(groups = "short") | |
| 322 | + public void should_save_and_retrieve_sphere() throws Exception { | |
| 323 | + sphereMapper.save(sphere); | |
| 324 | + assertThat(sphereMapper.get(sphere.getId())).isEqualTo(sphere); | |
| 325 | + } | |
| 326 | + | |
| 327 | +} |