Ignore built-in type qualifier defaults when Jsr305State=IGNORE

This commit is contained in:
Denis Zharkov
2017-09-14 11:14:38 +03:00
parent b744ed0fd3
commit 2ca220d442
8 changed files with 121 additions and 1 deletions

View File

@@ -0,0 +1,35 @@
// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER
// JSR305_ANNOTATIONS_IGNORE
// FILE: A.java
import javax.annotation.*;
@ParametersAreNonnullByDefault
public class A {
@Nullable public String field = null;
public String foo(String q, @Nonnull String x, @CheckForNull CharSequence y) {
return "";
}
@Nonnull
public String bar() {
return "";
}
}
// FILE: main.kt
fun main(a: A) {
// foo is platform
a.foo("", "", null)?.length
a.foo("", "", null).length
a.foo(null, <!NULL_FOR_NONNULL_TYPE!>null<!>, "").length
a.bar().length
a.bar()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>.length
a.field?.length
a.field<!UNSAFE_CALL!>.<!>length
}

View File

@@ -0,0 +1,13 @@
package
public fun main(/*0*/ a: A): kotlin.Unit
@javax.annotation.ParametersAreNonnullByDefault public open class A {
public constructor A()
@javax.annotation.Nullable public final var field: kotlin.String?
@javax.annotation.Nonnull public open fun bar(): kotlin.String
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open fun foo(/*0*/ q: kotlin.String!, /*1*/ @javax.annotation.Nonnull x: kotlin.String, /*2*/ @javax.annotation.CheckForNull 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

@@ -29,6 +29,7 @@ val TEST_ANNOTATIONS_SOURCE_PATH = "compiler/testData/foreignAnnotations/testAnn
abstract class AbstractForeignAnnotationsTest : AbstractDiagnosticsTest() {
private val WARNING_FOR_JSR305_ANNOTATIONS_DIRECTIVE = "WARNING_FOR_JSR305_ANNOTATIONS"
private val JSR305_ANNOTATIONS_IGNORE_DIRECTIVE = "JSR305_ANNOTATIONS_IGNORE"
override fun getExtraClasspath(): List<File> {
val foreignAnnotations = listOf(MockLibraryUtil.compileJvmLibraryToJar(annotationsPath, "foreign-annotations"))
@@ -54,7 +55,16 @@ abstract class AbstractForeignAnnotationsTest : AbstractDiagnosticsTest() {
InTextDirectivesUtils.isDirectiveDefined(it.expectedText, WARNING_FOR_JSR305_ANNOTATIONS_DIRECTIVE)
}
val jsr305State = if (hasWarningDirective) Jsr305State.WARN else Jsr305State.STRICT
val hasIgnoreDirective = module.any {
InTextDirectivesUtils.isDirectiveDefined(it.expectedText, JSR305_ANNOTATIONS_IGNORE_DIRECTIVE)
}
val jsr305State = when {
hasIgnoreDirective -> Jsr305State.IGNORE
hasWarningDirective -> Jsr305State.WARN
else -> Jsr305State.STRICT
}
return LanguageVersionSettingsImpl(LanguageVersion.LATEST_STABLE, ApiVersion.LATEST_STABLE,
mapOf(AnalysisFlag.jsr305 to jsr305State)
)

View File

@@ -90,6 +90,21 @@ public class ForeignAnnotationsNoAnnotationInClasspathTestGenerated extends Abst
doTest(fileName);
}
@TestMetadata("compiler/testData/foreignAnnotations/tests/jsr305Ignore")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Jsr305Ignore extends AbstractForeignAnnotationsNoAnnotationInClasspathTest {
public void testAllFilesPresentInJsr305Ignore() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/foreignAnnotations/tests/jsr305Ignore"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("parametersAreNonnullByDefault.kt")
public void testParametersAreNonnullByDefault() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/foreignAnnotations/tests/jsr305Ignore/parametersAreNonnullByDefault.kt");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/foreignAnnotations/tests/jsr305NullabilityWarnings")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)

View File

@@ -90,6 +90,21 @@ public class ForeignAnnotationsNoAnnotationInClasspathWithFastClassReadingTestGe
doTest(fileName);
}
@TestMetadata("compiler/testData/foreignAnnotations/tests/jsr305Ignore")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Jsr305Ignore extends AbstractForeignAnnotationsNoAnnotationInClasspathWithFastClassReadingTest {
public void testAllFilesPresentInJsr305Ignore() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/foreignAnnotations/tests/jsr305Ignore"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("parametersAreNonnullByDefault.kt")
public void testParametersAreNonnullByDefault() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/foreignAnnotations/tests/jsr305Ignore/parametersAreNonnullByDefault.kt");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/foreignAnnotations/tests/jsr305NullabilityWarnings")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)

View File

@@ -90,6 +90,21 @@ public class ForeignAnnotationsTestGenerated extends AbstractForeignAnnotationsT
doTest(fileName);
}
@TestMetadata("compiler/testData/foreignAnnotations/tests/jsr305Ignore")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Jsr305Ignore extends AbstractForeignAnnotationsTest {
public void testAllFilesPresentInJsr305Ignore() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/foreignAnnotations/tests/jsr305Ignore"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("parametersAreNonnullByDefault.kt")
public void testParametersAreNonnullByDefault() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/foreignAnnotations/tests/jsr305Ignore/parametersAreNonnullByDefault.kt");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/foreignAnnotations/tests/jsr305NullabilityWarnings")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)

View File

@@ -90,6 +90,21 @@ public class JavacForeignAnnotationsTestGenerated extends AbstractJavacForeignAn
doTest(fileName);
}
@TestMetadata("compiler/testData/foreignAnnotations/tests/jsr305Ignore")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Jsr305Ignore extends AbstractJavacForeignAnnotationsTest {
public void testAllFilesPresentInJsr305Ignore() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/foreignAnnotations/tests/jsr305Ignore"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("parametersAreNonnullByDefault.kt")
public void testParametersAreNonnullByDefault() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/foreignAnnotations/tests/jsr305Ignore/parametersAreNonnullByDefault.kt");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/foreignAnnotations/tests/jsr305NullabilityWarnings")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)

View File

@@ -112,6 +112,8 @@ fun LazyJavaResolverContext.child(
fun LazyJavaResolverContext.computeNewDefaultTypeQualifiers(
additionalAnnotations: Annotations
): JavaTypeQualifiersByElementType? {
if (components.annotationTypeQualifierResolver.jsr305State.isIgnored()) return defaultTypeQualifiers
val nullabilityQualifiersWithApplicability =
additionalAnnotations.mapNotNull(this::extractDefaultNullabilityQualifier)