Support configuring of nullability annotations with their report levels through a test directive

This commit is contained in:
Victor Petukhov
2021-06-02 17:21:42 +03:00
parent 61c2f1b203
commit 6d3badb2cd
8 changed files with 237 additions and 2 deletions

View File

@@ -180,4 +180,11 @@ class JavaTypeEnhancementStateParser(private val collector: MessageCollector) {
return FqName(name) to state
}
companion object {
private val DEFAULT = JavaTypeEnhancementStateParser(MessageCollector.NONE)
fun parsePlainNullabilityAnnotationReportLevels(nullabilityAnnotations: String) =
DEFAULT.parseNullabilityAnnotationReportLevels(arrayOf(nullabilityAnnotations)).entries.singleOrNull()?.toPair()
}
}

View File

@@ -0,0 +1,158 @@
// NULLABILITY_ANNOTATIONS: @io.reactivex.annotations:strict, @org.eclipse.jdt.annotation:warn, @androidx.annotation:strict, @com.android.annotations:ignore
// FILE: A1.java
import io.reactivex.annotations.*;
public class A1<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: A2.java
import org.eclipse.jdt.annotation.*;
public class A2<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: A3.java
import androidx.annotation.*;
public class A3<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: A4.java
import com.android.annotations.*;
public class A4<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 main1(a: A1<String>, a1: A1<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
}
fun main2(a: A2<String>, a1: A2<String?>) {
a.foo("", null)?.length
<!RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>a.foo("", null)<!>.length
<!RECEIVER_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
<!RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>a.field<!>.length
<!RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>a.baz("")<!>.length
a.baz("")?.length
<!RECEIVER_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
}
fun main3(a: A3<String>, a1: A3<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
}
fun main4(a: A4<String>, a1: A4<String?>) {
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
}

View File

@@ -0,0 +1,39 @@
package
public fun main1(/*0*/ a: A1<kotlin.String>, /*1*/ a1: A1<kotlin.String?>): kotlin.Unit
public fun main2(/*0*/ a: A2<kotlin.String>, /*1*/ a1: A2<kotlin.String?>): kotlin.Unit
public fun main3(/*0*/ a: A3<kotlin.String>, /*1*/ a1: A3<kotlin.String?>): kotlin.Unit
public fun main4(/*0*/ a: [ERROR : A4<String>]<kotlin.String>, /*1*/ a1: [ERROR : A4<String?>]<kotlin.String?>): kotlin.Unit
public open class A1</*0*/ T : kotlin.Any!> {
public constructor A1</*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
}
public open class A2</*0*/ T : kotlin.Any!> {
public constructor A2</*0*/ T : kotlin.Any!>()
@org.eclipse.jdt.annotation.Nullable public final var field: kotlin.String!
@org.eclipse.jdt.annotation.NonNull public open fun bar(): kotlin.String!
@org.eclipse.jdt.annotation.Nullable public open fun baz(/*0*/ @org.eclipse.jdt.annotation.NonNull x: T!): T!
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
@org.eclipse.jdt.annotation.Nullable public open fun foo(/*0*/ @org.eclipse.jdt.annotation.NonNull x: kotlin.String!, /*1*/ @org.eclipse.jdt.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
}
public open class A3</*0*/ T : kotlin.Any!> {
public constructor A3</*0*/ T : kotlin.Any!>()
@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
}

View File

@@ -86,6 +86,12 @@ public class ForeignAnnotationsCompiledJavaTestGenerated extends AbstractForeign
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/lombokSimple.kt");
}
@Test
@TestMetadata("multiple.kt")
public void testMultiple() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/multiple.kt");
}
@Test
@TestMetadata("rxjava.kt")
public void testRxjava() throws Exception {

View File

@@ -86,6 +86,12 @@ public class ForeignAnnotationsCompiledJavaWithPsiClassReadingTestGenerated exte
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/lombokSimple.kt");
}
@Test
@TestMetadata("multiple.kt")
public void testMultiple() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/multiple.kt");
}
@Test
@TestMetadata("rxjava.kt")
public void testRxjava() throws Exception {

View File

@@ -86,6 +86,12 @@ public class ForeignAnnotationsSourceJavaTestGenerated extends AbstractForeignAn
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/lombokSimple.kt");
}
@Test
@TestMetadata("multiple.kt")
public void testMultiple() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/multiple.kt");
}
@Test
@TestMetadata("rxjava.kt")
public void testRxjava() throws Exception {

View File

@@ -5,9 +5,11 @@
package org.jetbrains.kotlin.test.directives
import org.jetbrains.kotlin.cli.common.arguments.JavaTypeEnhancementStateParser
import org.jetbrains.kotlin.test.directives.model.SimpleDirectivesContainer
import org.jetbrains.kotlin.test.services.configuration.JavaForeignAnnotationType
import org.jetbrains.kotlin.load.java.ReportLevel
import org.jetbrains.kotlin.name.FqName
@Suppress("RemoveExplicitTypeArguments")
object ForeignAnnotationsDirectives : SimpleDirectivesContainer() {
@@ -30,6 +32,11 @@ object ForeignAnnotationsDirectives : SimpleDirectivesContainer() {
additionalParser = ReportLevel.Companion::findByDescription
)
val NULLABILITY_ANNOTATIONS by valueDirective<Pair<FqName, ReportLevel>>(
description = "List of annotations with their report levels",
parser = JavaTypeEnhancementStateParser.Companion::parsePlainNullabilityAnnotationReportLevels
)
val ANNOTATIONS_PATH by enumDirective<JavaForeignAnnotationType>(
description = "Path to foreign annotations"
)

View File

@@ -45,6 +45,7 @@ open class JvmForeignAnnotationsConfigurator(testServices: TestServices) : Envir
override val directivesContainers: List<DirectivesContainer>
get() = listOf(ForeignAnnotationsDirectives)
@OptIn(ExperimentalStdlibApi::class)
override fun provideAdditionalAnalysisFlags(directives: RegisteredDirectives): Map<AnalysisFlag<*>, Any?> {
val defaultJsr305Settings = Jsr305Settings.DEFAULT
val globalState = directives.singleOrZeroValue(JSR305_GLOBAL_REPORT) ?: defaultJsr305Settings.globalLevel
@@ -54,8 +55,13 @@ open class JvmForeignAnnotationsConfigurator(testServices: TestServices) : Envir
val state = ReportLevel.findByDescription(stateDescription) ?: return@mapNotNull null
FqName(name) to state
}.toMap()
val configuredReportLevels =
directives.singleOrZeroValue(JSPECIFY_STATE)?.let { mapOf(JSPECIFY_ANNOTATIONS_PACKAGE to it) } ?: emptyMap()
val configuredReportLevels = buildMap<FqName, ReportLevel> {
directives.singleOrZeroValue(JSPECIFY_STATE)?.let { put(JSPECIFY_ANNOTATIONS_PACKAGE, it) }
for ((fqname, reportLevel) in directives[ForeignAnnotationsDirectives.NULLABILITY_ANNOTATIONS]) {
put(fqname, reportLevel)
}
}
return mapOf(
JvmAnalysisFlags.javaTypeEnhancementState to JavaTypeEnhancementState(
Jsr305Settings(globalState, migrationState, userAnnotationsState),