United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: 6227971 generic inferrence bug in varargs
6227971 : generic inferrence bug in varargs

Details
Type:
Bug
Submit Date:
2005-02-10
Status:
Closed
Updated Date:
2010-04-02
Project Name:
JDK
Resolved Date:
2005-02-12
Component:
tools
OS:
windows_xp
Sub-Component:
javac
CPU:
x86
Priority:
P4
Resolution:
Not an Issue
Affected Versions:
5.0
Fixed Versions:

Related Reports

Sub Tasks

Description
FULL PRODUCT VERSION :
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing)


ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
javac report unchecked array creation for varargs parameters on code that is parameterized correctly

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
enable -Xlint:unchecked for javac and try to compile the code provided

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
compilation result should be fine
ACTUAL -
compilation warnings

ERROR MESSAGES/STACK TRACES THAT OCCUR :
nformation: Compilation completed successfully with 1 warnings
Information: 0 errors
Information: 1 warning
C:\test\GenericVarArgsTest.java
    Warning: Warning: line (21)[unchecked] unchecked generic array creation of type Queue<SessionImpl>[] for varargs parameter

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class GenericVarArgsTest {
    public static void main( String[] args ) {
        Controller<SessionImpl> controller = new ControllerImpl<SessionImpl>( new QueueImpl(), new QueueImpl() );
    }
}

interface Session {
}

class SessionImpl implements Session {
}

interface Queue <S extends Session> {
}

class QueueImpl implements Queue<SessionImpl> {
}

interface Controller <S extends Session> {
}

class ControllerImpl <S extends Session> implements Controller<S> {
    private Queue<S>[] queues;

    public ControllerImpl( Queue<S>... queues ) {
        this.queues = queues;
    }
}
---------- END SOURCE ----------
###@###.### 2005-2-10 22:48:58 GMT

                                    

Comments
EVALUATION

This is not a bug.  Arrays (hence varargs) don't mix well
with generic types.

The problem is illustrated by adding a method to 
ControllerImpl:

void messUp() {
    Queue<?>[] qs = queues;
    Queue<Integer> q = new Queue<Integer>();
    q.add(1); // if such a method exists
    qs[0] = q;
    // queues[0] is a Queue<Integer>, which can later cause
    // a class cast exception as all the queues in queues are
    // supposed to contain subclasses of Session.
}

###@###.### 2005-2-12 21:22:15 GMT
                                     
2005-02-12



Hardware and Software, Engineered to Work Together