diff --git a/compiler/testData/foreignAnnotations/tests/android.kt b/compiler/testData/foreignAnnotations/tests/android_support.kt similarity index 100% rename from compiler/testData/foreignAnnotations/tests/android.kt rename to compiler/testData/foreignAnnotations/tests/android_support.kt diff --git a/compiler/testData/foreignAnnotations/tests/android.txt b/compiler/testData/foreignAnnotations/tests/android_support.txt similarity index 100% rename from compiler/testData/foreignAnnotations/tests/android.txt rename to compiler/testData/foreignAnnotations/tests/android_support.txt diff --git a/compiler/testData/foreignAnnotations/tests/androidx.kt b/compiler/testData/foreignAnnotations/tests/androidx.kt new file mode 100644 index 00000000000..190f221bb4b --- /dev/null +++ b/compiler/testData/foreignAnnotations/tests/androidx.kt @@ -0,0 +1,43 @@ +// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER +// FILE: A.java + +import androidx.annotation.*; + +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/androidx.txt b/compiler/testData/foreignAnnotations/tests/androidx.txt new file mode 100644 index 00000000000..5db379d2aa2 --- /dev/null +++ b/compiler/testData/foreignAnnotations/tests/androidx.txt @@ -0,0 +1,14 @@ +package + +public fun main(/*0*/ a: A, /*1*/ a1: A): kotlin.Unit + +public open class A { + public constructor A() + @androidx.annotation.Nullable public final var field: kotlin.String? + @androidx.annotation.NonNull public open fun bar(): kotlin.String + @androidx.annotation.Nullable public open fun baz(/*0*/ @androidx.annotation.NonNull x: T): T? + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + @androidx.annotation.Nullable public open fun foo(/*0*/ @androidx.annotation.NonNull x: kotlin.String, /*1*/ @androidx.annotation.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/javac/JavacForeignAnnotationsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/JavacForeignAnnotationsTestGenerated.java index 19a1efe9aaa..c93ad479612 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/JavacForeignAnnotationsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/JavacForeignAnnotationsTestGenerated.java @@ -25,9 +25,15 @@ public class JavacForeignAnnotationsTestGenerated extends AbstractJavacForeignAn KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/foreignAnnotations/tests"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); } - @TestMetadata("android.kt") - public void testAndroid() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/foreignAnnotations/tests/android.kt"); + @TestMetadata("android_support.kt") + public void testAndroidSupport() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/foreignAnnotations/tests/android_support.kt"); + doTest(fileName); + } + + @TestMetadata("androidx.kt") + public void testAndroidSupport() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/foreignAnnotations/tests/androidx.kt"); doTest(fileName); } diff --git a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/JvmAnnotationNames.kt b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/JvmAnnotationNames.kt index f83e3af6e4f..2e2b60d8729 100644 --- a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/JvmAnnotationNames.kt +++ b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/JvmAnnotationNames.kt @@ -20,6 +20,7 @@ import org.jetbrains.kotlin.name.FqName val NULLABLE_ANNOTATIONS = listOf( JvmAnnotationNames.JETBRAINS_NULLABLE_ANNOTATION, + FqName("androidx.annotation.Nullable"), FqName("android.support.annotation.Nullable"), FqName("com.android.annotations.Nullable"), FqName("org.eclipse.jdt.annotation.Nullable"), @@ -38,6 +39,7 @@ val JAVAX_CHECKFORNULL_ANNOTATION = FqName("javax.annotation.CheckForNull") val NOT_NULL_ANNOTATIONS = listOf( JvmAnnotationNames.JETBRAINS_NOT_NULL_ANNOTATION, FqName("edu.umd.cs.findbugs.annotations.NonNull"), + FqName("androidx.annotation.NonNull"), FqName("android.support.annotation.NonNull"), FqName("com.android.annotations.NonNull"), FqName("org.eclipse.jdt.annotation.NonNull"), diff --git a/third-party/annotations/androidx/annotation/NonNull.java b/third-party/annotations/androidx/annotation/NonNull.java new file mode 100644 index 00000000000..e4be4f1fb87 --- /dev/null +++ b/third-party/annotations/androidx/annotation/NonNull.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * 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 androidx.annotation; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.RetentionPolicy.CLASS; + +/** + * Denotes that a parameter, field or method return value can never be null. + *

+ * This is a marker annotation and it has no specific attributes. + */ +@Retention(CLASS) +@Target({METHOD, PARAMETER, FIELD}) +public @interface NonNull { +} diff --git a/third-party/annotations/androidx/annotation/Nullable.java b/third-party/annotations/androidx/annotation/Nullable.java new file mode 100644 index 00000000000..15ce62a282b --- /dev/null +++ b/third-party/annotations/androidx/annotation/Nullable.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * 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 androidx.annotation; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.RetentionPolicy.CLASS; + +/** + * Denotes that a parameter, field or method return value can be null. + *

+ * When decorating a method call parameter, this denotes that the parameter can + * legitimately be null and the method will gracefully deal with it. Typically + * used on optional parameters. + *

+ * When decorating a method, this denotes the method might legitimately return + * null. + *

+ * This is a marker annotation and it has no specific attributes. + */ +@Retention(CLASS) +@Target({METHOD, PARAMETER, FIELD}) +public @interface Nullable { +}