Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: Checked casting with Generics Date: Wed, 4 Jul 2018 17:22:38 -0400 Organization: A noiseless patient Spider Lines: 66 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 4 Jul 2018 21:22:39 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="e8d34a50d9378d2fce96d06412f07bf3"; logging-data="3054"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+udtvpFpSLuBHUvnlfnk1J" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 Cancel-Lock: sha1:tpSng40x+YptAkG9Ry574Kl3yeg= In-Reply-To: Content-Language: en-US Xref: csiph.com comp.lang.java.programmer:38471 On 7/4/2018 3:57 PM, Martin Gregorie wrote: > I have a problem with casting Generics. Been searching around but haven't > found and solutions that work (and even 'solutions' that don't > compile(!). Anyway, here's the problem: > > I'm handling email using javax.mail classes. The code was originally > written with Java 6 and it has run untouched until now, when I changed > some code and recompiled the app using Java 8 (OpenJava 1.8.0). Its all > fine apart from a couple of casts in unrelated code. > > The immediate problem is that I need to handle the list of header in part > of a multipart message. The statement: > > Enumeration
e = m.getAllHeaders(); > > compiled without any fuss under Java 6, but when compiled now it issues a > warning: > [javac] /home/marchive/marchive/org/gregorie/mailarchive/internals/ > HeaderBlock.java:52: warning: [unchecked] unchecked conversion > [javac] Enumeration
e = m.getAllHeaders(); > [javac] ^ > [javac] required: Enumeration
> [javac] found: Enumeration Nothing definite, just a few things you might want to check: 1) What is the class/interface type of `m'? The point of looking into this is (a) to disambiguate among the six different getAllHeaders() methods, and (b) to make absolutely certain-sure it's a javax.mail class and not some accidental interloper. (I recall a questioner who couldn't get `@Override' to work; it turned out he was trying to use it in a class named `Override' ...) 2) Similarly with `Header': Is it really javax.mail.Header, and not some kind of illegal immigrant? (I'd guess this one would lead to a different symptom, but you might as well double-check.) 3) Is your javax.mail installation up-to-date, or reasonably so? If nothing's been touched since the Java 1.6 days (7-12.5 years ago), perhaps you've got an ancient javax.mail that can't keep up with the much younger JDK. (Current javax.mail version is 1.6.1, which I think has nothing at all to do with Java 1.6 or indeed with Java 1.anything.) 4) Is it an OpenJDK problem? What happens if you build with Oracle's JDK? (Even if you don't want to use Oracle's in production, a cross-check might help identify a bug.) > Writing it as a simple cast by adding (Enumeration
) after the > assignment operator has no effect and nothing I've adapted from examples > using isinstanceof or inherited Class.isInstance() does anything except > throw compilation syntax errors. Adding a cast shouldn't silence the warning, since the conversion from plain `Thing' to `Thing' (which takes place even without the cast) is still unchecked. > I can run with it like this, but would really like to handle this > unchecked conversion, partly to get rid of the compiler warning and > partly so I understand how it should be done. Unless something else turns up, the best I can suggest is to add `@SuppressWarning("unchecked")' and an extensive comment ... -- esosman@comcast-dot-net.invalid Nine hundred thirty-one days to go.