jdk 5 warnings in the jre source code

J

johnmmcparland

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?
 
J

Jan Thomä

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
 
J

johnmmcparland

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.
 
L

Lew

johnmmcparland said:
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.
 
J

johnmmcparland

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.

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?
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

johnmmcparland schreef:
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.

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-----
 
J

johnmmcparland

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

johnmmcparland schreef:


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).
 
R

Roedy Green

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
 
L

Lew

johnmmcparland said:
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", }
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top