Skip to content

Commit d30cbd2

Browse files
authored
Support java records as measurement classes (#983)
1 parent 738acd5 commit d30cbd2

File tree

8 files changed

+1631
-44
lines changed

8 files changed

+1631
-44
lines changed

MANUAL.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ public class Cpu {
323323
}
324324
```
325325

326-
2. Add @Measurement,@TimeColumn and @Column annotations (column names default to field names unless otherwise specified):
326+
2. Add @Measurement, @TimeColumn and @Column annotations (column names default to field names unless otherwise specified):
327327

328328
```Java
329329
@Measurement(name = "cpu")
@@ -364,6 +364,24 @@ public class Cpu {
364364
}
365365
```
366366

367+
Or (if you're on JDK14+ and/or [Android SDK34+](https://android-developers.googleblog.com/2023/06/records-in-android-studio-flamingo.html)):
368+
369+
```Java
370+
@Measurement(name = "cpu", allFields = true)
371+
public record Cpu(
372+
@TimeColumn
373+
Instant time,
374+
@Column(name = "host", tag = true)
375+
String hostname,
376+
@Column(tag = true)
377+
String region,
378+
Double idle,
379+
Boolean happydevop,
380+
@Column(name = "uptimesecs")
381+
Long uptimeSecs
382+
) {}
383+
```
384+
367385
3. Call _InfluxDBResultMapper.toPOJO(...)_ to map the QueryResult to your POJO:
368386

369387
```java

pom.xml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@
8484
<configuration>
8585
<source>1.8</source>
8686
<target>1.8</target>
87+
<compilerArgs>
88+
<!-- Needed for Android desugared record tests -->
89+
<arg>-parameters</arg>
90+
</compilerArgs>
8791
</configuration>
8892
</plugin>
8993
<plugin>
@@ -405,5 +409,50 @@
405409
</plugins>
406410
</build>
407411
</profile>
412+
<profile>
413+
<!-- A different setup for JDK 17 -->
414+
<id>java17</id>
415+
<activation>
416+
<jdk>17</jdk>
417+
</activation>
418+
<build>
419+
<plugins>
420+
<plugin>
421+
<groupId>org.codehaus.mojo</groupId>
422+
<artifactId>build-helper-maven-plugin</artifactId>
423+
<executions>
424+
<execution>
425+
<id>add-test-source</id>
426+
<phase>generate-test-sources</phase>
427+
<goals>
428+
<goal>add-test-source</goal>
429+
</goals>
430+
<configuration>
431+
<sources>
432+
<source>src/test-jdk17/java</source>
433+
</sources>
434+
</configuration>
435+
</execution>
436+
</executions>
437+
</plugin>
438+
<plugin>
439+
<groupId>org.apache.maven.plugins</groupId>
440+
<artifactId>maven-compiler-plugin</artifactId>
441+
<inherited>true</inherited>
442+
<configuration>
443+
<!-- Enable Java 17 for all sources so that Intellij picks the right language level -->
444+
<source>17</source>
445+
<release>17</release>
446+
<compilerArgs>
447+
<!-- Needed for record tests -->
448+
<arg>-parameters</arg>
449+
<arg>--add-opens=java.base/java.lang=ALL-UNNAMED</arg>
450+
<arg>--add-opens=java.base/java.util=ALL-UNNAMED</arg>
451+
</compilerArgs>
452+
</configuration>
453+
</plugin>
454+
</plugins>
455+
</build>
456+
</profile>
408457
</profiles>
409458
</project>

src/main/java/org/influxdb/annotation/Exclude.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
/**
2929
* When a POJO annotated with {@code @Measurement(allFields = true)} is loaded or saved,
3030
* this annotation can be used to exclude some of its fields.
31+
* <p>
32+
* Note: this is not considered when loading record measurements.
33+
*
3134
* @see Measurement#allFields()
3235
*
3336
* @author Eran Leshem

src/main/java/org/influxdb/annotation/Measurement.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
/**
4545
* If {@code true}, then all non-static fields of this measurement will be loaded or saved,
4646
* regardless of any {@code @Column} annotations.
47+
* <p>
48+
* Note: When loading record measurements, this is always implied to be true,
49+
* since the record's canonical constructor is used to populate the record.
50+
*
4751
* @see Exclude
4852
*/
4953
boolean allFields() default false;

0 commit comments

Comments
 (0)