Skip to content

Commit

Permalink
Record annotation warning fix in mapper-processor
Browse files Browse the repository at this point in the history
Java 14 introduces record keyword which stands for immutable data class. 
Records require only the name and types of fields. Java generates some 
code to operate on the record's objects - including public getter 
methods.

When annotating record with @entity annotation and its field with one 
of the exclusive annotations, we get a compilation warning:

  @entity
  public record FooRecord(
      @PartitionKey int fooPK,
      @ClusteringColumn String fooCC
  ) { }

  /home/mikolajuzarski/playground/java-driver-test/src/main/java/org/example/FooRecord.java:9:27
  java: [FooRecord.fooPK] @PartitionKey should be used either on the field or the getter, but not both. The annotation on this field will be ignored.

  /home/mikolajuzarski/playground/java-driver-test/src/main/java/org/example/FooRecord.java:10:34
  java: [FooRecord.fooCC] @ClusteringColumn should be used either on the field or the getter, but not both. The annotation on this field will be ignored.

Java duplicates the annotation on the record's field to the corresponding
getter method as well. This results in generating the warning when using 
records and exclusive field annotations.

This commit fixes the issue, so the compilation warning message is
not printed for records.

Fixes #246
  • Loading branch information
muzarski authored and avelanarius committed Dec 13, 2023
1 parent b97a4de commit 8af74b5
Showing 1 changed file with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -510,15 +510,18 @@ private Optional<PropertyStrategy> getPropertyStrategy(Set<TypeElement> typeHier

private void reportMultipleAnnotationError(
Element element, Class<? extends Annotation> a0, Class<? extends Annotation> a1) {
if (a0 == a1) {
// A hack to prevent displaying warning messages when annotating record's fields.
// See: https://github.com/scylladb/java-driver/issues/246
boolean isParentRecord = element.getEnclosingElement().getKind().name().equals("RECORD");
if (a0 == a1 && !isParentRecord) {
context
.getMessager()
.warn(
element,
"@%s should be used either on the field or the getter, but not both. "
+ "The annotation on this field will be ignored.",
a0.getSimpleName());
} else {
} else if (a0 != a1) {
context
.getMessager()
.error(
Expand Down

0 comments on commit 8af74b5

Please sign in to comment.