diff --git a/CHANGELOG.md b/CHANGELOG.md index b571a3a96..bcdff3971 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ would be used [Issue #548](https://github.com/influxdata/influxdb-java/issues/548) - Add BatchPoints.Builder.points(Collection) [Issue #451](https://github.com/influxdata/influxdb-java/issues/451) +- @Column supports class inheritance + [Issue #367](https://github.com/influxdata/influxdb-java/issues/367) ## 2.14 [2018-10-12] diff --git a/src/main/java/org/influxdb/impl/InfluxDBResultMapper.java b/src/main/java/org/influxdb/impl/InfluxDBResultMapper.java index ec66028de..cdbf8e9a1 100644 --- a/src/main/java/org/influxdb/impl/InfluxDBResultMapper.java +++ b/src/main/java/org/influxdb/impl/InfluxDBResultMapper.java @@ -217,11 +217,15 @@ void cacheMeasurementClass(final Class... classVarAgrs) { influxColumnAndFieldMap = initialMap; } - for (Field field : clazz.getDeclaredFields()) { - Column colAnnotation = field.getAnnotation(Column.class); - if (colAnnotation != null) { - influxColumnAndFieldMap.put(colAnnotation.name(), field); + Class c = clazz; + while (c != null) { + for (Field field : c.getDeclaredFields()) { + Column colAnnotation = field.getAnnotation(Column.class); + if (colAnnotation != null) { + influxColumnAndFieldMap.put(colAnnotation.name(), field); + } } + c = c.getSuperclass(); } } } diff --git a/src/test/java/org/influxdb/impl/InfluxDBResultMapperTest.java b/src/test/java/org/influxdb/impl/InfluxDBResultMapperTest.java index 445528fb6..fd3dfb7ef 100644 --- a/src/test/java/org/influxdb/impl/InfluxDBResultMapperTest.java +++ b/src/test/java/org/influxdb/impl/InfluxDBResultMapperTest.java @@ -409,6 +409,38 @@ void testToPOJO_SetMeasureName() { Assertions.assertTrue(result.size() == 1); } + @Test + void testToPOJOInheritance() { + // Given... + mapper.cacheMeasurementClass(MySubMeasurement.class); + + String superValue = UUID.randomUUID().toString(); + String subValue = "my sub value"; + List columnList = Arrays.asList("superValue", "subValue"); + + List firstSeriesResult = Arrays.asList(superValue, subValue); + + QueryResult.Series series = new QueryResult.Series(); + series.setName("MySeriesName"); + series.setColumns(columnList); + series.setValues(Arrays.asList(firstSeriesResult)); + + QueryResult.Result internalResult = new QueryResult.Result(); + internalResult.setSeries(Arrays.asList(series)); + + QueryResult queryResult = new QueryResult(); + queryResult.setResults(Arrays.asList(internalResult)); + + //When... + List result = + mapper.toPOJO(queryResult, MySubMeasurement.class, "MySeriesName"); + + //Then... + Assertions.assertTrue(result.size() == 1); + Assertions.assertEquals(superValue, result.get(0).superValue); + Assertions.assertEquals(subValue, result.get(0).subValue); + } + @Measurement(name = "CustomMeasurement") static class MyCustomMeasurement { @@ -456,7 +488,31 @@ public String toString() { } } - @Measurement(name = "foo") + @Measurement(name = "SuperMeasurement") + static class MySuperMeasurement { + + @Column(name = "superValue") + protected String superValue; + + @Override + public String toString() { + return "SuperMeasurement [superValue=" + superValue + "]"; + } + } + + @Measurement(name = "SubMeasurement") + static class MySubMeasurement extends MySuperMeasurement { + + @Column(name = "subValue") + protected String subValue; + + @Override + public String toString() { + return "MySubMeasurement [subValue=" + subValue + ", superValue=" + superValue + "]"; + } + } + + @Measurement(name = "foo") static class MyPojoWithUnsupportedField { @Column(name = "bar")