Add support for RxJava's nullability annotations.

This commit is contained in:
Christopher Horner
2017-04-19 13:29:35 +10:00
committed by Denis Zharkov
parent aee5326ca7
commit 8df40eaa46
7 changed files with 130 additions and 3 deletions
@@ -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 { }
@@ -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 { }
+42
View File
@@ -0,0 +1,42 @@
// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER
// FILE: A.java
import io.reactivex.annotations.*;
public class A<T> {
@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<String>, a1: A<String?>) {
a.foo("", null)?.length
a.foo("", null)<!UNSAFE_CALL!>.<!>length
a.foo(<!NULL_FOR_NONNULL_TYPE!>null<!>, "")<!UNSAFE_CALL!>.<!>length
a.bar().length
a.bar()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>.length
a.field?.length
a.field<!UNSAFE_CALL!>.<!>length
a.baz("")<!UNSAFE_CALL!>.<!>length
a.baz("")?.length
a.baz(<!NULL_FOR_NONNULL_TYPE!>null<!>)<!UNSAFE_CALL!>.<!>length
a1.baz("")!!.length
a1.baz(<!NULL_FOR_NONNULL_TYPE!>null<!>)!!.length
}
+14
View File
@@ -0,0 +1,14 @@
package
public fun main(/*0*/ a: A<kotlin.String>, /*1*/ a1: A<kotlin.String?>): kotlin.Unit
public open class A</*0*/ T : kotlin.Any!> {
public constructor A</*0*/ T : kotlin.Any!>()
@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
}
@@ -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);
}
}
@@ -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(
@@ -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(