Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@Column to support support super class inheritance #562

Merged
merged 3 commits into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
12 changes: 8 additions & 4 deletions src/main/java/org/influxdb/impl/InfluxDBResultMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Expand Down
58 changes: 57 additions & 1 deletion src/test/java/org/influxdb/impl/InfluxDBResultMapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> columnList = Arrays.asList("superValue", "subValue");

List<Object> 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<MySubMeasurement> 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 {

Expand Down Expand Up @@ -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")
Expand Down