Bug 102181 - [1.5][compiler] Generic varargs are built with incorrect array type
Summary: [1.5][compiler] Generic varargs are built with incorrect array type
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P3 major (vote)
Target Milestone: 3.1.1   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 102278 108095 (view as bug list)
Depends on:
Blocks:
 
Reported: 2005-06-29 14:00 EDT by David Stubbs CLA
Modified: 2005-09-26 10:07 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Stubbs CLA 2005-06-29 14:00:39 EDT
The basic problem is that when building the varargs for A... args where A has
been specified as String, the compiler is ignoring the specified type and
building an array of Objects instead.

Note that this behaviour has been introduced since at least Eclipse 3.1rc1.

This causes a ClassCastException in the following code:
public class MyTest {

    public static void main(String[] args) {
        Test<String> t = new Tester();
        t.method("1st string");
    }
    
    static abstract class Test<A> {
        abstract void method(A... args);
    }
    
    static class Tester extends Test<String> {

        void method(String... args) {
            System.out.println(args);
            call(args);
        }
        
        void call(String[] args) {
            for ( String str : args )
                System.out.println("String: "+str);
        }
    }
    
}

the exact output when compiled with Eclipse 3.1 is:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object;
	at MyTest$Tester.method(MyTest.java:1)
	at MyTest.main(MyTest.java:10)

when compiled using Eclipse 3.1rc1, or javac from Sun's JDK1.5.0_01 the output
is as I would expect:
[Ljava.lang.String;@360be0
String: 1st string

I don't know which is the "correct" result, but if the latest Eclipse is then I
would expect a compiler warning or error, rather than a run-time ClassCastException.
Comment 1 Olivier Thomann CLA 2005-06-30 09:24:29 EDT
*** Bug 102278 has been marked as a duplicate of this bug. ***
Comment 2 Philipe Mulet CLA 2005-07-04 03:42:49 EDT
Added VarargsTest#test031.

Problem comes from the fact the original method is used instead of parameterized
one for retrieving the varargs array type.

Test<String> -> method(String...) -> original: method(A...) -> arg leaftype: A
-> erasure(): Object.
It should simply erase the substituted argument: String -> String

Fixed
Comment 3 Philipe Mulet CLA 2005-07-04 03:48:50 EDT
Fixed
Comment 4 Maxime Daniel CLA 2005-08-09 10:28:19 EDT
Verified in 3.2 M1 with build I20050808-2000.
Comment 5 Olivier Thomann CLA 2005-08-26 09:22:57 EDT
*** Bug 108095 has been marked as a duplicate of this bug. ***
Comment 6 David Audel CLA 2005-09-26 10:07:57 EDT
Verified using M20050923-1430 for 3.1.1