Bug 82288

Summary: Wrong "Unnecessary cast" warning with generic array instantiation
Product: [Eclipse Project] JDT Reporter: Daniel Le Berre <leberre>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: RESOLVED DUPLICATE QA Contact:
Severity: normal    
Priority: P3    
Version: 3.1   
Target Milestone: 3.1 M5   
Hardware: PC   
OS: Linux   
Whiteboard:

Description Daniel Le Berre CLA 2005-01-05 17:13:29 EST
As far as I understood Java generics, it looks like there is no other way to
instantiate a generic array.

T [] myarray = (T[])new Object[size];

Eclipse 3.1M6 reports an unnecessary cast warning here, while removing the cast
prevent the code to compile.

It would be nice to correct that warning issue. unnecessary cast warnings are
pretty helpful when moving existing code to "generic" code.
Comment 1 Olivier Thomann CLA 2005-01-05 17:22:50 EST
With latest we report:
----------
1. WARNING in C:\tests_sources\Toto.java
 (at line 3)
	T [] myarray = (T[]) new Object[10];
	               ^^^^^^^^^^^^^^^^^^^^
Type safety: The cast from Object[] to T[] is actually checking against the
erased type Object[]
----------

In fact, javac is reporting an unchecked cast warning:

Toto.java:3: warning: [unchecked] unchecked cast
found   : java.lang.Object[]
required: T[]
    T [] myarray = (T[])new Object[10];
                        ^
1 warning

This looks consistent.
Ok to close?
Comment 2 Philipe Mulet CLA 2005-01-05 18:56:41 EST
This is another symptom of bug 78591.

*** This bug has been marked as a duplicate of 78591 ***
Comment 3 Daniel Le Berre CLA 2005-01-06 01:41:10 EST
ok, you can close.