Community
Participate
Working Groups
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()); } }
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 ***
(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!!
Verified for 3.3 M1 using build I20060807-2000.
Verified for 3.2.1 using build M20060915-1045