Re: Generics and ClassLoaders



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
.



Relevant Pages

  • Re: void * vs char *
    ... That doesn't mean never cast, but it does mean that if the compiler is warning you, you should generally assume it's right. ... and not some optional lint tool to pick through my code for places ... If you don't like your compiler being helpful, either turn off or ignore the warnings. ...
    (comp.lang.c)
  • Re: shame on MISRA
    ... of warnings about "explicit cast required" for assignments and ... with a cast. ... GCC with no warnings. ...
    (comp.arch.embedded)
  • Re: Correct way to handle "makes integer from pointer without cast
    ... warnings of "makes integer from pointer without cast." ... The second arg is a const char *, ...
    (comp.unix.programmer)
  • Re: 0/1 Knapsack problem, what am I doing wrong
    ... Not using a cast can also turn off a diagnostic in certain situations. ... It shows that gcc does not by default generate the warnings that several people claim that you get from compilers. ... If when people advised casting they also advised making sure that compiler options to enable the warnings for implicit function declarations I would be less concerned about it. ...
    (comp.lang.c)
  • Re: [PATCH 2/4] vfs: explicitly cast s_maxbytes in fiemap_check_ranges
    ... Johannes Weiner wrote: ... possible that the value would be cast to a signed value for comparison ... happen by explicitly casting s_maxbytes to an unsigned value for the ... however that it doesn't hurt to do explict casts when comparing signed ...
    (Linux-Kernel)