Re: Generics and ClassLoaders



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
.



Relevant Pages

  • Re: Why INFINITE loop in a thread occupy so much CPU time??
    ... With the attendant warning about the constant expression. ... This is a common programming technique. ... Good programming practice would consist of using the compiler at ... One can write 'bool' in C++ but not C. ...
    (microsoft.public.vc.mfc)
  • Re: help needed please!
    ... But some compilers like to give you a warning ... Each warning a compiler can give you is important when you not ... but you may use flages to handle flags to handle flags when you ... >> stdout is line bufferd and until one or both of the following cases is ...
    (comp.lang.c)
  • Re: Overloading abstract methods
    ... I'm calling this abstract class ResumeWriter. ... > use of in each subclass is up to you. ... >> However, if I do that, I get compiler warnings that the parameters for ... Disable that warning. ...
    (comp.lang.java.programmer)
  • Re: Portability / compatibility issues
    ... I have posted my test code below, and the results I get on each compiler. ... The crux of the matter is that the 'strstr' function differs wildly in its behaviour on different platforms, and precious few man pages will actually provide a worked example of any function, let alone this one. ... I have had to write the "has_slash" function and use the reserved work SLASH already, to get around the problems that strstrgives me. ...
    (comp.lang.c)
  • Re: Developer poll. return within finally: I need your opinons
    ... > example) but the compiler still raises the warning. ... Section 14.1 of the Language Spec covers normal vs. abrupt ... unwise to write code whose correct interpretation by another human ...
    (comp.lang.java.programmer)