From 8df40eaa46b0b41ca556fa528a219afe8deb05d2 Mon Sep 17 00:00:00 2001 From: Christopher Horner Date: Wed, 19 Apr 2017 13:29:35 +1000 Subject: [PATCH] Add support for RxJava's nullability annotations. --- .../io/reactivex/annotations/NonNull.java | 29 +++++++++++++ .../io/reactivex/annotations/Nullable.java | 32 ++++++++++++++ .../foreignAnnotations/tests/rxjava.kt | 42 +++++++++++++++++++ .../foreignAnnotations/tests/rxjava.txt | 14 +++++++ .../ForeignAnnotationsTestGenerated.java | 6 +++ .../kotlin/load/java/JvmAnnotationNames.kt | 6 ++- .../kotlin/renderer/DescriptorRenderer.kt | 4 +- 7 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/foreignAnnotations/annotations/io/reactivex/annotations/NonNull.java create mode 100644 compiler/testData/foreignAnnotations/annotations/io/reactivex/annotations/Nullable.java create mode 100644 compiler/testData/foreignAnnotations/tests/rxjava.kt create mode 100644 compiler/testData/foreignAnnotations/tests/rxjava.txt diff --git a/compiler/testData/foreignAnnotations/annotations/io/reactivex/annotations/NonNull.java b/compiler/testData/foreignAnnotations/annotations/io/reactivex/annotations/NonNull.java new file mode 100644 index 00000000000..8b52ef1a9bb --- /dev/null +++ b/compiler/testData/foreignAnnotations/annotations/io/reactivex/annotations/NonNull.java @@ -0,0 +1,29 @@ +/** + * Copyright (c) 2016-present, RxJava Contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is + * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See + * the License for the specific language governing permissions and limitations under the License. + */ + +package io.reactivex.annotations; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.*; +import static java.lang.annotation.RetentionPolicy.CLASS; + +/** + * Indicates that a field/parameter/variable/return type is never null. + */ +@Documented +@Target(value = {FIELD, METHOD, PARAMETER, LOCAL_VARIABLE}) +@Retention(value = CLASS) +public @interface NonNull { } diff --git a/compiler/testData/foreignAnnotations/annotations/io/reactivex/annotations/Nullable.java b/compiler/testData/foreignAnnotations/annotations/io/reactivex/annotations/Nullable.java new file mode 100644 index 00000000000..512fc752174 --- /dev/null +++ b/compiler/testData/foreignAnnotations/annotations/io/reactivex/annotations/Nullable.java @@ -0,0 +1,32 @@ +/** + * Copyright (c) 2016-present, RxJava Contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is + * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See + * the License for the specific language governing permissions and limitations under the License. + */ + +package io.reactivex.annotations; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.LOCAL_VARIABLE; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.RetentionPolicy.CLASS; + +/** + * Indicates that a field/parameter/variable/return type may be null. + */ +@Documented +@Target(value = {FIELD, METHOD, PARAMETER, LOCAL_VARIABLE}) +@Retention(value = CLASS) +public @interface Nullable { } diff --git a/compiler/testData/foreignAnnotations/tests/rxjava.kt b/compiler/testData/foreignAnnotations/tests/rxjava.kt new file mode 100644 index 00000000000..2661011370a --- /dev/null +++ b/compiler/testData/foreignAnnotations/tests/rxjava.kt @@ -0,0 +1,42 @@ +// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER +// FILE: A.java + +import io.reactivex.annotations.*; + +public class A { + @Nullable public String field = null; + + @Nullable + public String foo(@NonNull String x, @Nullable CharSequence y) { + return ""; + } + + @NonNull + public String bar() { + return ""; + } + + @Nullable + public T baz(@NonNull T x) { return x; } +} + +// FILE: main.kt + +fun main(a: A, a1: A) { + a.foo("", null)?.length + a.foo("", null).length + a.foo(null, "").length + + a.bar().length + a.bar()!!.length + + a.field?.length + a.field.length + + a.baz("").length + a.baz("")?.length + a.baz(null).length + + a1.baz("")!!.length + a1.baz(null)!!.length +} diff --git a/compiler/testData/foreignAnnotations/tests/rxjava.txt b/compiler/testData/foreignAnnotations/tests/rxjava.txt new file mode 100644 index 00000000000..d7355e89103 --- /dev/null +++ b/compiler/testData/foreignAnnotations/tests/rxjava.txt @@ -0,0 +1,14 @@ +package + +public fun main(/*0*/ a: A, /*1*/ a1: A): kotlin.Unit + +public open class A { + public constructor A() + @io.reactivex.annotations.Nullable public final var field: kotlin.String? + @io.reactivex.annotations.NonNull public open fun bar(): kotlin.String + @io.reactivex.annotations.Nullable public open fun baz(/*0*/ @io.reactivex.annotations.NonNull x: T): T? + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + @io.reactivex.annotations.Nullable public open fun foo(/*0*/ @io.reactivex.annotations.NonNull x: kotlin.String, /*1*/ @io.reactivex.annotations.Nullable y: kotlin.CharSequence?): kotlin.String? + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/ForeignAnnotationsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/ForeignAnnotationsTestGenerated.java index b4ae5d44b3f..9c6ef4a267a 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/ForeignAnnotationsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/ForeignAnnotationsTestGenerated.java @@ -77,4 +77,10 @@ public class ForeignAnnotationsTestGenerated extends AbstractForeignAnnotationsT String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/foreignAnnotations/tests/lombokSimple.kt"); doTest(fileName); } + + @TestMetadata("rxjava.kt") + public void testRxjava() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/foreignAnnotations/tests/rxjava.kt"); + doTest(fileName); + } } diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/JvmAnnotationNames.kt b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/JvmAnnotationNames.kt index 8475bb2541d..c536e7c3826 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/JvmAnnotationNames.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/JvmAnnotationNames.kt @@ -28,7 +28,8 @@ val NULLABLE_ANNOTATIONS = listOf( FqName("javax.annotation.CheckForNull"), FqName("edu.umd.cs.findbugs.annotations.CheckForNull"), FqName("edu.umd.cs.findbugs.annotations.Nullable"), - FqName("edu.umd.cs.findbugs.annotations.PossiblyNull") + FqName("edu.umd.cs.findbugs.annotations.PossiblyNull"), + FqName("io.reactivex.annotations.Nullable") ) val JAVAX_NONNULL_ANNOTATION = FqName("javax.annotation.Nonnull") @@ -40,7 +41,8 @@ val NOT_NULL_ANNOTATIONS = listOf( FqName("com.android.annotations.NonNull"), FqName("org.eclipse.jdt.annotation.NonNull"), FqName("org.checkerframework.checker.nullness.qual.NonNull"), - FqName("lombok.NonNull") + FqName("lombok.NonNull"), + FqName("io.reactivex.annotations.NonNull") ) val READ_ONLY_ANNOTATIONS = listOf( diff --git a/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRenderer.kt b/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRenderer.kt index f95a964c5a6..fd5828183ce 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRenderer.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRenderer.kt @@ -238,7 +238,9 @@ object ExcludedTypeAnnotations { FqName("edu.umd.cs.findbugs.annotations.CheckForNull"), FqName("edu.umd.cs.findbugs.annotations.Nullable"), FqName("edu.umd.cs.findbugs.annotations.PossiblyNull"), - FqName("lombok.NonNull") + FqName("lombok.NonNull"), + FqName("io.reactivex.annotations.Nullable"), + FqName("io.reactivex.annotations.NonNull") ) val internalAnnotationsForResolve = setOf(