Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJay Arthanareeswaran2020-04-22 10:14:04 +0000
committerJay Arthanareeswaran2020-04-22 11:51:32 +0000
commit7bd737f958efefb05ab8cc567eff85d4f5efca69 (patch)
tree6eb0573bebf522d55bfaef9340900ba9828d1052 /org.eclipse.jdt.core.tests.compiler
parent5d9cc1986eb8f3d5aef5656ecc74d5ec0c392fc1 (diff)
downloadeclipse.jdt.core-7bd737f958efefb05ab8cc567eff85d4f5efca69.tar.gz
eclipse.jdt.core-7bd737f958efefb05ab8cc567eff85d4f5efca69.tar.xz
eclipse.jdt.core-7bd737f958efefb05ab8cc567eff85d4f5efca69.zip
Bug 562392 - [14] Type parameters should be allowed in instanceof
expressions from Java 14 Change-Id: I2b1d720fd174d06520d7425a6e94acb2199b145d Signed-off-by: Jay Arthanareeswaran <jarthana@in.ibm.com>
Diffstat (limited to 'org.eclipse.jdt.core.tests.compiler')
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericTypeTest.java16
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullTypeAnnotationTest.java117
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PatternMatching14Test.java24
3 files changed, 118 insertions, 39 deletions
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericTypeTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericTypeTest.java
index 94add4c42a..a97c29f664 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericTypeTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericTypeTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2019 IBM Corporation and others.
+ * Copyright (c) 2000, 2020 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -5358,6 +5358,8 @@ public class GenericTypeTest extends AbstractComparableTest {
}
// reject instanceof type variable or parameterized type
public void test0178() {
+ if (this.complianceLevel >= ClassFileConstants.JDK14)
+ return;
Map customOptions = getCompilerOptions();
this.runNegativeTest(
new String[] {
@@ -26368,6 +26370,8 @@ public void test0813() {
}
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=104695
public void test0814() {
+ if (this.complianceLevel >= ClassFileConstants.JDK14)
+ return;
this.runNegativeTest(
new String[] {
"X.java",
@@ -26449,6 +26453,8 @@ public void test0815() {
}
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=104695 - variation
public void test0816() {
+ if (this.complianceLevel >= ClassFileConstants.JDK14)
+ return;
this.runNegativeTest(
new String[] {
"X.java",
@@ -26475,6 +26481,8 @@ public void test0816() {
}
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=104695 - variation
public void test0817() {
+ if (this.complianceLevel >= ClassFileConstants.JDK14)
+ return;
this.runNegativeTest(
new String[] {
"X.java",
@@ -31918,6 +31926,8 @@ public void test0954() {
}
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=105049
public void test0955() {
+ if (this.complianceLevel >= ClassFileConstants.JDK14)
+ return;
this.runNegativeTest(
new String[] {
"X.java", //================================
@@ -50124,6 +50134,8 @@ public void test1425() {
}
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=258039
public void test1426() {
+ if (this.complianceLevel >= ClassFileConstants.JDK14)
+ return;
this.runNegativeTest(
new String[] {
"X.java", //-----------------------------------------------------------------------
@@ -51685,6 +51697,8 @@ public void test268798a() {
}
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=307885
public void test1460() {
+ if (this.complianceLevel >= ClassFileConstants.JDK14)
+ return;
this.runNegativeTest(
new String[] {
"Test.java",
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullTypeAnnotationTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullTypeAnnotationTest.java
index 22722bf014..11e7d01c02 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullTypeAnnotationTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullTypeAnnotationTest.java
@@ -2270,31 +2270,8 @@ public class NullTypeAnnotationTest extends AbstractNullAnnotationTest {
// illegal / unchecked for cast & instanceof with complex type
public void testUnsupportedLocation04() {
- runNegativeTest(
- new String[] {
- "p/X.java",
- "package p;\n" +
- "import org.eclipse.jdt.annotation.*;\n" +
- "import java.util.*;\n" +
- "public class X {\n" +
- " List<@NonNull X> parameterized(List<X> arg) {\n" +
- " if (!(arg instanceof List<@NonNull X>))\n" +
- " return (java.util.List<@NonNull X>)arg;\n" +
- " return arg;\n" +
- " }\n" +
- " X @NonNull[] arrays(X[] arg) {\n" +
- " if (!(arg instanceof X @NonNull[]))\n" +
- " return (p.X @NonNull[])arg;\n" +
- " return arg;\n" +
- " }\n" +
- " ArrayList<@NonNull String> foo(List<@NonNull String> l) {\n" +
- " return (ArrayList<@NonNull String>) l;\n" + // OK
- " }" +
- " ArrayList<@NonNull String> foo2(List<@NonNull String> l) {\n" +
- " return (ArrayList<String>) l;\n" + // warn, TODO(stephan) with flow analysis (bug 415292) we might recover the original @NonNull...
- " }" +
- "}\n"
- },
+ String expectedProblemLog =
+ (this.complianceLevel < ClassFileConstants.JDK14) ?
"----------\n" +
"1. ERROR in p\\X.java (at line 6)\n" +
" if (!(arg instanceof List<@NonNull X>))\n" +
@@ -2330,7 +2307,64 @@ public class NullTypeAnnotationTest extends AbstractNullAnnotationTest {
" return (ArrayList<String>) l;\n" +
" ^^^^^^^^^^^^^^^^^^^^^\n" +
"Null type safety (type annotations): The expression of type \'ArrayList<String>\' needs unchecked conversion to conform to \'ArrayList<@NonNull String>\'\n" +
- "----------\n",
+ "----------\n" :
+ "----------\n" +
+ "1. ERROR in p\\X.java (at line 6)\n" +
+ " if (!(arg instanceof List<@NonNull X>))\n" +
+ " ^^^^^^^^^^^^^^^^\n" +
+ "Nullness annotations are not applicable at this location \n" +
+ "----------\n" +
+ "2. WARNING in p\\X.java (at line 7)\n" +
+ " return (java.util.List<@NonNull X>)arg;\n" +
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
+ "Null type safety: Unchecked cast from List<X> to List<@NonNull X>\n" +
+ "----------\n" +
+ "3. WARNING in p\\X.java (at line 11)\n" +
+ " if (!(arg instanceof X @NonNull[]))\n" +
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
+ "The expression of type X[] is already an instance of type X[]\n" +
+ "----------\n" +
+ "4. ERROR in p\\X.java (at line 11)\n" +
+ " if (!(arg instanceof X @NonNull[]))\n" +
+ " ^^^^^^^^^^^^\n" +
+ "Nullness annotations are not applicable at this location \n" +
+ "----------\n" +
+ "5. WARNING in p\\X.java (at line 12)\n" +
+ " return (p.X @NonNull[])arg;\n" +
+ " ^^^^^^^^^^^^^^^^^^^\n" +
+ "Null type safety: Unchecked cast from X[] to X @NonNull[]\n" +
+ "----------\n" +
+ "6. WARNING in p\\X.java (at line 18)\n" +
+ " return (ArrayList<String>) l;\n" +
+ " ^^^^^^^^^^^^^^^^^^^^^\n" +
+ "Null type safety (type annotations): The expression of type \'ArrayList<String>\' needs unchecked conversion to conform to \'ArrayList<@NonNull String>\'\n" +
+ "----------\n";
+ runNegativeTest(
+ new String[] {
+ "p/X.java",
+ "package p;\n" +
+ "import org.eclipse.jdt.annotation.*;\n" +
+ "import java.util.*;\n" +
+ "public class X {\n" +
+ " List<@NonNull X> parameterized(List<X> arg) {\n" +
+ " if (!(arg instanceof List<@NonNull X>))\n" +
+ " return (java.util.List<@NonNull X>)arg;\n" +
+ " return arg;\n" +
+ " }\n" +
+ " X @NonNull[] arrays(X[] arg) {\n" +
+ " if (!(arg instanceof X @NonNull[]))\n" +
+ " return (p.X @NonNull[])arg;\n" +
+ " return arg;\n" +
+ " }\n" +
+ " ArrayList<@NonNull String> foo(List<@NonNull String> l) {\n" +
+ " return (ArrayList<@NonNull String>) l;\n" + // OK
+ " }" +
+ " ArrayList<@NonNull String> foo2(List<@NonNull String> l) {\n" +
+ " return (ArrayList<String>) l;\n" + // warn, TODO(stephan) with flow analysis (bug 415292) we might recover the original @NonNull...
+ " }" +
+ "}\n"
+ },
+ expectedProblemLog,
this.LIBS,
true/*flush*/);
}
@@ -8408,6 +8442,25 @@ public void testBug466713c() {
}
// variant for https://bugs.eclipse.org/bugs/show_bug.cgi?id=466713#c5
public void testBug466713d() {
+ String expectedProblemLog =
+ (this.complianceLevel < ClassFileConstants.JDK14) ?
+ "----------\n" +
+ "1. ERROR in Bug.java (at line 3)\n" +
+ " return o instanceof java.util.Iterator<java.lang. @MyAnnot @org.eclipse.jdt.annotation.Nullable String>;\n" +
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
+ "Cannot perform instanceof check against parameterized type Iterator<String>. Use the form Iterator<?> instead since further generic type information will be erased at runtime\n" +
+ "----------\n" +
+ "2. ERROR in Bug.java (at line 3)\n" +
+ " return o instanceof java.util.Iterator<java.lang. @MyAnnot @org.eclipse.jdt.annotation.Nullable String>;\n" +
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
+ "Nullness annotations are not applicable at this location \n" +
+ "----------\n" :
+ "----------\n" +
+ "1. ERROR in Bug.java (at line 3)\n" +
+ " return o instanceof java.util.Iterator<java.lang. @MyAnnot @org.eclipse.jdt.annotation.Nullable String>;\n" +
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
+ "Nullness annotations are not applicable at this location \n" +
+ "----------\n";
runNegativeTest(
new String[] {
"MyAnnot.java",
@@ -8422,17 +8475,7 @@ public void testBug466713d() {
" }\n" +
"}\n"
},
- "----------\n" +
- "1. ERROR in Bug.java (at line 3)\n" +
- " return o instanceof java.util.Iterator<java.lang. @MyAnnot @org.eclipse.jdt.annotation.Nullable String>;\n" +
- " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
- "Cannot perform instanceof check against parameterized type Iterator<String>. Use the form Iterator<?> instead since further generic type information will be erased at runtime\n" +
- "----------\n" +
- "2. ERROR in Bug.java (at line 3)\n" +
- " return o instanceof java.util.Iterator<java.lang. @MyAnnot @org.eclipse.jdt.annotation.Nullable String>;\n" +
- " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
- "Nullness annotations are not applicable at this location \n" +
- "----------\n",
+ expectedProblemLog,
this.LIBS,
true/*flush*/);
}
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PatternMatching14Test.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PatternMatching14Test.java
index 0bb17edf0a..95faee0fd8 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PatternMatching14Test.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PatternMatching14Test.java
@@ -2045,4 +2045,26 @@ public class PatternMatching14Test extends AbstractRegressionTest {
compilerOptions);
compilerOptions.put(CompilerOptions.OPTION_PreserveUnusedLocal, old);
}
-} \ No newline at end of file
+ public void testBug562392() {
+ Map<String, String> compilerOptions = getCompilerOptions(true);
+ runConformTest(
+ new String[] {
+ "X.java",
+ "public class X<T> {\n" +
+ " public boolean foo(T obj) {\n" +
+ " if (obj instanceof T s) {\n" +
+ " System.out.println(s);\n" +
+ " }\n" +
+ " return true;\n" +
+ " }\n" +
+ " public static void main(String argv[]) {\n" +
+ " String s = \"x\";\n" +
+ " System.out.println(new X<String>().foo(s));\n" +
+ " }\n" +
+ "}\n",
+ },
+ "x\n" +
+ "true",
+ compilerOptions);
+ }
+}

Back to the top