Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #38473
| From | Martin Gregorie <martin@mydomain.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Checked casting with Generics |
| Date | 2018-07-04 22:56 +0000 |
| Organization | albasani.net |
| Message-ID | <phjjao$jak$1@news.albasani.net> (permalink) |
| References | <phj8qt$hpp$1@news.albasani.net> <phjdqv$2ve$1@dont-email.me> |
On Wed, 04 Jul 2018 17:22:38 -0400, Eric Sosman wrote:
> 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' ...)
>
Its javax.mail.Part.getAllHeaders() and returns an Enumeration - which is
a bit wild seeing thats only defined as an interface.
> 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.)
>
Yes, it is.
> 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.)
>
Good point. It may well be 1.4. The weather is looking better, so I'm
going flying tomorrow and hopefully won't be around to check/update for a
day or three, but I will have the latest version before coming back to
this.
> 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.)
>
I don't think so. I haven't had any problems I can trace to it being
OpenJava since I switched somewhat almost a year ago. And anyway, I
though its source was maintained bt Oracle - or at least that they had a
fairly large control over it.
>> 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 ...
Good point. Thanks.
--
Martin | martin at
Gregorie | gregorie dot org
Back to comp.lang.java.programmer | Previous | Next — Previous 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