United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6192945 Declaration order of interdependent generic types should not matter
JDK-6192945 : Declaration order of interdependent generic types should not matter

Details
Type:
Bug
Submit Date:
2004-11-09
Status:
Closed
Updated Date:
2011-03-08
Project Name:
JDK
Resolved Date:
2011-03-08
Component:
tools
OS:
windows_2000
Sub-Component:
javac
CPU:
x86
Priority:
P5
Resolution:
Fixed
Affected Versions:
5.0
Fixed Versions:

Related Reports
Backport:
Relates:
Relates:
Relates:
Relates:
Relates:
Relates:

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)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows 2000 [Version 5.00.2195]

A DESCRIPTION OF THE PROBLEM :
Discussed in more detail in the generics forum (http://forum.java.sun.com/thread.jsp?forum=316&thread=566266), this bug relates to the unnecessary restriction of the declaration order of generic types in a class or method. Basically the following class declaration

    public class MyClass<E, D super E>

causes the following compiler errors

    Error:  line (1) > expected
    Error:  line (4) '{' expected

Rewritten in another form, the following declaration

    public class MyClass<E extends D, D>

causes the following compiler error

    Error:  line (1) illegal forward reference

Section 6.3 of the Language Specification says that "type parameters can appear as parts of their own bounds, or as bounds of other type parameters declared in the same section."  The current behaviour appears to contradict this.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Attempt to compile either of the two following classes:

public class MyClass<E, D super E> {}

public class MyClass<E extends D, D> {}


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Successful compilation for either form of the class declaration.
ACTUAL -
Compilation failures for both forms of the class declaration.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Compilation of

public class MyClass<E, D super E> {}

yields the following errors

    Error:  line (1) > expected
    Error:  line (4) '{' expected


Compilation of

public class MyClass<E extends D, D> {}

yields the following error

Error:  line (1) illegal forward reference


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
Either

public class MyClass<E, D super E> {}

or

public class MyClass<E extends D, D> {}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Current workaround involves swapping the declaration order of the generic types. This can be undesirable, particularly in my case, as the class needs to extend an interface, thus

public class MyClass<D super E, E> implements MyInterface<E, D>

so we have the awkward situation of the the generic types being reversed for the interface and the corresponding class, thus

MyInterface<Integer, Number> obj = new MyClass<Number, Integer>();


Note that there are situations for which there appears to be no such workaround, for example

public class SomeClass<S extends T, T extends SomeInterface<S>> {}
###@###.### 2004-11-09 20:10:37 GMT

                                    

Comments
EVALUATION

This is two seperate issues:

1. Allowing lower bounds on type variables.
2. Making the declaration order of type variables independent.

ad 1. see RFE 5052956.

ad 2. this is a compiler bug and here is a simple example of what should be allowed:

class Test<D extends E, E> {}

It is not yet clear if there are any restrictions on the use type variables (are cycles OK?) I can't find any restrictions in JLS.

###@###.### 2004-11-10 01:07:58 GMT

The only (unspecified) restriction is that we cannot have a cycle in the subtype relation.

###@###.### 2004-11-11 03:53:24 GMT
                                     
2004-11-10
SUGGESTED FIX

Webrev of changes: http://sa.sfbay/projects/langtools/bugid_summary.pl?bugid=6192945
See also attachment 6192945.tar.gz.
                                     
2006-11-25



Hardware and Software, Engineered to Work Together