jdk 5 warnings in the jre source code

Discussion in 'Java' started by johnmmcparland, Dec 9, 2008.

  1. Hi all,

    I'm compiling 1.4.2 code into java 5 and obviously there are a large
    number of warnings that i have to fix. However I notice that when I
    look at the java 5 jre source code (e.g. for DefaultTableModel) I
    notice that these classes do not fix some the issues I am (e.g.
    generics);

    public void addRow(Vector rowData) {
    insertRow(getRowCount(), rowData);
    }

    Clearly Vector is not given a type.

    How does the jre source avoid this. Do they simply use compiler flags
    or is it something in the code?
    johnmmcparland, Dec 9, 2008
    #1
    1. Advertising

  2. johnmmcparland

    Jan Thomä Guest

    On 2008-12-09 11:04:02 +0100, johnmmcparland
    <> said:

    > Hi all,
    >
    > I'm compiling 1.4.2 code into java 5 and obviously there are a large
    > number of warnings that i have to fix. However I notice that when I
    > look at the java 5 jre source code (e.g. for DefaultTableModel) I
    > notice that these classes do not fix some the issues I am (e.g.
    > generics);How does the jre source avoid this. Do they simply use
    > compiler flags
    > or is it something in the code?


    I assume they just ignore the compiler warnings. Which is something you
    can do as well, unless it bothers you. Some would argue that it should
    bother you but in the end it is your decision. You can also use the
    @SuppressWarnings annotation
    (http://java.sun.com/j2se/1.5.0/docs/api/java/lang/SuppressWarnings.html)
    to get rid of any warnings you deem superfluous.

    Best regards,
    Jan
    Jan Thomä, Dec 9, 2008
    #2
    1. Advertising

  3. On Dec 9, 11:10 am, Jan Thomä <> wrote:
    > On 2008-12-09 11:04:02 +0100, johnmmcparland
    > <> said:
    >
    > > Hi all,

    >
    > > I'm compiling 1.4.2 code into java 5 and obviously there are a large
    > > number of warnings that i have to fix.  However I notice that when I
    > > look at the java 5 jre source code (e.g. for DefaultTableModel) I
    > > notice that these classes do not fix some the issues I am (e.g.
    > > generics);How does the jre source avoid this.  Do they simply use
    > > compiler flags
    > > or is it something in the code?

    >
    > I assume they just ignore the compiler warnings. Which is something you
    > can do as well, unless it bothers you. Some would argue that it should
    > bother you but in the end it is your decision. You can also use the
    > @SuppressWarnings annotation
    > (http://java.sun.com/j2se/1.5.0/docs/api/java/lang/SuppressWarnings.html)
    > to get rid of any warnings you deem superfluous.
    >
    > Best regards,
    > Jan


    SuppressWarnings is not being used but I guessed they might ignore the
    compiler warnings. I don't want to in my code because it encourages
    people not to try and fix warnings they put in (e.g. if they only want
    a Vector of Strings use Vector<String>) and also because of the sheer
    number of them we can't see real warnings from spurious.
    johnmmcparland, Dec 9, 2008
    #3
  4. johnmmcparland

    Lew Guest

    johnmmcparland wrote:
    > SuppressWarnings is not being used but I guessed they might ignore the
    > compiler warnings. I don't want to in my code because it encourages
    > people not to try and fix warnings they put in (e.g. if they only want
    > a Vector of Strings use Vector<String>) and also because of the sheer
    > number of them we can't see real warnings from spurious.


    One should never use raw types in one's own code any more.

    Joshua Bloch, in /Effective Java/
    <http://java.sun.com/docs/books/effective/generics.pdf>
    <http://java.sun.com/docs/books/effective/index.html>,
    explains the proper way to deal with unchecked warnings. You suppress them at
    key places, and only those places, where you can prove that no
    ClassCastException will result, and you document that proof in comments at
    those places.

    Do the raw-to-generics conversion right at the point where you are forced to
    use legacy code (the only place where raw types should occur), earliest so
    that you have the benefits of generics soonest.

    --
    Lew
    Lew, Dec 9, 2008
    #4
  5. On Dec 9, 3:02 pm, Lew <> wrote:
    > johnmmcparland wrote:
    > > SuppressWarnings is not being used but I guessed they might ignore the
    > > compiler warnings.  I don't want to in my code because it encourages
    > > people not to try and fix warnings they put in (e.g. if they only want
    > > a Vector of Strings use Vector<String>) and also because of the sheer
    > > number of them we can't see real warnings from spurious.

    >
    > One should never use raw types in one's own code any more.
    >
    > Joshua Bloch, in /Effective Java/
    > <http://java.sun.com/docs/books/effective/generics.pdf>
    > <http://java.sun.com/docs/books/effective/index.html>,
    > explains the proper way to deal with unchecked warnings.  You suppress them at
    > key places, and only those places, where you can prove that no
    > ClassCastException will result, and you document that proof in comments at
    > those places.
    >
    > Do the raw-to-generics conversion right at the point where you are forced to
    > use legacy code (the only place where raw types should occur), earliest so
    > that you have the benefits of generics soonest.
    >
    > --
    > Lew


    Cheers. On a related note how do I add objects to a collection when I
    may not know the return type;

    public class SomeObj
    {
    public Vector<?> getMsgs()
    {
    // Do something that returns a Vector<String>
    }
    }

    public class SomeOtherObj extends SomeObj
    {
    public Vector<?> getMsgs()
    {
    // Do something that returns a Vector<byte[]>
    }
    }

    public class UserClass
    {
    public static void main(String args[])
    {
    SomeObj ob = new SomeObj();
    Vector<?> msgs = ob.getMsgs();
    // How do I do this?
    Vector<?> otherMsgs = ....
    otherMsgs.addAll(msgs);
    }

    }

    How do I add the contents of the 1st vector into the 2nd?
    johnmmcparland, Dec 9, 2008
    #5
  6. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    johnmmcparland schreef:
    > On Dec 9, 3:02 pm, Lew <> wrote:
    >> johnmmcparland wrote:
    >>> SuppressWarnings is not being used but I guessed they might ignore the
    >>> compiler warnings. I don't want to in my code because it encourages
    >>> people not to try and fix warnings they put in (e.g. if they only want
    >>> a Vector of Strings use Vector<String>) and also because of the sheer
    >>> number of them we can't see real warnings from spurious.

    >> One should never use raw types in one's own code any more.
    >>
    >> Joshua Bloch, in /Effective Java/
    >> <http://java.sun.com/docs/books/effective/generics.pdf>
    >> <http://java.sun.com/docs/books/effective/index.html>,
    >> explains the proper way to deal with unchecked warnings. You suppress them at
    >> key places, and only those places, where you can prove that no
    >> ClassCastException will result, and you document that proof in comments at
    >> those places.
    >>
    >> Do the raw-to-generics conversion right at the point where you are forced to
    >> use legacy code (the only place where raw types should occur), earliest so
    >> that you have the benefits of generics soonest.
    >>
    >> --
    >> Lew

    >
    > Cheers. On a related note how do I add objects to a collection when I
    > may not know the return type;
    >
    > public class SomeObj
    > {
    > public Vector<?> getMsgs()
    > {
    > // Do something that returns a Vector<String>
    > }
    > }
    >
    > public class SomeOtherObj extends SomeObj
    > {
    > public Vector<?> getMsgs()
    > {
    > // Do something that returns a Vector<byte[]>
    > }
    > }
    >
    > public class UserClass
    > {
    > public static void main(String args[])
    > {
    > SomeObj ob = new SomeObj();
    > Vector<?> msgs = ob.getMsgs();
    > // How do I do this?
    > Vector<?> otherMsgs = ....
    > otherMsgs.addAll(msgs);
    > }
    >
    > }
    >
    > How do I add the contents of the 1st vector into the 2nd?


    You can’t. Read the Sun generics document on the web for why.

    Instead, declare otherMsgs as Vector<String>, or rather as
    ArrayList<String>.

    Vector should not be used unless you need the synchronization it
    provides. Google for ‘Vector vs. ArrayList’.

    H.
    - --
    Hendrik Maryns
    http://tcl.sfs.uni-tuebingen.de/~hendrik/
    ==================
    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.9 (GNU/Linux)
    Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

    iEYEARECAAYFAkk+qKEACgkQBGFP0CTku6MFIwCeOapIH9sxTPUqnV4t46IVPPTl
    8WAAoNV4Z7tF2B8w/oMgoTc5y5NzQt4I
    =wQng
    -----END PGP SIGNATURE-----
    Hendrik Maryns, Dec 9, 2008
    #6
  7. On Dec 9, 5:19 pm, Hendrik Maryns <> wrote:
    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > johnmmcparland schreef:
    >
    >
    >
    > > On Dec 9, 3:02 pm, Lew <> wrote:
    > >> johnmmcparland wrote:
    > >>> SuppressWarnings is not being used but I guessed they might ignore the
    > >>> compiler warnings.  I don't want to in my code because it encourages
    > >>> people not to try and fix warnings they put in (e.g. if they only want
    > >>> a Vector of Strings use Vector<String>) and also because of the sheer
    > >>> number of them we can't see real warnings from spurious.
    > >> One should never use raw types in one's own code any more.

    >
    > >> Joshua Bloch, in /Effective Java/
    > >> <http://java.sun.com/docs/books/effective/generics.pdf>
    > >> <http://java.sun.com/docs/books/effective/index.html>,
    > >> explains the proper way to deal with unchecked warnings.  You suppress them at
    > >> key places, and only those places, where you can prove that no
    > >> ClassCastException will result, and you document that proof in comments at
    > >> those places.

    >
    > >> Do the raw-to-generics conversion right at the point where you are forced to
    > >> use legacy code (the only place where raw types should occur), earliest so
    > >> that you have the benefits of generics soonest.

    >
    > >> --
    > >> Lew

    >
    > > Cheers.  On a related note how do I add objects to a collection when I
    > > may not know the return type;

    >
    > > public class SomeObj
    > > {
    > >     public Vector<?> getMsgs()
    > >     {
    > >         // Do something that returns a Vector<String>
    > >     }
    > > }

    >
    > > public class SomeOtherObj extends SomeObj
    > > {
    > >     public Vector<?> getMsgs()
    > >     {
    > >         // Do something that returns a Vector<byte[]>
    > >     }
    > > }

    >
    > > public class UserClass
    > > {
    > >      public static void main(String args[])
    > >      {
    > >          SomeObj ob = new SomeObj();
    > >          Vector<?> msgs = ob.getMsgs();
    > >          // How do I do this?
    > >          Vector<?> otherMsgs = ....
    > >          otherMsgs.addAll(msgs);
    > >      }

    >
    > > }

    >
    > > How do I add the contents of the 1st vector into the 2nd?

    >
    > You can’t.  Read the Sun generics document on the web for why.
    >
    > Instead, declare otherMsgs as Vector<String>, or rather as
    > ArrayList<String>.
    >
    > Vector should not be used unless you need the synchronization it
    > provides.  Google for ‘Vector vs. ArrayList’.
    >
    > H.
    > - --
    > Hendrik Marynshttp://tcl.sfs.uni-tuebingen.de/~hendrik/
    > ==================
    > Ask smart questions, get good answers:http://www.catb.org/~esr/faqs/smart-questions.html
    > -----BEGIN PGP SIGNATURE-----
    > Version: GnuPG v2.0.9 (GNU/Linux)
    > Comment: Using GnuPG with SUSE -http://enigmail.mozdev.org
    >
    > iEYEARECAAYFAkk+qKEACgkQBGFP0CTku6MFIwCeOapIH9sxTPUqnV4t46IVPPTl
    > 8WAAoNV4Z7tF2B8w/oMgoTc5y5NzQt4I
    > =wQng
    > -----END PGP SIGNATURE-----


    Cheers. Yea I know about the Vectors vs ArrayList bit.

    I suppose the problem I'm trying to acheive here is having a Vector
    which could store Strings or could store Integers. I know I will only
    store one of the other but not sure which (depends on the calling
    classes).
    johnmmcparland, Dec 9, 2008
    #7
  8. johnmmcparland

    Roedy Green Guest

    On Tue, 9 Dec 2008 02:04:02 -0800 (PST), johnmmcparland
    <> wrote, quoted or indirectly quoted
    someone who said :

    >
    >I'm compiling 1.4.2 code into java 5 and obviously there are a large
    >number of warnings that i have to fix.


    Most of these will be proddings to use generics. Sun added generics to
    all their collection classes.

    See http://mindprod.com/jgloss/generics.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    PM Steven Harper is fixated on the costs of implementing Kyoto, estimated as high as 1% of GDP.
    However, he refuses to consider the costs of not implementing Kyoto which the
    famous economist Nicholas Stern estimated at 5 to 20% of GDP
    Roedy Green, Dec 9, 2008
    #8
  9. johnmmcparland

    Lew Guest

    johnmmcparland wrote:
    > I suppose the problem I'm trying to acheive here is having a Vector
    > which could store Strings or could store Integers. I know I will only
    > store one of the other but not sure which (depends on the calling
    > classes).


    You can use a List (pick your own implementation) or Collection that
    stores a common supertype of the two types, without wildcards.

    Collection <Object> stuff = new ArrayList <Object> ();

    A wildcard indicates a homogeneous collection, one that wouldn't be able to
    hold both Strings and Integers, though it could hold Objects.

    The point of generic types is to capture and enforce type decisions. This
    Collection will only know that it holds Objects, so you don't have much
    benefit of generics.

    Since you indicate that your collection is homogeneous, you can use a wildcard
    pegged to the common supertype, '? extends Object', or more succinctly, '?'.
    Based on a flag or input or something, you instantiate the actual collection
    type:

    Collection <?> stuff = (isAlfa? new ArrayList <String> ()
    : new ArrayList <Integer> ());

    This buys you a slight, very slight generics advantage, but doesn't free you
    of risky runtime casting.

    You get full benefit if you use a generic method or class to handle the logic,
    separating the input format from its processing polymorphically and generically.

    This slightly long example should convey the main idea, that generics plus
    polymorphism equals object-oriented type safety. The class 'ScreenHandler'
    fakes out how it might handle received data differently for String or Integer.
    The nested classes simulate the isolation between screen input and typed
    handling by the logic. Notice in particular the ScreenHandler main() and
    handle() methods near the bottom. This is an SSCCE - the output is shown at
    the bottom.

    <sscce class="testit.ScreenHandler" >
    /* ScreenHandler */
    package testit;

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collection;

    /** ScreenHandler. */
    public class ScreenHandler
    {
    public static class ScreenData
    {
    private static final String [] ALFAS = { "1", "2", "3" };
    public Collection <Object> getData()
    {
    return Arrays.asList( (Object []) ALFAS );
    }
    }

    public static interface Handler <T>
    {
    public void gather( ScreenData data );
    public void act();
    }

    public static class AlfaHandler implements Handler <String>
    {
    private Collection <String> stuff = new ArrayList <String> ();

    @Override public void gather( ScreenData data )
    {
    for ( Object datum : data.getData() )
    {
    stuff.add( datum.toString() );
    }
    }

    @Override public void act()
    {
    System.out.print( "ALFA: { " );

    for ( String thing : stuff )
    {
    System.out.print( "\"" );
    System.out.print( thing );
    System.out.print( "\", " );
    }
    System.out.println( "}" );
    }
    }

    public static class NumaHandler implements Handler <Integer>
    {
    private Collection <Integer> stuff = new ArrayList <Integer> ();

    @Override public void gather( ScreenData data )
    {
    for ( Object datum : data.getData() )
    {
    try
    {
    stuff.add( Integer.valueOf( datum.toString() ));
    }
    catch ( NumberFormatException exc )
    {
    stuff.add( Integer.valueOf( 0 ));
    }
    }
    }

    @Override public void act()
    {
    System.out.print( "NUMA: { " );

    for ( Integer thing : stuff )
    {
    System.out.print( thing );
    System.out.print( ", " );
    }
    System.out.println( "}" );
    }
    }

    private boolean alfa;
    public final boolean isAlfa()
    {
    return this.alfa;
    }
    public final void setAlfa( boolean value )
    {
    this.alfa = value;
    }

    public void handle( ScreenData data )
    {
    Handler <?> handler =
    (isAlfa()? new AlfaHandler() : new NumaHandler());
    handler.gather( data );
    handler.act();
    }

    public static void main( String [] args )
    {
    ScreenData screen = new ScreenData();

    ScreenHandler handler = new ScreenHandler();
    handler.setAlfa( false );
    handler.handle( screen );

    handler.setAlfa( true );
    handler.handle( screen );
    }
    }
    </sscce>

    Output:

    NUMA: { 1, 2, 3, }
    ALFA: { "1", "2", "3", }

    --
    Lew
    Lew, Dec 9, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nospawn
    Replies:
    2
    Views:
    502
    Real Gagnon
    Apr 25, 2006
  2. Ulf Meinhardt
    Replies:
    0
    Views:
    6,395
    Ulf Meinhardt
    Aug 10, 2006
  3. Prasoon
    Replies:
    1
    Views:
    448
    Andrew Thompson
    Apr 13, 2007
  4. Artur McCennon
    Replies:
    0
    Views:
    387
    Artur McCennon
    Jul 22, 2009
  5. Jochen Brenzlinger
    Replies:
    5
    Views:
    902
    Roedy Green
    Sep 2, 2011
Loading...

Share This Page