Re: Generics and ClassLoaders
- From: Josef Garvi <josef.REMOVE@xxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 29 Apr 2005 23:07:26 +0200
John C. Bollinger wrote:
Look at the Javadocs: ClassLoader.loadClass(String) returns a Class<?>. You can assign it to a variable of that type without warning. This makes complete sense, because there is no general way to determine at compile time what type the method will load a Class for. The class might not even be available to the compiler. Making any assumption about what class has been loaded is inherently a type safety problem (which is nothing new; it's just that Java 5 produces warnings for it, and previous compilers didn't). You should load the class per your last example, and put up with the type safety warning that you will get.
Yep, returning Class<?> makes perfect sense, but warning me when I cast it to Class<LinkHandler> less so - after all I am explicitly casting. Sooner or later, a runtime error will anyway occur if I get it wrong. But I guess I am warned because erasure implies that the runtime cast error can not be detected at that particular line of code, should the types not match.
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! :-)
-- Josef Garvi
"Reversing desertification through drought tolerant trees" http://www.eden-foundation.org/
new income - better environment - more food - less poverty .
- Follow-Ups:
- Re: Generics and ClassLoaders
- From: Chris Smith
- Re: Generics and ClassLoaders
- References:
- Generics and ClassLoaders
- From: Josef Garvi
- Re: Generics and ClassLoaders
- From: John C. Bollinger
- Generics and ClassLoaders
- Prev by Date: Re: Comparables and Generics
- Next by Date: Re: Generics and ClassLoaders
- Previous by thread: Re: Generics and ClassLoaders
- Next by thread: Re: Generics and ClassLoaders
- Index(es):
Relevant Pages
|