From a42406f2aa82f6dff03cf6ac74dfb7024c5ba050 Mon Sep 17 00:00:00 2001 From: Tor Norbye Date: Wed, 16 Jan 2019 13:54:17 -0800 Subject: [PATCH] KT-29586: Add android.annotation annotations KT-29586 Fixed --- .../foreignAnnotations/tests/androidSdk.kt | 42 +++++++++++++++++++ .../foreignAnnotations/tests/androidSdk.txt | 14 +++++++ ...sNoAnnotationInClasspathTestGenerated.java | 5 +++ ...pathWithFastClassReadingTestGenerated.java | 5 +++ .../ForeignAnnotationsTestGenerated.java | 5 +++ .../JavacForeignAnnotationsTestGenerated.java | 5 +++ .../kotlin/load/java/JvmAnnotationNames.kt | 2 + .../android/annotation/NonNull.java | 34 +++++++++++++++ .../android/annotation/Nullable.java | 41 ++++++++++++++++++ 9 files changed, 153 insertions(+) create mode 100644 compiler/testData/foreignAnnotations/tests/androidSdk.kt create mode 100644 compiler/testData/foreignAnnotations/tests/androidSdk.txt create mode 100644 third-party/annotations/android/annotation/NonNull.java create mode 100644 third-party/annotations/android/annotation/Nullable.java diff --git a/compiler/testData/foreignAnnotations/tests/androidSdk.kt b/compiler/testData/foreignAnnotations/tests/androidSdk.kt new file mode 100644 index 00000000000..1e6fe093ed7 --- /dev/null +++ b/compiler/testData/foreignAnnotations/tests/androidSdk.kt @@ -0,0 +1,42 @@ +// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER +// FILE: A.java + +import android.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/androidSdk.txt b/compiler/testData/foreignAnnotations/tests/androidSdk.txt new file mode 100644 index 00000000000..b581eea64db --- /dev/null +++ b/compiler/testData/foreignAnnotations/tests/androidSdk.txt @@ -0,0 +1,14 @@ +package + +public fun main(/*0*/ a: A, /*1*/ a1: A): kotlin.Unit + +public open class A { + public constructor A() + @android.annotation.Nullable public final var field: kotlin.String? + @android.annotation.NonNull public open fun bar(): kotlin.String + @android.annotation.Nullable public open fun baz(/*0*/ @android.annotation.NonNull x: T): T? + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + @android.annotation.Nullable public open fun foo(/*0*/ @android.annotation.NonNull x: kotlin.String, /*1*/ @android.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/ForeignAnnotationsNoAnnotationInClasspathTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/ForeignAnnotationsNoAnnotationInClasspathTestGenerated.java index 57ff6ae9c3b..f1fc51ff280 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/ForeignAnnotationsNoAnnotationInClasspathTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/ForeignAnnotationsNoAnnotationInClasspathTestGenerated.java @@ -34,6 +34,11 @@ public class ForeignAnnotationsNoAnnotationInClasspathTestGenerated extends Abst runTest("compiler/testData/foreignAnnotations/tests/androidRecently.kt"); } + @TestMetadata("androidSdk.kt") + public void testAndroidSdk() throws Exception { + runTest("compiler/testData/foreignAnnotations/tests/androidSdk.kt"); + } + @TestMetadata("android_support.kt") public void testAndroid_support() throws Exception { runTest("compiler/testData/foreignAnnotations/tests/android_support.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/ForeignAnnotationsNoAnnotationInClasspathWithFastClassReadingTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/ForeignAnnotationsNoAnnotationInClasspathWithFastClassReadingTestGenerated.java index 990ab6e3c67..30c0120a18e 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/ForeignAnnotationsNoAnnotationInClasspathWithFastClassReadingTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/ForeignAnnotationsNoAnnotationInClasspathWithFastClassReadingTestGenerated.java @@ -34,6 +34,11 @@ public class ForeignAnnotationsNoAnnotationInClasspathWithFastClassReadingTestGe runTest("compiler/testData/foreignAnnotations/tests/androidRecently.kt"); } + @TestMetadata("androidSdk.kt") + public void testAndroidSdk() throws Exception { + runTest("compiler/testData/foreignAnnotations/tests/androidSdk.kt"); + } + @TestMetadata("android_support.kt") public void testAndroid_support() throws Exception { runTest("compiler/testData/foreignAnnotations/tests/android_support.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/ForeignAnnotationsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/ForeignAnnotationsTestGenerated.java index 68d6a50325c..53e44332389 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/ForeignAnnotationsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/ForeignAnnotationsTestGenerated.java @@ -34,6 +34,11 @@ public class ForeignAnnotationsTestGenerated extends AbstractForeignAnnotationsT runTest("compiler/testData/foreignAnnotations/tests/androidRecently.kt"); } + @TestMetadata("androidSdk.kt") + public void testAndroidSdk() throws Exception { + runTest("compiler/testData/foreignAnnotations/tests/androidSdk.kt"); + } + @TestMetadata("android_support.kt") public void testAndroid_support() throws Exception { runTest("compiler/testData/foreignAnnotations/tests/android_support.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/JavacForeignAnnotationsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/JavacForeignAnnotationsTestGenerated.java index a263120a0db..a45c40b008b 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/JavacForeignAnnotationsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/JavacForeignAnnotationsTestGenerated.java @@ -34,6 +34,11 @@ public class JavacForeignAnnotationsTestGenerated extends AbstractJavacForeignAn runTest("compiler/testData/foreignAnnotations/tests/androidRecently.kt"); } + @TestMetadata("androidSdk.kt") + public void testAndroidSdk() throws Exception { + runTest("compiler/testData/foreignAnnotations/tests/androidSdk.kt"); + } + @TestMetadata("android_support.kt") public void testAndroid_support() throws Exception { runTest("compiler/testData/foreignAnnotations/tests/android_support.kt"); 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 1a7c947566a..191335dfea0 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 @@ -22,6 +22,7 @@ val NULLABLE_ANNOTATIONS = listOf( JvmAnnotationNames.JETBRAINS_NULLABLE_ANNOTATION, FqName("androidx.annotation.Nullable"), FqName("android.support.annotation.Nullable"), + FqName("android.annotation.Nullable"), FqName("com.android.annotations.Nullable"), FqName("org.eclipse.jdt.annotation.Nullable"), FqName("org.checkerframework.checker.nullness.qual.Nullable"), @@ -41,6 +42,7 @@ val NOT_NULL_ANNOTATIONS = listOf( FqName("edu.umd.cs.findbugs.annotations.NonNull"), FqName("androidx.annotation.NonNull"), FqName("android.support.annotation.NonNull"), + FqName("android.annotation.NonNull"), FqName("com.android.annotations.NonNull"), FqName("org.eclipse.jdt.annotation.NonNull"), FqName("org.checkerframework.checker.nullness.qual.NonNull"), diff --git a/third-party/annotations/android/annotation/NonNull.java b/third-party/annotations/android/annotation/NonNull.java new file mode 100644 index 00000000000..42288dab85c --- /dev/null +++ b/third-party/annotations/android/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 android.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/android/annotation/Nullable.java b/third-party/annotations/android/annotation/Nullable.java new file mode 100644 index 00000000000..86873a72f5a --- /dev/null +++ b/third-party/annotations/android/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 android.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 { +}