Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #38471
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Checked casting with Generics |
| Date | 2018-07-04 17:22 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <phjdqv$2ve$1@dont-email.me> (permalink) |
| References | <phj8qt$hpp$1@news.albasani.net> |
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<Header> 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<Header> e = m.getAllHeaders();
> [javac] ^
> [javac] required: Enumeration<Header>
> [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<Header>) 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<Whatsit>' (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.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Checked casting with Generics Martin Gregorie <martin@mydomain.invalid> - 2018-07-04 19:57 +0000
Re: Checked casting with Generics Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-07-04 17:22 -0400
Re: Checked casting with Generics Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-07-04 17:37 -0400
Re: Checked casting with Generics Martin Gregorie <martin@mydomain.invalid> - 2018-07-04 23:03 +0000
Re: Checked casting with Generics Martin Gregorie <martin@mydomain.invalid> - 2018-07-05 15:38 +0000
Re: Checked casting with Generics Martin Gregorie <martin@mydomain.invalid> - 2018-07-04 22:56 +0000
csiph-web