Bug 338350: COMPILER_PB_UNAVOIDABLE_GENERIC_TYPE_PROBLEMS wrongly suppresses "Unchecked cast"
- fixed indentation and position of new test
- added example that casts Object to List<String> (not unavoidable)
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericsRegressionTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericsRegressionTest.java
index dd8c568..87c8d60 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericsRegressionTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericsRegressionTest.java
@@ -34,7 +34,7 @@
// All specified tests which does not belong to the class are skipped...
static {
// TESTS_NAMES = new String[] { "testBug408441" };
-// TESTS_NAMES = new String[] { "test1464" };
+ TESTS_NAMES = new String[] { "test338350" };
// TESTS_NUMBERS = new int[] { 1465 };
// TESTS_RANGE = new int[] { 1097, -1 };
}
@@ -1165,43 +1165,6 @@
true,
customOptions);
}
-// https://bugs.eclipse.org/bugs/show_bug.cgi?id=334622 (private access - different packages)
-public void test334622a() {
- this.runNegativeTest(
- new String[] {
- "p/X.java",
- "package p;\n" +
- "public class X {\n" +
- " private Object foo;\n" +
- "}\n",
- "q/Y.java",
- "package q;\n" +
- "import p.X;\n" +
- "public class Y {\n" +
- " public <T extends X> void test(T t) {\n" +
- " System.out.println(t.foo);\n" +
- " }\n" +
- " Zork z;\n" +
- "}\n"
- },
- "----------\n" +
- "1. WARNING in p\\X.java (at line 3)\n" +
- " private Object foo;\n" +
- " ^^^\n" +
- "The value of the field X.foo is not used\n" +
- "----------\n" +
- "----------\n" +
- "1. ERROR in q\\Y.java (at line 5)\n" +
- " System.out.println(t.foo);\n" +
- " ^^^\n" +
- "The field X.foo is not visible\n" +
- "----------\n" +
- "2. ERROR in q\\Y.java (at line 7)\n" +
- " Zork z;\n" +
- " ^^^^\n" +
- "Zork cannot be resolved to a type\n" +
- "----------\n");
-}
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=338350 (unchecked cast - only unavoidable on raw expression)
public void test338350() {
String[] testFiles = new String[] {
@@ -1226,8 +1189,9 @@
" list.toArray((L[]) Array.newInstance(Integer.class, 2));\n" +
" }\n" +
" void bar() {\n" +
- " List<String> l = Bar.getRawList();\n" +
- " ArrayList<String> l2 = (ArrayList<String>) Bar.getRawList();\n" +
+ " List<String> l = (List<String>) Bar.getObject();\n" +
+ " List<String> l2 = Bar.getRawList();\n" +
+ " ArrayList<String> l3 = (ArrayList<String>) Bar.getRawList();\n" +
" }\n" +
"}\n",
"Bar.java",
@@ -1287,12 +1251,17 @@
"Type safety: Unchecked cast from Object to L[]\n" +
"----------\n" +
"8. WARNING in Try.java (at line 21)\n" +
- " List<String> l = Bar.getRawList();\n" +
- " ^^^^^^^^^^^^^^^^\n" +
- "Type safety: The expression of type List needs unchecked conversion to conform to List<String>\n" +
+ " List<String> l = (List<String>) Bar.getObject();\n" +
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
+ "Type safety: Unchecked cast from Object to List<String>\n" +
"----------\n" +
"9. WARNING in Try.java (at line 22)\n" +
- " ArrayList<String> l2 = (ArrayList<String>) Bar.getRawList();\n" +
+ " List<String> l2 = Bar.getRawList();\n" +
+ " ^^^^^^^^^^^^^^^^\n" +
+ "Type safety: The expression of type List needs unchecked conversion to conform to List<String>\n" +
+ "----------\n" +
+ "10. WARNING in Try.java (at line 23)\n" +
+ " ArrayList<String> l3 = (ArrayList<String>) Bar.getRawList();\n" +
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
"Type safety: Unchecked cast from List to ArrayList<String>\n" +
"----------\n" +
@@ -1314,57 +1283,99 @@
this.runNegativeTest(
testFiles,
"----------\n" +
- "1. WARNING in Try.java (at line 6)\n" +
- " takeObj((E) Bar.getObject());\n" +
- " ^^^^^^^^^^^^^^^^^^^\n" +
- "Type safety: Unchecked cast from Object to E\n" +
- "----------\n" +
- "2. WARNING in Try.java (at line 7)\n" +
- " takeObj((E) Bar.getArray());\n" +
- " ^^^^^^^^^^^^^^^^^^\n" +
- "Type safety: Unchecked cast from Object[] to E\n" +
- "----------\n" +
- "3. WARNING in Try.java (at line 8)\n" +
- " takeObj((E) Array.newInstance(Integer.class, 2));\n" +
- " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
- "Type safety: Unchecked cast from Object to E\n" +
- "----------\n" +
- "4. WARNING in Try.java (at line 12)\n" +
- " takeArray((E[]) Bar.getArray());\n" +
- " ^^^^^^^^^^^^^^^^^^^^\n" +
- "Type safety: Unchecked cast from Object[] to E[]\n" +
- "----------\n" +
- "5. WARNING in Try.java (at line 13)\n" +
- " takeArray((E[]) Array.newInstance(Integer.class, 2));\n" +
- " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
- "Type safety: Unchecked cast from Object to E[]\n" +
- "----------\n" +
- "6. WARNING in Try.java (at line 17)\n" +
- " list.toArray((L[]) Bar.getArray());\n" +
- " ^^^^^^^^^^^^^^^^^^^^\n" +
- "Type safety: Unchecked cast from Object[] to L[]\n" +
- "----------\n" +
- "7. WARNING in Try.java (at line 18)\n" +
- " list.toArray((L[]) Array.newInstance(Integer.class, 2));\n" +
- " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
- "Type safety: Unchecked cast from Object to L[]\n" +
- "----------\n" +
- "----------\n" +
- "1. WARNING in Bar.java (at line 11)\n" +
- " public static List getRawList() {\n" +
- " ^^^^\n" +
- "List is a raw type. References to generic type List<E> should be parameterized\n" +
- "----------\n" +
- "2. WARNING in Bar.java (at line 12)\n" +
- " return new ArrayList();\n" +
- " ^^^^^^^^^\n" +
- "ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized\n" +
- "----------\n",
- null,
- true,
- customOptions);
+ "1. WARNING in Try.java (at line 6)\n" +
+ " takeObj((E) Bar.getObject());\n" +
+ " ^^^^^^^^^^^^^^^^^^^\n" +
+ "Type safety: Unchecked cast from Object to E\n" +
+ "----------\n" +
+ "2. WARNING in Try.java (at line 7)\n" +
+ " takeObj((E) Bar.getArray());\n" +
+ " ^^^^^^^^^^^^^^^^^^\n" +
+ "Type safety: Unchecked cast from Object[] to E\n" +
+ "----------\n" +
+ "3. WARNING in Try.java (at line 8)\n" +
+ " takeObj((E) Array.newInstance(Integer.class, 2));\n" +
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
+ "Type safety: Unchecked cast from Object to E\n" +
+ "----------\n" +
+ "4. WARNING in Try.java (at line 12)\n" +
+ " takeArray((E[]) Bar.getArray());\n" +
+ " ^^^^^^^^^^^^^^^^^^^^\n" +
+ "Type safety: Unchecked cast from Object[] to E[]\n" +
+ "----------\n" +
+ "5. WARNING in Try.java (at line 13)\n" +
+ " takeArray((E[]) Array.newInstance(Integer.class, 2));\n" +
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
+ "Type safety: Unchecked cast from Object to E[]\n" +
+ "----------\n" +
+ "6. WARNING in Try.java (at line 17)\n" +
+ " list.toArray((L[]) Bar.getArray());\n" +
+ " ^^^^^^^^^^^^^^^^^^^^\n" +
+ "Type safety: Unchecked cast from Object[] to L[]\n" +
+ "----------\n" +
+ "7. WARNING in Try.java (at line 18)\n" +
+ " list.toArray((L[]) Array.newInstance(Integer.class, 2));\n" +
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
+ "Type safety: Unchecked cast from Object to L[]\n" +
+ "----------\n" +
+ "8. WARNING in Try.java (at line 21)\n" +
+ " List<String> l = (List<String>) Bar.getObject();\n" +
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
+ "Type safety: Unchecked cast from Object to List<String>\n" +
+ "----------\n" +
+ "----------\n" +
+ "1. WARNING in Bar.java (at line 11)\n" +
+ " public static List getRawList() {\n" +
+ " ^^^^\n" +
+ "List is a raw type. References to generic type List<E> should be parameterized\n" +
+ "----------\n" +
+ "2. WARNING in Bar.java (at line 12)\n" +
+ " return new ArrayList();\n" +
+ " ^^^^^^^^^\n" +
+ "ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized\n" +
+ "----------\n",
+ null,
+ true,
+ customOptions);
}
+// https://bugs.eclipse.org/bugs/show_bug.cgi?id=334622 (private access - different packages)
+public void test334622a() {
+ this.runNegativeTest(
+ new String[] {
+ "p/X.java",
+ "package p;\n" +
+ "public class X {\n" +
+ " private Object foo;\n" +
+ "}\n",
+ "q/Y.java",
+ "package q;\n" +
+ "import p.X;\n" +
+ "public class Y {\n" +
+ " public <T extends X> void test(T t) {\n" +
+ " System.out.println(t.foo);\n" +
+ " }\n" +
+ " Zork z;\n" +
+ "}\n"
+ },
+ "----------\n" +
+ "1. WARNING in p\\X.java (at line 3)\n" +
+ " private Object foo;\n" +
+ " ^^^\n" +
+ "The value of the field X.foo is not used\n" +
+ "----------\n" +
+ "----------\n" +
+ "1. ERROR in q\\Y.java (at line 5)\n" +
+ " System.out.println(t.foo);\n" +
+ " ^^^\n" +
+ "The field X.foo is not visible\n" +
+ "----------\n" +
+ "2. ERROR in q\\Y.java (at line 7)\n" +
+ " Zork z;\n" +
+ " ^^^^\n" +
+ "Zork cannot be resolved to a type\n" +
+ "----------\n");
+}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=334622 (private access - same package)
public void test334622b() {
this.runNegativeTest(