Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 338350

Summary: COMPILER_PB_UNAVOIDABLE_GENERIC_TYPE_PROBLEMS wrongly suppresses "Unchecked cast"
Product: [Eclipse Project] JDT Reporter: Markus Keller <markus.kell.r>
Component: CoreAssignee: Markus Keller <markus.kell.r>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: amj87.iitr, anchakrk, daniel_megert, jarthana, john.arthorne, manoj.palat, markus.kell.r, srikanth_sankaran, stephan.herrmann
Version: 3.7   
Target Milestone: 4.4 M1   
Hardware: PC   
OS: Windows 7   
Whiteboard:

Description Markus Keller CLA 2011-02-27 14:28:56 EST
COMPILER_PB_UNAVOIDABLE_GENERIC_TYPE_PROBLEMS wrongly suppresses "Unchecked cast" problems:

package xy;
import java.lang.reflect.Array;
import java.util.List;
public class Try<E> {

    void fooObj() {
        takeObj((E) Bar.getObject());
        takeObj((E) Bar.getArray());
        takeObj((E) Array.newInstance(Integer.class, 2));
    }

    void takeObj(E obj) {
    }

    void fooArray() {
        takeArray((E[]) Bar.getArray());
        takeArray((E[]) Array.newInstance(Integer.class, 2));
    }

    void takeArray(E[] array) {
    }

    <L> void foo(List<L> list) {
        list.toArray((L[]) Bar.getArray());
        list.toArray((L[]) Array.newInstance(Integer.class, 2));
    }
}

package xy;
import java.lang.reflect.Array;
public class Bar {
    
    public static Object getObject() {
        return new Object();
    }

    public static Object[] getArray() {
        return (Object[]) Array.newInstance(Integer.class, 2);
    }
}

All casts to E[] and E generate "Type safety: Unchecked cast ..." problems, and all these problems should stay even if unavoidable problems are suppressed.
These problems cannot be solved by generifying anything.
Comment 1 Srikanth Sankaran CLA 2011-03-31 02:19:32 EDT
This will have to wait a bit as I am busy
with other Java 7 related deliverables.
Comment 3 John Arthorne CLA 2013-07-30 11:03:36 EDT
(In reply to comment #0)
> All casts to E[] and E generate "Type safety: Unchecked cast ..." problems,
> and all these problems should stay even if unavoidable problems are
> suppressed. These problems cannot be solved by generifying anything.

I didn't quite follow your reasoning. You say this class of problems cannot be solved by generifying, but they should not be suppressed as "unavoidable problems". To me "cannot be solved" and "unavoidable" sound like the same thing. Maybe I am just misunderstanding what you are proposing here.
Comment 4 Markus Keller CLA 2013-07-30 12:18:41 EDT
(In reply to comment #3)
When re-reading the documentation for this option, I found that it misses the important restriction "unavoidable *due to raw APIs*".

Filed bug 414033 to explain the reasoning and to fix the docs.
Comment 5 John Arthorne CLA 2013-07-30 13:28:48 EDT
That explains it, thanks. Unavoidable really means "unavoidable because I am referring to an API that does not use generics".
Comment 6 Stephan Herrmann CLA 2013-08-01 11:33:22 EDT
Note that the latest commit left a dangling

  TESTS_NAMES = new String[] { "test338350" };

=> most of the tests are disabled.

I've fixed this via bug 413958 :)
Comment 7 ANIRBAN CHAKRABORTY CLA 2013-08-05 04:12:49 EDT
Verified for eclipse-SDK-I20130730-0800.
Comment 8 Manoj N Palat CLA 2013-08-06 07:18:56 EDT
(In reply to comment #7)
> Verified for eclipse-SDK-I20130730-0800.

which is Eclipse Luna 4.4M1
Comment 9 Markus Keller CLA 2013-08-08 05:32:15 EDT
(In reply to comment #6)
> => most of the tests are disabled.

Thanks for cleaning up! The tests still ran in the build, since the static initializer in RunComparableTests resets the TestCase.TESTS_* global variables.