Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 115691

Summary: Wrong Warning (type safety) when casting to reifiable type
Product: [Eclipse Project] JDT Reporter: Dirk Willecke <public>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: CLOSED WORKSFORME QA Contact:
Severity: normal    
Priority: P3 CC: hashproduct+eclipse
Version: 3.2   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Dirk Willecke CLA 2005-11-09 15:04:42 EST
public class TestList extends java.util.ArrayList<Integer> {
	
static void test() {
  java.util.ArrayList<?> a1 = new TestList();
  TestList b1 = (TestList) a1;
  // Warning: The cast from ArrayList<capture-of ?> to TestList
  // is actually checking against the erased type TestList

  TestList c1 = TestList.class.cast(a1); // Workaround without warning
		
  java.util.ArrayList<Integer> a2 = new TestList();
  TestList b2 = (TestList) a2;  // No warning for cast from ArrayList<Integer>
}
}

Tested against: 3.2M3 stable build and I20051108-1011

TestList is reifiable. Imho there shouldn't be a warning.
Comment 1 Matt McCutchen CLA 2005-12-17 21:56:28 EST
I ran into the same problem and found the same workaround.  The message "cast to T is actually checking against T" makes no sense; I agree that it should be removed.  I'm using Fedora Core's Eclipse 3.1.1 with "Build id: M20050929-0840".

Another workaround is to cast the object to a raw type first.  The extra cast generates an "unnecessary cast" warning, but that's better than an unchecked warning.
   TestList b1 = (TestList) (ArrayList) a1;
Comment 2 Olivier Thomann CLA 2006-10-10 13:46:00 EDT
Added regression test org.eclipse.jdt.core.tests.compiler.regression.GenericTypeTest#1052.

Could not reproduce in HEAD.
Comment 3 Matt McCutchen CLA 2006-10-11 21:52:13 EDT
I confirmed that the problem seems to be gone in HEAD.
Comment 4 Olivier Thomann CLA 2006-10-11 21:55:27 EDT
Closing.
Comment 5 Olivier Thomann CLA 2006-10-11 21:55:43 EDT
Thanks for double-checking.