kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | e18aa051474109aded44db17839fd8e01b936fe1d6705d0b2f518e2927830c05 |
|---|---|
| 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 | 270ab72db05a |
5 files changed
modified driver-mapping/src/main/java/com/datastax/driver/mapping/AnnotationChecks.java +1 −1
| @@ -105,7 +105,7 @@ | ||
| 105 | 105 | static void validateOrder(List<PropertyMapper> properties, String annotation) { |
| 106 | 106 | for (int i = 0; i < properties.size(); i++) { |
| 107 | 107 | PropertyMapper property = properties.get(i); |
| 108 | - int pos = property.getPosition(); | |
| 108 | + int pos = property.position; | |
| 109 | 109 | if (pos != i) |
| 110 | 110 | throw new IllegalArgumentException(String.format("Invalid ordering value %d for annotation %s of property '%s', was expecting %d", |
| 111 | 111 | pos, annotation, property, i)); |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/AnnotationParser.java +3 −8
| @@ -42,7 +42,7 @@ | ||
| 42 | 42 | * - {@link #parseUDT(Class, MappingManager)}: UDT classes into {@link MappedUDTCodec} instances. |
| 43 | 43 | * - {@link #parseAccessor(Class, MappingManager)}: Accessor interfaces into {@link AccessorMapper} instances. |
| 44 | 44 | */ |
| 45 | -@SuppressWarnings("unchecked") | |
| 45 | +@SuppressWarnings({"unchecked", "WeakerAccess"}) | |
| 46 | 46 | class AnnotationParser { |
| 47 | 47 | |
| 48 | 48 | @VisibleForTesting |
| @@ -67,7 +67,7 @@ | ||
| 67 | 67 | private static final Comparator<PropertyMapper> POSITION_COMPARATOR = new Comparator<PropertyMapper>() { |
| 68 | 68 | @Override |
| 69 | 69 | public int compare(PropertyMapper o1, PropertyMapper o2) { |
| 70 | - return o1.getPosition() - o2.getPosition(); | |
| 70 | + return o1.position - o2.position; | |
| 71 | 71 | } |
| 72 | 72 | }; |
| 73 | 73 | |
| @@ -112,7 +112,7 @@ | ||
| 112 | 112 | java.lang.reflect.Field field = (java.lang.reflect.Field) entry.getValue()[0]; |
| 113 | 113 | PropertyDescriptor property = (PropertyDescriptor) entry.getValue()[1]; |
| 114 | 114 | String alias = (columnCounter != null) |
| 115 | - ? newAlias(columnCounter.incrementAndGet()) | |
| 115 | + ? "col" + columnCounter.incrementAndGet() | |
| 116 | 116 | : null; |
| 117 | 117 | |
| 118 | 118 | PropertyMapper propertyMapper = new PropertyMapper(propertyName, alias, field, property); |
| @@ -262,11 +262,6 @@ | ||
| 262 | 262 | return new AccessorMapper<T>(accClass, methods); |
| 263 | 263 | } |
| 264 | 264 | |
| 265 | - private static String newAlias(int columnNumber) { | |
| 266 | - return "col" + columnNumber; | |
| 267 | - } | |
| 268 | - | |
| 269 | - @SuppressWarnings({"unchecked", "rawtypes"}) | |
| 270 | 265 | private static ParamMapper newParamMapper(String className, String methodName, int idx, String paramName, Class<? extends TypeCodec<?>> codecClass, Type paramType, MappingManager mappingManager) { |
| 271 | 266 | if (paramType instanceof Class) { |
| 272 | 267 | Class<?> paramClass = (Class<?>) paramType; |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/PropertyMapper.java +1 −5
| @@ -44,11 +44,11 @@ | ||
| 44 | 44 | final String columnName; |
| 45 | 45 | final TypeToken<Object> javaType; |
| 46 | 46 | final TypeCodec<Object> customCodec; |
| 47 | + final int position; | |
| 47 | 48 | |
| 48 | 49 | private final Field field; |
| 49 | 50 | private final Method getter; |
| 50 | 51 | private final Method setter; |
| 51 | - private final int position; | |
| 52 | 52 | private final Map<Class<? extends Annotation>, Annotation> annotations; |
| 53 | 53 | |
| 54 | 54 | PropertyMapper(String propertyName, String alias, Field field, PropertyDescriptor property) { |
| @@ -121,10 +121,6 @@ | ||
| 121 | 121 | return hasAnnotation(ClusteringColumn.class); |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | - int getPosition() { | |
| 125 | - return position; | |
| 126 | - } | |
| 127 | - | |
| 128 | 124 | private String inferColumnName() { |
| 129 | 125 | Column column = annotation(Column.class); |
| 130 | 126 | if (column != null && !column.name().isEmpty()) { |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/ReflectionUtils.java +34 −29
| @@ -35,20 +35,29 @@ | ||
| 35 | 35 | class ReflectionUtils { |
| 36 | 36 | |
| 37 | 37 | static <T> T newInstance(Class<T> clazz) { |
| 38 | + Constructor<T> publicConstructor; | |
| 38 | 39 | try { |
| 39 | - return clazz.newInstance(); | |
| 40 | - } catch (Exception e) { | |
| 40 | + publicConstructor = clazz.getConstructor(); | |
| 41 | + } catch (NoSuchMethodException e) { | |
| 41 | 42 | try { |
| 42 | 43 | // try private constructor |
| 43 | - Constructor<T> constructor = clazz.getDeclaredConstructor(); | |
| 44 | - constructor.setAccessible(true); | |
| 45 | - return constructor.newInstance(); | |
| 44 | + Constructor<T> privateConstructor = clazz.getDeclaredConstructor(); | |
| 45 | + privateConstructor.setAccessible(true); | |
| 46 | + return privateConstructor.newInstance(); | |
| 46 | 47 | } catch (Exception e1) { |
| 47 | - throw new IllegalArgumentException("Can't create an instance of " + clazz.getName()); | |
| 48 | + throw new IllegalArgumentException("Can't create an instance of " + clazz, e); | |
| 48 | 49 | } |
| 49 | 50 | } |
| 51 | + try { | |
| 52 | + return publicConstructor.newInstance(); | |
| 53 | + } catch (Exception e) { | |
| 54 | + throw new IllegalStateException("Can't create an instance of " + clazz, e); | |
| 55 | + } | |
| 50 | 56 | } |
| 51 | 57 | |
| 58 | + // for each key representing a property name, | |
| 59 | + // value[0] contains a Field object, value[1] contains a PropertyDescriptor object; | |
| 60 | + // they cannot be both null at the same time | |
| 52 | 61 | static <T> Map<String, Object[]> scanFieldsAndProperties(Class<T> baseClass) { |
| 53 | 62 | Map<String, Object[]> fieldsAndProperties = new HashMap<String, Object[]>(); |
| 54 | 63 | Map<String, Field> fields = scanFields(baseClass); |
| @@ -99,39 +108,35 @@ | ||
| 99 | 108 | static Map<Class<? extends Annotation>, Annotation> scanPropertyAnnotations(Field field, PropertyDescriptor property) { |
| 100 | 109 | Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>(); |
| 101 | 110 | // annotations on getters should have precedence over annotations on fields |
| 102 | - scanFieldAnnotations(field, annotations); | |
| 103 | - if (property != null) { | |
| 104 | - Method getter = property.getReadMethod(); | |
| 111 | + if (field != null) | |
| 112 | + scanFieldAnnotations(field, annotations); | |
| 113 | + Method getter = findGetter(property); | |
| 114 | + if (getter != null) | |
| 105 | 115 | scanMethodAnnotations(getter, annotations); |
| 106 | - } | |
| 107 | 116 | return annotations; |
| 108 | 117 | } |
| 109 | 118 | |
| 110 | 119 | private static Map<Class<? extends Annotation>, Annotation> scanFieldAnnotations(Field field, Map<Class<? extends Annotation>, Annotation> annotations) { |
| 111 | - if (field != null) { | |
| 112 | - for (Annotation annotation : field.getAnnotations()) { | |
| 113 | - annotations.put(annotation.annotationType(), annotation); | |
| 114 | - } | |
| 120 | + for (Annotation annotation : field.getAnnotations()) { | |
| 121 | + annotations.put(annotation.annotationType(), annotation); | |
| 115 | 122 | } |
| 116 | 123 | return annotations; |
| 117 | 124 | } |
| 118 | 125 | |
| 119 | 126 | private static Map<Class<? extends Annotation>, Annotation> scanMethodAnnotations(Method method, Map<Class<? extends Annotation>, Annotation> annotations) { |
| 120 | - if (method != null) { | |
| 121 | - // 1. direct method annotations | |
| 122 | - for (Annotation annotation : method.getAnnotations()) { | |
| 123 | - annotations.put(annotation.annotationType(), annotation); | |
| 124 | - } | |
| 125 | - // 2. Class hierarchy: check for annotations in overridden methods in superclasses | |
| 126 | - Class<?> getterClass = method.getDeclaringClass(); | |
| 127 | - for (Class<?> clazz = getterClass.getSuperclass(); !clazz.equals(Object.class); clazz = clazz.getSuperclass()) { | |
| 128 | - maybeAddOverriddenMethodAnnotations(annotations, method, clazz); | |
| 129 | - } | |
| 130 | - // 3. Interfaces: check for annotations in implemented interfaces | |
| 131 | - for (Class<?> clazz = getterClass; !clazz.equals(Object.class); clazz = clazz.getSuperclass()) { | |
| 132 | - for (Class<?> itf : clazz.getInterfaces()) { | |
| 133 | - maybeAddOverriddenMethodAnnotations(annotations, method, itf); | |
| 134 | - } | |
| 127 | + // 1. direct method annotations | |
| 128 | + for (Annotation annotation : method.getAnnotations()) { | |
| 129 | + annotations.put(annotation.annotationType(), annotation); | |
| 130 | + } | |
| 131 | + // 2. Class hierarchy: check for annotations in overridden methods in superclasses | |
| 132 | + Class<?> getterClass = method.getDeclaringClass(); | |
| 133 | + for (Class<?> clazz = getterClass.getSuperclass(); !clazz.equals(Object.class); clazz = clazz.getSuperclass()) { | |
| 134 | + maybeAddOverriddenMethodAnnotations(annotations, method, clazz); | |
| 135 | + } | |
| 136 | + // 3. Interfaces: check for annotations in implemented interfaces | |
| 137 | + for (Class<?> clazz = getterClass; !clazz.equals(Object.class); clazz = clazz.getSuperclass()) { | |
| 138 | + for (Class<?> itf : clazz.getInterfaces()) { | |
| 139 | + maybeAddOverriddenMethodAnnotations(annotations, method, itf); | |
| 135 | 140 | } |
| 136 | 141 | } |
| 137 | 142 | return annotations; |
modified manual/object_mapper/creating/README.md +1 −1
| @@ -256,7 +256,7 @@ | ||
| 256 | 256 | they match the schema, in anticipation for the schema generation features |
| 257 | 257 | that will be added in a future version. |
| 258 | 258 | The default annotation is [@Frozen][frozen], [@FrozenKey][frozenkey] and |
| 259 | -[@FrozenValue][frozenvalue]` are also provided for convenience: | |
| 259 | +[@FrozenValue][frozenvalue] are also provided for convenience: | |
| 260 | 260 | |
| 261 | 261 | ```java |
| 262 | 262 | // Will be mapped as a 'list<text>' |