Re: Generics and ClassLoaders
- From: Josef Garvi <josef.REMOVE@xxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 30 Apr 2005 08:59:20 +0200
Chris Smith wrote:
Josef Garvi <josef.REMOVE@xxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Yep, returning Class<?> makes perfect sense, but warning me when I cast it to Class<LinkHandler> less so - after all I am explicitly casting.
In the particular instance of casting to Class<LinkHandler>, you probably shouldn't be doing this anyway. If you can refer to the class lexically from the code, then you should just use a class literal expression instead of ClassLoader.loadClass to obtain the reference. However, you might wish to cast the reference to something else, for example, Class<? extends LinkHandler>, and the problem would still apply.
Yes, Class<? extends LinkHandler> is what I should cast it to. It's a plugin system, so I can't no the precise class at compile time.
I'd really like to get rid of the compiler warnings when
I'm doing things right - otherwise i'll either start ignoring them, or turn them off - neither of which is likely to improve my code's type safety! :-)
It's unfortunately not really possible to develop with generics and warnings for unchecked casts enabled, without just ignoring certain compiler warnings.
Ok.
I've returned to just using Class<?> and using a cast at newInstance(). That way, at least I don't need to get bugged by the warning.
-- Josef Garvi
"Reversing desertification through drought tolerant trees" http://www.eden-foundation.org/
new income - better environment - more food - less poverty .
- References:
- Generics and ClassLoaders
- From: Josef Garvi
- Re: Generics and ClassLoaders
- From: John C. Bollinger
- Re: Generics and ClassLoaders
- From: Josef Garvi
- Re: Generics and ClassLoaders
- From: Chris Smith
- Generics and ClassLoaders
- Prev by Date: Re: Using Reflection to get all Classes?
- Next by Date: Re: Create InetAddress from a String.
- Previous by thread: Re: Generics and ClassLoaders
- Next by thread: Java Threads
- Index(es):
Relevant Pages
|