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
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER
|
||||
// FILE: A.java
|
||||
|
||||
import androidx.annotation.*;
|
||||
|
||||
public class A<T> {
|
||||
@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<String>, a1: A<String?>) {
|
||||
a.foo("", null)?.length
|
||||
<!NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>a.foo("", null)<!>.length
|
||||
<!NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>a.foo(<!NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>null<!>, "")<!>.length
|
||||
|
||||
a.bar().length
|
||||
a.bar()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>.length
|
||||
|
||||
a.field?.length
|
||||
<!NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>a.field<!>.length
|
||||
|
||||
<!NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>a.baz("")<!>.length
|
||||
a.baz("")?.length
|
||||
<!NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>a.baz(<!NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>null<!>)<!>.length
|
||||
|
||||
a1.baz("")!!.length
|
||||
a1.baz(<!NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>null<!>)!!.length
|
||||
|
||||
a.baz2("").length
|
||||
a.baz2("")<!UNNECESSARY_SAFE_CALL!>?.<!>length
|
||||
a.baz2("")<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>.length
|
||||
|
||||
a.baz2(null).length
|
||||
a.baz2(null)<!UNNECESSARY_SAFE_CALL!>?.<!>length
|
||||
a.baz2(null)<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>.length
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
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!>()
|
||||
@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
|
||||
}
|
||||
+5
@@ -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");
|
||||
|
||||
+5
@@ -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");
|
||||
|
||||
+5
@@ -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");
|
||||
|
||||
Generated
+5
@@ -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");
|
||||
|
||||
@@ -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
|
||||
|
||||
+10
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
* <p>
|
||||
* This is a marker annotation and it has no specific attributes.
|
||||
*/
|
||||
@Retention(CLASS)
|
||||
@Target({METHOD, PARAMETER, FIELD})
|
||||
public @interface RecentlyNonNull {
|
||||
}
|
||||
@@ -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.
|
||||
* <p>
|
||||
* 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.
|
||||
* <p>
|
||||
* When decorating a method, this denotes the method might legitimately return
|
||||
* null.
|
||||
* <p>
|
||||
* This is a marker annotation and it has no specific attributes.
|
||||
*/
|
||||
@Retention(CLASS)
|
||||
@Target({METHOD, PARAMETER, FIELD})
|
||||
public @interface RecentlyNullable {
|
||||
}
|
||||
Reference in New Issue
Block a user