From 9de174959e67f8fc0ed9e3e95de9d9eaad9111ae Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Mon, 14 May 2018 17:15:40 +0300 Subject: [PATCH] Support androidx under-migration-like nullability annotations They are hardcoded to avoid having dependency from android.jar on our annotations' jar with UnderMigration. Even while it could be a compile-only dependency we need to make sure that annotated types are read properly without RecentlyNonNull/RecentlyNullable in the classpath #KT-24278 Fixed --- .../tests/androidRecently.kt | 54 +++++++++++++++++++ .../tests/androidRecently.txt | 15 ++++++ ...sNoAnnotationInClasspathTestGenerated.java | 5 ++ ...pathWithFastClassReadingTestGenerated.java | 5 ++ .../ForeignAnnotationsTestGenerated.java | 5 ++ .../JavacForeignAnnotationsTestGenerated.java | 5 ++ .../kotlin/load/java/JvmAnnotationNames.kt | 3 ++ .../typeEnhancement/signatureEnhancement.kt | 10 ++++ .../androidx/annotation/RecentlyNonNull.java | 34 ++++++++++++ .../androidx/annotation/RecentlyNullable.java | 41 ++++++++++++++ 10 files changed, 177 insertions(+) create mode 100644 compiler/testData/foreignAnnotations/tests/androidRecently.kt create mode 100644 compiler/testData/foreignAnnotations/tests/androidRecently.txt create mode 100644 third-party/annotations/androidx/annotation/RecentlyNonNull.java create mode 100644 third-party/annotations/androidx/annotation/RecentlyNullable.java diff --git a/compiler/testData/foreignAnnotations/tests/androidRecently.kt b/compiler/testData/foreignAnnotations/tests/androidRecently.kt new file mode 100644 index 00000000000..5a2e276604d --- /dev/null +++ b/compiler/testData/foreignAnnotations/tests/androidRecently.kt @@ -0,0 +1,54 @@ +// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER +// FILE: A.java + +import androidx.annotation.*; + +public class A { + @RecentlyNullable public String field = null; + + @RecentlyNullable + public String foo(@RecentlyNonNull String x, @RecentlyNullable CharSequence y) { + return ""; + } + + @RecentlyNonNull + public String bar() { + return ""; + } + + @RecentlyNullable + public T baz(@RecentlyNonNull T x) { return x; } + + @RecentlyNonNull + public T baz2(@RecentlyNullable 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 + + a.baz2("").length + a.baz2("")?.length + a.baz2("")!!.length + + a.baz2(null).length + a.baz2(null)?.length + a.baz2(null)!!.length +} + diff --git a/compiler/testData/foreignAnnotations/tests/androidRecently.txt b/compiler/testData/foreignAnnotations/tests/androidRecently.txt new file mode 100644 index 00000000000..aea09e634f2 --- /dev/null +++ b/compiler/testData/foreignAnnotations/tests/androidRecently.txt @@ -0,0 +1,15 @@ +package + +public fun main(/*0*/ a: A, /*1*/ a1: A): kotlin.Unit + +public open class A { + public constructor A() + @androidx.annotation.RecentlyNullable public final var field: kotlin.String! + @androidx.annotation.RecentlyNonNull public open fun bar(): kotlin.String! + @androidx.annotation.RecentlyNullable public open fun baz(/*0*/ @androidx.annotation.RecentlyNonNull x: T!): T! + @androidx.annotation.RecentlyNonNull public open fun baz2(/*0*/ @androidx.annotation.RecentlyNullable x: T!): T! + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + @androidx.annotation.RecentlyNullable public open fun foo(/*0*/ @androidx.annotation.RecentlyNonNull x: kotlin.String!, /*1*/ @androidx.annotation.RecentlyNullable 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 7453058abab..61df0e020c7 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/ForeignAnnotationsNoAnnotationInClasspathTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/ForeignAnnotationsNoAnnotationInClasspathTestGenerated.java @@ -29,6 +29,11 @@ public class ForeignAnnotationsNoAnnotationInClasspathTestGenerated extends Abst KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/foreignAnnotations/tests"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); } + @TestMetadata("androidRecently.kt") + public void testAndroidRecently() throws Exception { + runTest("compiler/testData/foreignAnnotations/tests/androidRecently.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 f772d39b59e..8dcd816b943 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/ForeignAnnotationsNoAnnotationInClasspathWithFastClassReadingTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/ForeignAnnotationsNoAnnotationInClasspathWithFastClassReadingTestGenerated.java @@ -29,6 +29,11 @@ public class ForeignAnnotationsNoAnnotationInClasspathWithFastClassReadingTestGe KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/foreignAnnotations/tests"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); } + @TestMetadata("androidRecently.kt") + public void testAndroidRecently() throws Exception { + runTest("compiler/testData/foreignAnnotations/tests/androidRecently.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 71b89560089..5a5b75c2d26 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/ForeignAnnotationsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/ForeignAnnotationsTestGenerated.java @@ -29,6 +29,11 @@ public class ForeignAnnotationsTestGenerated extends AbstractForeignAnnotationsT KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/foreignAnnotations/tests"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); } + @TestMetadata("androidRecently.kt") + public void testAndroidRecently() throws Exception { + runTest("compiler/testData/foreignAnnotations/tests/androidRecently.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 c15c3085f1b..3bfb54b16fc 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/JavacForeignAnnotationsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/JavacForeignAnnotationsTestGenerated.java @@ -29,6 +29,11 @@ public class JavacForeignAnnotationsTestGenerated extends AbstractJavacForeignAn KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/foreignAnnotations/tests"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); } + @TestMetadata("androidRecently.kt") + public void testAndroidRecently() throws Exception { + runTest("compiler/testData/foreignAnnotations/tests/androidRecently.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 f237a3b5ec5..fc721142cc5 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 @@ -53,6 +53,9 @@ val NULLABILITY_ANNOTATIONS = NULLABLE_ANNOTATIONS + JAVAX_NONNULL_ANNOTATION + val COMPATQUAL_NULLABLE_ANNOTATION = FqName("org.checkerframework.checker.nullness.compatqual.NullableDecl") val COMPATQUAL_NONNULL_ANNOTATION = FqName("org.checkerframework.checker.nullness.compatqual.NonNullDecl") +val ANDROIDX_RECENTLY_NULLABLE_ANNOTATION = FqName("androidx.annotation.RecentlyNullable") +val ANDROIDX_RECENTLY_NON_NULL_ANNOTATION = FqName("androidx.annotation.RecentlyNonNull") + val READ_ONLY_ANNOTATIONS = listOf( JvmAnnotationNames.JETBRAINS_READONLY_ANNOTATION, JvmAnnotationNames.READONLY_ANNOTATION diff --git a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/signatureEnhancement.kt b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/signatureEnhancement.kt index c7200f7a715..893716d2669 100644 --- a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/signatureEnhancement.kt +++ b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/signatureEnhancement.kt @@ -90,6 +90,16 @@ class SignatureEnhancement( annotationFqName == COMPATQUAL_NONNULL_ANNOTATION && jsr305State.enableCompatqualCheckerFrameworkAnnotations -> NullabilityQualifierWithMigrationStatus(NullabilityQualifier.NOT_NULL) + + annotationFqName == ANDROIDX_RECENTLY_NON_NULL_ANNOTATION -> NullabilityQualifierWithMigrationStatus( + NullabilityQualifier.NOT_NULL, + isForWarningOnly = true + ) + + annotationFqName == ANDROIDX_RECENTLY_NULLABLE_ANNOTATION -> NullabilityQualifierWithMigrationStatus( + NullabilityQualifier.NULLABLE, + isForWarningOnly = true + ) else -> null } } diff --git a/third-party/annotations/androidx/annotation/RecentlyNonNull.java b/third-party/annotations/androidx/annotation/RecentlyNonNull.java new file mode 100644 index 00000000000..9811c182b8b --- /dev/null +++ b/third-party/annotations/androidx/annotation/RecentlyNonNull.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 RecentlyNonNull { +} diff --git a/third-party/annotations/androidx/annotation/RecentlyNullable.java b/third-party/annotations/androidx/annotation/RecentlyNullable.java new file mode 100644 index 00000000000..bf1cee1ecdb --- /dev/null +++ b/third-party/annotations/androidx/annotation/RecentlyNullable.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 RecentlyNullable { +}