Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #38470

Checked casting with Generics

From Martin Gregorie <martin@mydomain.invalid>
Newsgroups comp.lang.java.programmer
Subject Checked casting with Generics
Date 2018-07-04 19:57 +0000
Organization albasani.net
Message-ID <phj8qt$hpp$1@news.albasani.net> (permalink)

Show all headers | View raw


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

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.

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.



-- 
Martin    | martin at
Gregorie  | gregorie dot org

Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread


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