Generics: is actually checking against the erased type [message #196411] |
Mon, 21 February 2005 10:56  |
Eclipse User |
|
|
|
Originally posted by: gwiseman.exchangesolutions.net
I've just upgraded to M5 (from M4), and I've got a few warnings on
generics that I can't seem to resolve happily (as I did in M4). Is there
anything I can do about the 'is actually checking against the erased type'
messages short of turning off all the typesafety checks around generics?
For instance, the following code:
protected <T> T load( Class<T> clazz, Long persistentId )
throws HibernateException
{
return (T) getTestSession().load( clazz, persistentId );
}
This is a method that loads a class using Hibernate but, using generic
methods, returns the type specified in the parameter rather than the
supertype Object. Eclipse warns:
Type safety: The cast from Object to T is actually checking against the
erased type Object
But what can I do about it? Removing the cast simply turns into an error
-- I'm trying to return an Object instead of a T. Am I missing something
here?
Thanks
|
|
|
|
|
|
|
Re: Generics: is actually checking against the erased type [message #196989 is a reply to message #196757] |
Fri, 25 February 2005 09:43  |
Eclipse User |
|
|
|
"R.J. Lorimer" <rjlorimer@coffee-bytes.com> wrote in message
news:cvkuqk$j27$1@www.eclipse.org...
> Geoffrey,
>
> Contributing to what Philippe has already said:
>
> 1.) You wouldn't always need that cast if you had control of the because
> the class being passed in already is parameterized for that type, and if
> the 'load' method was properly parameterized, method inference should
> allow you to not cast at all (this is largely dependent on the
> implementation of the session object - see Phillipe's second message for a
> good example).
> 2.) One documenting way to avoid this unchecked warning in Java code
> (assuming you have to have it because of some situation in the code) is
> via the use of the @SuppressWarnings annotation - which Eclipse is still
> not quite supporting.
I don't believe javac 1.5.0_01 is supporting it yet either (though it is
supporting
some of the other builtin annotations like @Override). Still, it would be
nice
if eclipse did support it. The legal arguments are suppossed to be the
values
for -Xlint:xxx.
Steve Buroff.
>Your code would look something like this:
>
> @SuppressWarnings("unchecked")
> protected <T> T load(Class<T> clazz, Long id) throws HibernateException {
> // ...
> }
>
> 3.) Another option that works now that is a bit different is to use the
> Class.cast method:
>
> protected <T> T load(Class<T> clazz, Long id) throws HibernateException {
> Object o = getTestSession().load(clazz, id);
> return clazz.cast(o); // casts o to the runtime type of T
> }
> Geoffrey Wiseman wrote:
>> Geoffrey Wiseman wrote:
>>
>>> Type safety: The cast from Object to T is actually checking against the
>>> erased type Object
>>
>>
>> Re-reading Gilad Bracha's generics tutorial, 7.2 points out that types
>> used in casts are erased so that there's an unchecked warning. This is
>> presumably Eclipse's way to telling me the same thing.
>>
>> In this case, what I really want is the ability to tell Eclipse that I've
>> processed and verified the warning and that I don't need to see this
>> warning for this line any longer. ;)
>>
>>
|
|
|
Powered by
FUDForum. Page generated in 0.03340 seconds