Elffuss BraidHub

kikocis/java-driver public

Minor enhancements.
Changesete18aa051474109aded44db17839fd8e01b936fe1d6705d0b2f518e2927830c05
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
Parents270ab72db05a

Browse the files at this changeset

5 files changed

modified driver-mapping/src/main/java/com/datastax/driver/mapping/AnnotationChecks.java +1 −1
@@ -105,7 +105,7 @@
105105 static void validateOrder(List<PropertyMapper> properties, String annotation) {
106106 for (int i = 0; i < properties.size(); i++) {
107107 PropertyMapper property = properties.get(i);
108- int pos = property.getPosition();
108+ int pos = property.position;
109109 if (pos != i)
110110 throw new IllegalArgumentException(String.format("Invalid ordering value %d for annotation %s of property '%s', was expecting %d",
111111 pos, annotation, property, i));
modified driver-mapping/src/main/java/com/datastax/driver/mapping/AnnotationParser.java +3 −8
@@ -42,7 +42,7 @@
4242 * - {@link #parseUDT(Class, MappingManager)}: UDT classes into {@link MappedUDTCodec} instances.
4343 * - {@link #parseAccessor(Class, MappingManager)}: Accessor interfaces into {@link AccessorMapper} instances.
4444 */
45-@SuppressWarnings("unchecked")
45+@SuppressWarnings({"unchecked", "WeakerAccess"})
4646 class AnnotationParser {
4747
4848 @VisibleForTesting
@@ -67,7 +67,7 @@
6767 private static final Comparator<PropertyMapper> POSITION_COMPARATOR = new Comparator<PropertyMapper>() {
6868 @Override
6969 public int compare(PropertyMapper o1, PropertyMapper o2) {
70- return o1.getPosition() - o2.getPosition();
70+ return o1.position - o2.position;
7171 }
7272 };
7373
@@ -112,7 +112,7 @@
112112 java.lang.reflect.Field field = (java.lang.reflect.Field) entry.getValue()[0];
113113 PropertyDescriptor property = (PropertyDescriptor) entry.getValue()[1];
114114 String alias = (columnCounter != null)
115- ? newAlias(columnCounter.incrementAndGet())
115+ ? "col" + columnCounter.incrementAndGet()
116116 : null;
117117
118118 PropertyMapper propertyMapper = new PropertyMapper(propertyName, alias, field, property);
@@ -262,11 +262,6 @@
262262 return new AccessorMapper<T>(accClass, methods);
263263 }
264264
265- private static String newAlias(int columnNumber) {
266- return "col" + columnNumber;
267- }
268-
269- @SuppressWarnings({"unchecked", "rawtypes"})
270265 private static ParamMapper newParamMapper(String className, String methodName, int idx, String paramName, Class<? extends TypeCodec<?>> codecClass, Type paramType, MappingManager mappingManager) {
271266 if (paramType instanceof Class) {
272267 Class<?> paramClass = (Class<?>) paramType;
modified driver-mapping/src/main/java/com/datastax/driver/mapping/PropertyMapper.java +1 −5
@@ -44,11 +44,11 @@
4444 final String columnName;
4545 final TypeToken<Object> javaType;
4646 final TypeCodec<Object> customCodec;
47+ final int position;
4748
4849 private final Field field;
4950 private final Method getter;
5051 private final Method setter;
51- private final int position;
5252 private final Map<Class<? extends Annotation>, Annotation> annotations;
5353
5454 PropertyMapper(String propertyName, String alias, Field field, PropertyDescriptor property) {
@@ -121,10 +121,6 @@
121121 return hasAnnotation(ClusteringColumn.class);
122122 }
123123
124- int getPosition() {
125- return position;
126- }
127-
128124 private String inferColumnName() {
129125 Column column = annotation(Column.class);
130126 if (column != null && !column.name().isEmpty()) {
modified driver-mapping/src/main/java/com/datastax/driver/mapping/ReflectionUtils.java +34 −29
@@ -35,20 +35,29 @@
3535 class ReflectionUtils {
3636
3737 static <T> T newInstance(Class<T> clazz) {
38+ Constructor<T> publicConstructor;
3839 try {
39- return clazz.newInstance();
40- } catch (Exception e) {
40+ publicConstructor = clazz.getConstructor();
41+ } catch (NoSuchMethodException e) {
4142 try {
4243 // 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();
4647 } 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);
4849 }
4950 }
51+ try {
52+ return publicConstructor.newInstance();
53+ } catch (Exception e) {
54+ throw new IllegalStateException("Can't create an instance of " + clazz, e);
55+ }
5056 }
5157
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
5261 static <T> Map<String, Object[]> scanFieldsAndProperties(Class<T> baseClass) {
5362 Map<String, Object[]> fieldsAndProperties = new HashMap<String, Object[]>();
5463 Map<String, Field> fields = scanFields(baseClass);
@@ -99,39 +108,35 @@
99108 static Map<Class<? extends Annotation>, Annotation> scanPropertyAnnotations(Field field, PropertyDescriptor property) {
100109 Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
101110 // 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)
105115 scanMethodAnnotations(getter, annotations);
106- }
107116 return annotations;
108117 }
109118
110119 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);
115122 }
116123 return annotations;
117124 }
118125
119126 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);
135140 }
136141 }
137142 return annotations;
modified manual/object_mapper/creating/README.md +1 −1
@@ -256,7 +256,7 @@
256256 they match the schema, in anticipation for the schema generation features
257257 that will be added in a future version.
258258 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:
260260
261261 ```java
262262 // Will be mapped as a 'list<text>'