|
25 | 25 | import java.util.Set; |
26 | 26 |
|
27 | 27 | import javax.annotation.processing.ProcessingEnvironment; |
| 28 | +import javax.lang.model.element.AnnotationMirror; |
28 | 29 | import javax.lang.model.element.Element; |
29 | 30 | import javax.lang.model.element.ElementKind; |
30 | 31 | import javax.lang.model.element.ExecutableElement; |
@@ -375,7 +376,7 @@ private boolean categorizeField(Element element) { |
375 | 376 | if (!categorizeIndexField(element, field)) { return false; } |
376 | 377 | } |
377 | 378 |
|
378 | | - if (field.getAnnotation(Required.class) != null) { |
| 379 | + if (isRequiredField(field)) { |
379 | 380 | categorizeRequiredField(element, field); |
380 | 381 | } else { |
381 | 382 | // The field doesn't have the @Required annotation. |
@@ -408,6 +409,23 @@ private boolean categorizeField(Element element) { |
408 | 409 | return true; |
409 | 410 | } |
410 | 411 |
|
| 412 | + private boolean isRequiredField(VariableElement field) { |
| 413 | + if (field.getAnnotation(Required.class) != null) { |
| 414 | + return true; |
| 415 | + } |
| 416 | + |
| 417 | + // Kotlin uses the `org.jetbrains.annotations.NotNull` annotation to mark non-null fields. |
| 418 | + // In order to fully support the Kotlin type system we interpret `@NotNull` as an alias |
| 419 | + // for `@Required` |
| 420 | + for (AnnotationMirror annotation : field.getAnnotationMirrors()) { |
| 421 | + if (annotation.getAnnotationType().toString().equals("org.jetbrains.annotations.NotNull")) { |
| 422 | + return true; |
| 423 | + } |
| 424 | + } |
| 425 | + |
| 426 | + return false; |
| 427 | + } |
| 428 | + |
411 | 429 | // The field has the @Index annotation. It's only valid for column types: |
412 | 430 | // STRING, DATE, INTEGER, BOOLEAN, and RealmMutableInteger |
413 | 431 | private boolean categorizeIndexField(Element element, VariableElement variableElement) { |
@@ -441,13 +459,13 @@ private boolean categorizeIndexField(Element element, VariableElement variableEl |
441 | 459 | private void categorizeRequiredField(Element element, VariableElement variableElement) { |
442 | 460 | if (Utils.isPrimitiveType(variableElement)) { |
443 | 461 | Utils.error(String.format(Locale.US, |
444 | | - "@Required annotation is unnecessary for primitive field \"%s\".", element)); |
| 462 | + "@Required or @NotNull annotation is unnecessary for primitive field \"%s\".", element)); |
445 | 463 | return; |
446 | 464 | } |
447 | 465 |
|
448 | | - if (Utils.isRealmList(variableElement) || Utils.isRealmModel(variableElement)) { |
| 466 | + if (Utils.isRealmModel(variableElement)) { |
449 | 467 | Utils.error(String.format(Locale.US, |
450 | | - "Field \"%s\" with type \"%s\" cannot be @Required.", element, element.asType())); |
| 468 | + "Field \"%s\" with type \"%s\" cannot be @Required or @NotNull.", element, element.asType())); |
451 | 469 | return; |
452 | 470 | } |
453 | 471 |
|
|
0 commit comments