JDK-6223347 : (reflect) Add a generic replacement for Array.newInstance() to help write type safe code
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: linux
  • CPU: x86
  • Submitted: 2005-01-31
  • Updated: 2022-08-09
  • Resolved: 2022-08-09
Related Reports
Relates :  
Relates :  
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
  Bug # 5101892 explains why the Array.newInstance() method can't be retrofitted to be typesafe.

Please add a new static method to the Array class that is type safe, with a signature like this one:

public static <T> T[] Array.newArray(Class<T> componentType, int length)

.

 

JUSTIFICATION :
Having this new type-safe method will give us a better way to write code than using the @SupressWarnings("unchecked") annotation. It will lead to cleaner code, and be a small step towards reducing caclification in the Java libraries.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I expect to be able to compile code that creates arrays without getting any warnings.
ACTUAL -
When I compile the code, I get warnings that I should be able to fix.

---------- BEGIN SOURCE ----------
import java.util.List;


class ArrayWarnings
{
//    @SuppressWarnings("unchecked")
    private List<String>[] createArray(int size)
    {
        return (List<String>[])Array.newInstance(List.class,size);
    }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
@SupressWarnings("unchecked") provides a grezzy option, but -Xlint ignores it at the moment.
###@###.### 2005-1-31 03:21:51 GMT

Comments
Closing as will not fix.
09-08-2022