Skip to content

Commit 31a379b

Browse files
muzarskiavelanarius
authored andcommitted
Record annotation warning fix in mapper-processor
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
1 parent 0e84681 commit 31a379b

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

Diff for: mapper-processor/src/main/java/com/datastax/oss/driver/internal/mapper/processor/entity/DefaultEntityFactory.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -510,15 +510,18 @@ private Optional<PropertyStrategy> getPropertyStrategy(Set<TypeElement> typeHier
510510

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

0 commit comments

Comments
 (0)