Bug 150294 - [1.5][compiler] getClass() does not return erased type
Summary: [1.5][compiler] getClass() does not return erased type
Status: VERIFIED DUPLICATE of bug 147381
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.2.1   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-07-11 14:49 EDT by Brad Cupit CLA
Modified: 2006-09-17 12:11 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Brad Cupit CLA 2006-07-11 14:49:56 EDT
when calling getClass() on a generic type, Eclipse states that a wildcard bound by the generic type will be returned. For example, the following code compiles in Eclipse 3.1.2 and 3.2:

    private static <T> void testWithGeneric(T input)
    {
        Class<? extends T> clazz = input.getClass();
    }

but javac won't compile this same code. Here's the javac error with java version 1.5.0_06:

    MainClass.java:20: incompatible types
    found   : java.lang.Class<capture of ? extends java.lang.Object>
    required: java.lang.Class<? extends T>
            Class<? extends T> clazz = input.getClass();

See this bug from the sun database:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5004321

According to the above mentioned bug, T must be erased, therefore getClass() should return Class<? extends Object>. javac appears to be correct, whereas Eclipse returns Class<? extends T>

This may be related to bug 120088 and bug 58666.
I tried hard to find an existing bug report on this, assuming that surely someone else has reported it. If this is a dupe, I sincerly apologize.

Complete source code to reproduce:

public class MainClass
{
    public static void main(String[] args)
    {
        String testString = "test string";

        testWithNonGeneric(testString);
        testWithGeneric(testString);
    }

    private static void testWithNonGeneric(String input)
    {
        Class<? extends String> clazz = input.getClass();
        
        System.out.println(clazz.getName());
    }

    private static <T> void testWithGeneric(T input)
    {
        Class<? extends T> clazz = input.getClass();
        
        System.out.println(clazz.getName());
    }
}
Comment 1 Philipe Mulet CLA 2006-07-11 18:15:35 EDT
Dup of bug 147381. This will be fixed in the 3.2.1 update (or 3.3).
Added GenericTypeTest#test1022

In the meantime, you may grab our 3.2.1 preview at:
http://www.eclipse.org/jdt/core/r3.2/index.php#UPDATES

*** This bug has been marked as a duplicate of 147381 ***
Comment 2 Brad Cupit CLA 2006-07-11 18:26:33 EDT
(In reply to comment #1)
> Dup of bug 147381. This will be fixed in the 3.2.1 update (or 3.3).

Very cool! Thanks for working on such a great product!!
Comment 3 Frederic Fusier CLA 2006-08-08 09:12:02 EDT
Verified for 3.3 M1 using build I20060807-2000.
Comment 4 Frederic Fusier CLA 2006-09-17 12:11:55 EDT
Verified for 3.2.1 using build M20060915-1045