Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Generics: is actually checking against the erased type
Generics: is actually checking against the erased type [message #196411] Mon, 21 February 2005 10:56 Go to next message
Eclipse UserFriend
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 #196440 is a reply to message #196411] Mon, 21 February 2005 14:50 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: gwiseman.exchangesolutions.net

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. ;)
Re: Generics: is actually checking against the erased type [message #196572 is a reply to message #196440] Wed, 23 February 2005 06:33 Go to previous messageGo to next message
Eclipse UserFriend
Indeed, our support for 1.5 is making progress; and things we did not handle
in M4 are now better supported in M5.
As for getting rid of the additional warning, we will add support for
@SuppressWarnings annotation soon.

"Geoffrey Wiseman" <gwiseman@exchangesolutions.net> wrote in message
news:cvde2m$8qs$1@www.eclipse.org...
> 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. ;)
>
>
>
Re: Generics: is actually checking against the erased type [message #196580 is a reply to message #196440] Wed, 23 February 2005 06:44 Go to previous messageGo to next message
Eclipse UserFriend
BTW, on the following I am getting no unchecked warning:

public class X {
protected <T> T load(Class<T> clazz) throws Exception {
return (T) getTestSession().load(clazz);
}
Session getTestSession() {
return new Session();
}
public static void main(String argv[]) {
}
}
class Session {
<U> U load(Class<U> clazz) throws Exception {
return clazz.newInstance();
}
}

It correctly flags that the cast to (T) is unnecessary. So you may have
opportunities to make your code warning free.

"Geoffrey Wiseman" <gwiseman@exchangesolutions.net> wrote in message
news:cvde2m$8qs$1@www.eclipse.org...
> 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. ;)
>
>
>
Re: Generics: is actually checking against the erased type [message #196757 is a reply to message #196440] Thu, 24 February 2005 11:19 Go to previous messageGo to next message
Eclipse UserFriend
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. 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. ;)
>
>
>
Re: Generics: is actually checking against the erased type [message #196989 is a reply to message #196757] Fri, 25 February 2005 09:43 Go to previous message
Eclipse UserFriend
"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. ;)
>>
>>
Previous Topic:Adding a Jar Dinamically in a Wizard
Next Topic:What are the new features in 3.1M5a?
Goto Forum:
  


Current Time: Mon May 12 16:59:31 EDT 2025

Powered by FUDForum. Page generated in 0.03340 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top