20
20
*/
21
21
package org .influxdb .impl ;
22
22
23
+ import org .influxdb .InfluxDBMapperException ;
24
+ import org .influxdb .annotation .Column ;
25
+ import org .influxdb .annotation .Exclude ;
26
+ import org .influxdb .annotation .Measurement ;
27
+ import org .influxdb .dto .QueryResult ;
28
+
23
29
import java .lang .reflect .Field ;
30
+ import java .lang .reflect .Modifier ;
24
31
import java .time .Instant ;
25
32
import java .time .format .DateTimeFormatter ;
26
33
import java .time .format .DateTimeFormatterBuilder ;
33
40
import java .util .concurrent .ConcurrentMap ;
34
41
import java .util .concurrent .TimeUnit ;
35
42
36
- import org .influxdb .InfluxDBMapperException ;
37
- import org .influxdb .annotation .Column ;
38
- import org .influxdb .annotation .Measurement ;
39
- import org .influxdb .dto .QueryResult ;
40
-
41
43
/**
42
44
* Main class responsible for mapping a QueryResult to a POJO.
43
45
*
@@ -213,20 +215,34 @@ void cacheMeasurementClass(final Class<?>... classVarAgrs) {
213
215
}
214
216
ConcurrentMap <String , Field > influxColumnAndFieldMap = new ConcurrentHashMap <>();
215
217
218
+ Measurement measurement = clazz .getAnnotation (Measurement .class );
219
+ boolean allFields = measurement != null && measurement .allFields ();
220
+
216
221
Class <?> c = clazz ;
217
222
while (c != null ) {
218
223
for (Field field : c .getDeclaredFields ()) {
219
224
Column colAnnotation = field .getAnnotation (Column .class );
220
- if (colAnnotation != null ) {
221
- influxColumnAndFieldMap .put (colAnnotation .name (), field );
225
+ if (colAnnotation == null && !(allFields
226
+ && !field .isAnnotationPresent (Exclude .class ) && !Modifier .isStatic (field .getModifiers ()))) {
227
+ continue ;
222
228
}
229
+
230
+ influxColumnAndFieldMap .put (getFieldName (field , colAnnotation ), field );
223
231
}
224
232
c = c .getSuperclass ();
225
233
}
226
234
CLASS_FIELD_CACHE .putIfAbsent (clazz .getName (), influxColumnAndFieldMap );
227
235
}
228
236
}
229
237
238
+ private static String getFieldName (final Field field , final Column colAnnotation ) {
239
+ if (colAnnotation != null && !colAnnotation .name ().isEmpty ()) {
240
+ return colAnnotation .name ();
241
+ }
242
+
243
+ return field .getName ();
244
+ }
245
+
230
246
String getMeasurementName (final Class <?> clazz ) {
231
247
return ((Measurement ) clazz .getAnnotation (Measurement .class )).name ();
232
248
}
@@ -289,17 +305,11 @@ <T> List<T> parseSeriesAs(final QueryResult.Series series, final Class<T> clazz,
289
305
290
306
/**
291
307
* InfluxDB client returns any number as Double.
292
- * See https://github.com/influxdata/influxdb-java/issues/153#issuecomment-259681987
308
+ * See <a href=" https://github.com/influxdata/influxdb-java/issues/153#issuecomment-259681987">...</a>
293
309
* for more information.
294
310
*
295
- * @param object
296
- * @param field
297
- * @param value
298
- * @param precision
299
- * @throws IllegalArgumentException
300
- * @throws IllegalAccessException
301
311
*/
302
- <T > void setFieldValue (final T object , final Field field , final Object value , final TimeUnit precision )
312
+ private static <T > void setFieldValue (final T object , final Field field , final Object value , final TimeUnit precision )
303
313
throws IllegalArgumentException , IllegalAccessException {
304
314
if (value == null ) {
305
315
return ;
@@ -325,8 +335,8 @@ <T> void setFieldValue(final T object, final Field field, final Object value, fi
325
335
}
326
336
}
327
337
328
- <T > boolean fieldValueModified (final Class <?> fieldType , final Field field , final T object , final Object value ,
329
- final TimeUnit precision )
338
+ static <T > boolean fieldValueModified (final Class <?> fieldType , final Field field , final T object , final Object value ,
339
+ final TimeUnit precision )
330
340
throws IllegalArgumentException , IllegalAccessException {
331
341
if (String .class .isAssignableFrom (fieldType )) {
332
342
field .set (object , String .valueOf (value ));
@@ -351,8 +361,9 @@ <T> boolean fieldValueModified(final Class<?> fieldType, final Field field, fina
351
361
return false ;
352
362
}
353
363
354
- <T > boolean fieldValueForPrimitivesModified (final Class <?> fieldType , final Field field , final T object ,
355
- final Object value ) throws IllegalArgumentException , IllegalAccessException {
364
+ static <T > boolean fieldValueForPrimitivesModified (final Class <?> fieldType , final Field field , final T object ,
365
+ final Object value )
366
+ throws IllegalArgumentException , IllegalAccessException {
356
367
if (double .class .isAssignableFrom (fieldType )) {
357
368
field .setDouble (object , ((Double ) value ).doubleValue ());
358
369
return true ;
@@ -372,8 +383,9 @@ <T> boolean fieldValueForPrimitivesModified(final Class<?> fieldType, final Fiel
372
383
return false ;
373
384
}
374
385
375
- <T > boolean fieldValueForPrimitiveWrappersModified (final Class <?> fieldType , final Field field , final T object ,
376
- final Object value ) throws IllegalArgumentException , IllegalAccessException {
386
+ static <T > boolean fieldValueForPrimitiveWrappersModified (final Class <?> fieldType , final Field field , final T object ,
387
+ final Object value )
388
+ throws IllegalArgumentException , IllegalAccessException {
377
389
if (Double .class .isAssignableFrom (fieldType )) {
378
390
field .set (object , value );
379
391
return true ;
@@ -393,7 +405,7 @@ <T> boolean fieldValueForPrimitiveWrappersModified(final Class<?> fieldType, fin
393
405
return false ;
394
406
}
395
407
396
- private Long toMillis (final long value , final TimeUnit precision ) {
408
+ private static Long toMillis (final long value , final TimeUnit precision ) {
397
409
398
410
return TimeUnit .MILLISECONDS .convert (value , precision );
399
411
}
0 commit comments