Please explain JDK 1.5 generic type warning

D

Don Leckie

Hello,

Could someone please explain the JDK 1.5 warning below? It appears every
readObject( ) for a list. I've tried several things, but cannot correct the
code to eliminate the warning.

Sample code:

private List<TLoopingData> m_loopingData;
.. . .
m_loopingData = (ArrayList<TLoopingData>) reader.readObject( );

The warning:
Type safety: The cast from Object to ArrayList<TLoopData> is actually
checking against the erased type ArrayList.

Thank you,
Don
 
T

Thomas Hawtin

Don said:
Could someone please explain the JDK 1.5 warning below? It appears every
readObject( ) for a list. I've tried several things, but cannot correct the
code to eliminate the warning.

Sample code:

private List<TLoopingData> m_loopingData;
. . .
m_loopingData = (ArrayList<TLoopingData>) reader.readObject( );

The warning:
Type safety: The cast from Object to ArrayList<TLoopData> is actually
checking against the erased type ArrayList.

On Sun's compiler you should get a warning like:

Test.java:9: warning: [unchecked] unchecked cast
found : java.lang.Object
required: java.util.List<java.lang.String>
data = (List<String>)in.readObject();
^
1 warning

which should give you enough to google with.

The cast cannot be checked a runtime, therefore a warning is given. With
an up to date compiler, you can suppress the warning using
@SuppressWarnings("unchecked").

The problem is unavoidable with readObject, so I suggest writing a small
method to wrap up the nastiness in. Just so long as you know it is there.

import static com.mycompany.myproject.serial.Serial.readObject;
....
data = readObject(in);
....

package com.mycompany.myproject.serial;

/** blah */
public class Serial {
/** blah */
@SuppressWarnings("unchecked")
public static <T> T readObject(
java.io_ObjectInputStream in
) throws java.io.IOException, java.lang.ClassNotFoundException {
return (T)in.readObject();
}
}

Tom Hawtin
 
D

Don Leckie

Hi Tom,

Thanks for the info!

Don



Thomas Hawtin said:
Don said:
Could someone please explain the JDK 1.5 warning below? It appears every
readObject( ) for a list. I've tried several things, but cannot correct
the
code to eliminate the warning.

Sample code:

private List<TLoopingData> m_loopingData;
. . .
m_loopingData = (ArrayList<TLoopingData>) reader.readObject( );

The warning:
Type safety: The cast from Object to ArrayList<TLoopData> is actually
checking against the erased type ArrayList.

On Sun's compiler you should get a warning like:

Test.java:9: warning: [unchecked] unchecked cast
found : java.lang.Object
required: java.util.List<java.lang.String>
data = (List<String>)in.readObject();
^
1 warning

which should give you enough to google with.

The cast cannot be checked a runtime, therefore a warning is given. With
an up to date compiler, you can suppress the warning using
@SuppressWarnings("unchecked").

The problem is unavoidable with readObject, so I suggest writing a small
method to wrap up the nastiness in. Just so long as you know it is there.

import static com.mycompany.myproject.serial.Serial.readObject;
...
data = readObject(in);
...

package com.mycompany.myproject.serial;

/** blah */
public class Serial {
/** blah */
@SuppressWarnings("unchecked")
public static <T> T readObject(
java.io_ObjectInputStream in
) throws java.io.IOException, java.lang.ClassNotFoundException {
return (T)in.readObject();
}
}

Tom Hawtin
 

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,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top