JDK-6227971 : generic inferrence bug in varargs
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2005-02-10
  • Updated: 2010-04-02
  • Resolved: 2005-02-12
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
12-02-2005