From a644dd3ae9e7fd9b03a84e2fedc90611a22e43ec Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Wed, 16 Mar 2016 11:52:29 +0300 Subject: [PATCH] Create tests for TYPE_USE nullability annotations - tests and declarations for checkerframework has been moved, because they only Java 8 targeted - tests for eclipse annotations has been just copied, because there are two jars: one for Java 8 and other for earlier versions --- .../ForeignJava8AnnotationsTestGenerated.java | 49 +++++++++++++++++ .../checker/nullness/qual/NonNull.java | 3 +- .../checker/nullness/qual/Nullable.java | 3 +- .../org/eclipse/jdt/annotation/NonNull.java | 55 +++++++++++++++++++ .../org/eclipse/jdt/annotation/Nullable.java | 46 ++++++++++++++++ .../tests/checkerFramework.kt | 0 .../tests/checkerFramework.txt | 0 .../foreignAnnotationsJava8/tests/eclipse.kt | 42 ++++++++++++++ .../foreignAnnotationsJava8/tests/eclipse.txt | 14 +++++ .../AbstractForeignAnnotationsTest.kt | 5 +- .../AbstractForeignJava8AnnotationsTest.kt | 22 ++++++++ .../ForeignAnnotationsTestGenerated.java | 6 -- .../kotlin/generators/tests/GenerateTests.kt | 3 + 13 files changed, 237 insertions(+), 11 deletions(-) create mode 100644 compiler/java8-tests/tests/org/jetbrains/kotlin/checkers/ForeignJava8AnnotationsTestGenerated.java rename compiler/testData/{foreignAnnotations => foreignAnnotationsJava8}/annotations/org/checkerframework/checker/nullness/qual/NonNull.java (88%) rename compiler/testData/{foreignAnnotations => foreignAnnotationsJava8}/annotations/org/checkerframework/checker/nullness/qual/Nullable.java (82%) create mode 100644 compiler/testData/foreignAnnotationsJava8/annotations/org/eclipse/jdt/annotation/NonNull.java create mode 100644 compiler/testData/foreignAnnotationsJava8/annotations/org/eclipse/jdt/annotation/Nullable.java rename compiler/testData/{foreignAnnotations => foreignAnnotationsJava8}/tests/checkerFramework.kt (100%) rename compiler/testData/{foreignAnnotations => foreignAnnotationsJava8}/tests/checkerFramework.txt (100%) create mode 100644 compiler/testData/foreignAnnotationsJava8/tests/eclipse.kt create mode 100644 compiler/testData/foreignAnnotationsJava8/tests/eclipse.txt create mode 100644 compiler/tests/org/jetbrains/kotlin/checkers/AbstractForeignJava8AnnotationsTest.kt diff --git a/compiler/java8-tests/tests/org/jetbrains/kotlin/checkers/ForeignJava8AnnotationsTestGenerated.java b/compiler/java8-tests/tests/org/jetbrains/kotlin/checkers/ForeignJava8AnnotationsTestGenerated.java new file mode 100644 index 00000000000..3348e7edc36 --- /dev/null +++ b/compiler/java8-tests/tests/org/jetbrains/kotlin/checkers/ForeignJava8AnnotationsTestGenerated.java @@ -0,0 +1,49 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * 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 org.jetbrains.kotlin.checkers; + +import com.intellij.testFramework.TestDataPath; +import org.jetbrains.kotlin.test.JUnit3RunnerWithInners; +import org.jetbrains.kotlin.test.KotlinTestUtils; +import org.jetbrains.kotlin.test.TestMetadata; +import org.junit.runner.RunWith; + +import java.io.File; +import java.util.regex.Pattern; + +/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("compiler/testData/foreignAnnotationsJava8/tests") +@TestDataPath("$PROJECT_ROOT") +@RunWith(JUnit3RunnerWithInners.class) +public class ForeignJava8AnnotationsTestGenerated extends AbstractForeignJava8AnnotationsTest { + public void testAllFilesPresentInTests() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/foreignAnnotationsJava8/tests"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("checkerFramework.kt") + public void testCheckerFramework() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/foreignAnnotationsJava8/tests/checkerFramework.kt"); + doTest(fileName); + } + + @TestMetadata("eclipse.kt") + public void testEclipse() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/foreignAnnotationsJava8/tests/eclipse.kt"); + doTest(fileName); + } +} diff --git a/compiler/testData/foreignAnnotations/annotations/org/checkerframework/checker/nullness/qual/NonNull.java b/compiler/testData/foreignAnnotationsJava8/annotations/org/checkerframework/checker/nullness/qual/NonNull.java similarity index 88% rename from compiler/testData/foreignAnnotations/annotations/org/checkerframework/checker/nullness/qual/NonNull.java rename to compiler/testData/foreignAnnotationsJava8/annotations/org/checkerframework/checker/nullness/qual/NonNull.java index f25979be369..bfc7aeb5fb2 100644 --- a/compiler/testData/foreignAnnotations/annotations/org/checkerframework/checker/nullness/qual/NonNull.java +++ b/compiler/testData/foreignAnnotationsJava8/annotations/org/checkerframework/checker/nullness/qual/NonNull.java @@ -35,7 +35,6 @@ import java.lang.annotation.Target; */ @Documented @Retention(RetentionPolicy.RUNTIME) -// TODO: originally it's target was: TYPE_USE, TYPE_PARAMETER -@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE }) +@Target({ ElementType.TYPE_USE, ElementType.TYPE_PARAMETER }) public @interface NonNull { } \ No newline at end of file diff --git a/compiler/testData/foreignAnnotations/annotations/org/checkerframework/checker/nullness/qual/Nullable.java b/compiler/testData/foreignAnnotationsJava8/annotations/org/checkerframework/checker/nullness/qual/Nullable.java similarity index 82% rename from compiler/testData/foreignAnnotations/annotations/org/checkerframework/checker/nullness/qual/Nullable.java rename to compiler/testData/foreignAnnotationsJava8/annotations/org/checkerframework/checker/nullness/qual/Nullable.java index b60746d8f99..8eab1a11396 100644 --- a/compiler/testData/foreignAnnotations/annotations/org/checkerframework/checker/nullness/qual/Nullable.java +++ b/compiler/testData/foreignAnnotationsJava8/annotations/org/checkerframework/checker/nullness/qual/Nullable.java @@ -22,7 +22,6 @@ import java.lang.annotation.Target; */ @Documented @Retention(RetentionPolicy.RUNTIME) -// TODO: originally it's target was: TYPE_USE, TYPE_PARAMETER -@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE }) +@Target({ ElementType.TYPE_USE, ElementType.TYPE_PARAMETER }) public @interface Nullable { } \ No newline at end of file diff --git a/compiler/testData/foreignAnnotationsJava8/annotations/org/eclipse/jdt/annotation/NonNull.java b/compiler/testData/foreignAnnotationsJava8/annotations/org/eclipse/jdt/annotation/NonNull.java new file mode 100644 index 00000000000..7722bf89c6f --- /dev/null +++ b/compiler/testData/foreignAnnotationsJava8/annotations/org/eclipse/jdt/annotation/NonNull.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (c) 2011, 2013 Stephan Herrmann and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Stephan Herrmann - initial API and implementation + * IBM Corporation - bug fixes + *******************************************************************************/ +package org.eclipse.jdt.annotation; + +import static java.lang.annotation.ElementType.TYPE_USE; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Qualifier for a reference type in a {@link ElementType#TYPE_USE TYPE_USE} position: + * The type that has this annotation is intended to not include the value null. + *

+ * If annotation based null analysis is enabled using this annotation has two consequences: + *

+ *
    + *
  1. Dereferencing an expression of this type is safe, i.e., no NullPointerException can occur at runtime.
  2. + *
  3. An attempt to bind a null value to an entity (field, local variable, method parameter or method return value) + * of this type is a compile time error.
  4. + *
+ * For the second case, diagnostics issued by the compiler should distinguish three situations: + *
    + *
  1. Nullness of the value can be statically determined, the entity is definitely bound from either of: + *
    • the value null, or
    • + *
    • an entity with a {@link Nullable @Nullable} type.
  2. + *
  3. Nullness cannot definitely be determined, because different code branches yield different results.
  4. + *
  5. Nullness cannot be determined, because other program elements are involved for which + * null annotations are lacking.
  6. + *
+ *

+ * Note: Since org.eclipse.jdt.annotation 2.0.0, the + * @Target is {TYPE_USE}. For the old API, see + * + * @NonNull in 1.1.0. + *

+ * @since 1.0 + */ +@Documented +@Retention(RetentionPolicy.CLASS) +@Target({ TYPE_USE }) +public @interface NonNull { + // marker annotation with no members +} \ No newline at end of file diff --git a/compiler/testData/foreignAnnotationsJava8/annotations/org/eclipse/jdt/annotation/Nullable.java b/compiler/testData/foreignAnnotationsJava8/annotations/org/eclipse/jdt/annotation/Nullable.java new file mode 100644 index 00000000000..70e479f349e --- /dev/null +++ b/compiler/testData/foreignAnnotationsJava8/annotations/org/eclipse/jdt/annotation/Nullable.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (c) 2011, 2013 Stephan Herrmann and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Stephan Herrmann - initial API and implementation + * IBM Corporation - bug fixes + *******************************************************************************/ +package org.eclipse.jdt.annotation; + +import static java.lang.annotation.ElementType.TYPE_USE; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Qualifier for a reference type in a {@link ElementType#TYPE_USE TYPE_USE} position: + * The type that has this annotation explicitly includes the value null. + *

+ * If annotation based null analysis is enabled using this annotation has two consequences: + *

+ *
    + *
  1. Binding a null value to an entity (field, local variable, method parameter or method return value) + * of this type is legal.
  2. + *
  3. Dereferencing an expression of this type is unsafe, i.e., a NullPointerException can occur at runtime.
  4. + *
+ *

+ * Note: Since org.eclipse.jdt.annotation 2.0.0, the + * @Target is {TYPE_USE}. For the old API, see + * + * @Nullable in 1.1.0. + *

+ * @since 1.0 + */ +@Documented +@Retention(RetentionPolicy.CLASS) +@Target({ TYPE_USE }) +public @interface Nullable { + // marker annotation with no members +} \ No newline at end of file diff --git a/compiler/testData/foreignAnnotations/tests/checkerFramework.kt b/compiler/testData/foreignAnnotationsJava8/tests/checkerFramework.kt similarity index 100% rename from compiler/testData/foreignAnnotations/tests/checkerFramework.kt rename to compiler/testData/foreignAnnotationsJava8/tests/checkerFramework.kt diff --git a/compiler/testData/foreignAnnotations/tests/checkerFramework.txt b/compiler/testData/foreignAnnotationsJava8/tests/checkerFramework.txt similarity index 100% rename from compiler/testData/foreignAnnotations/tests/checkerFramework.txt rename to compiler/testData/foreignAnnotationsJava8/tests/checkerFramework.txt diff --git a/compiler/testData/foreignAnnotationsJava8/tests/eclipse.kt b/compiler/testData/foreignAnnotationsJava8/tests/eclipse.kt new file mode 100644 index 00000000000..86c6c7bae33 --- /dev/null +++ b/compiler/testData/foreignAnnotationsJava8/tests/eclipse.kt @@ -0,0 +1,42 @@ +// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER +// FILE: A.java + +import org.eclipse.jdt.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/foreignAnnotationsJava8/tests/eclipse.txt b/compiler/testData/foreignAnnotationsJava8/tests/eclipse.txt new file mode 100644 index 00000000000..99fe9e5cd5a --- /dev/null +++ b/compiler/testData/foreignAnnotationsJava8/tests/eclipse.txt @@ -0,0 +1,14 @@ +package + +public fun main(/*0*/ a: A, /*1*/ a1: A): kotlin.Unit + +public open class A { + public constructor A() + @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 +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/AbstractForeignAnnotationsTest.kt b/compiler/tests/org/jetbrains/kotlin/checkers/AbstractForeignAnnotationsTest.kt index 941fa2a5832..46050fb6890 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/AbstractForeignAnnotationsTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/checkers/AbstractForeignAnnotationsTest.kt @@ -22,8 +22,11 @@ import java.io.File abstract class AbstractForeignAnnotationsTest : AbstractDiagnosticsTest() { override fun getExtraClasspath(): List { return listOf(MockLibraryUtil.compileLibraryToJar( - "compiler/testData/foreignAnnotations/annotations", + annotationsPath, "foreign-annotations", /* addSources = */false, /* allowKotlinPackage = */ false )) } + + open protected val annotationsPath: String + get() = "compiler/testData/foreignAnnotations/annotations" } diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/AbstractForeignJava8AnnotationsTest.kt b/compiler/tests/org/jetbrains/kotlin/checkers/AbstractForeignJava8AnnotationsTest.kt new file mode 100644 index 00000000000..5cd516a0f39 --- /dev/null +++ b/compiler/tests/org/jetbrains/kotlin/checkers/AbstractForeignJava8AnnotationsTest.kt @@ -0,0 +1,22 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * 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 org.jetbrains.kotlin.checkers + +abstract class AbstractForeignJava8AnnotationsTest : AbstractForeignAnnotationsTest() { + override val annotationsPath: String + get() = "compiler/testData/foreignAnnotationsJava8/annotations" +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/ForeignAnnotationsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/ForeignAnnotationsTestGenerated.java index a1f7b046101..7f7fb9cd53e 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/ForeignAnnotationsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/ForeignAnnotationsTestGenerated.java @@ -47,12 +47,6 @@ public class ForeignAnnotationsTestGenerated extends AbstractForeignAnnotationsT doTest(fileName); } - @TestMetadata("checkerFramework.kt") - public void testCheckerFramework() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/foreignAnnotations/tests/checkerFramework.kt"); - doTest(fileName); - } - @TestMetadata("eclipse.kt") public void testEclipse() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/foreignAnnotations/tests/eclipse.kt"); diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index 4e950891dcc..9a8cd5f5c7d 100644 --- a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -343,6 +343,9 @@ fun main(args: Array) { testClass("DiagnosticsWithJava8TestGenerated") { model("diagnostics/testsWithJava8") } + testClass() { + model("foreignAnnotationsJava8/tests") + } }