Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

Generics warning message.

Thread view: 
Roedy Green - 30 Dec 2007 12:25 GMT
Is there a way to get rid of this warning message?

E:\com\mindprod\vercheck\VerCheck.java:575: warning: [unchecked]
unchecked cast
found   : java.lang.Object
required: java.util.ArrayList<com.mindprod.vercheck.AppToWatch>
            allRows = (ArrayList<AppToWatch>) ois.readObject();

the code is

ArrayList<AppToWatch> allRows = new ArrayList<AppToWatch>(30);
...
allRows = (ArrayList<AppToWatch>) ois.readObject();
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

External Concepts Guild - 30 Dec 2007 18:12 GMT
On Dec 30, 12:25 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> Is there a way to get rid of this warning message?
>
[quoted text clipped - 12 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com
There might be a way.  I think the following code performs a similar
function to what you are trying to achieve.  The code compiles without
warnings or errors and it runs correctly.  Nonetheless, it leaves
something to be desired.

import java . io . * ;
import java . util . * ;

class test
{
 public static void main ( String [ ] args ) throws Exception
 {
   ObjectInputStream ois = setupExample ( args ) ;
   runExample ( ois ) ;
 }

 /**
  * This just sets up the example.
  * It returns an appropriate ObjectInputStream
  * (i.e. one that has been stuffed with an ArrayList.
  **/
 private static ObjectInputStream setupExample ( String [ ] args )
   throws Exception
 {
   ArrayList < String > list = new ArrayList < String > ( ) ;
   for ( int i = 0 ; i < args . length ; i ++ )
     {
       list . add ( args [ i ] ) ;
     }
   ByteArrayOutputStream bos = new ByteArrayOutputStream ( ) ;
   ObjectOutputStream oos = new ObjectOutputStream ( bos ) ;
   oos . writeObject ( list ) ;
   oos . close ( ) ;
   bos . close ( ) ;
   byte [ ] b = bos . toByteArray ( ) ;
   ByteArrayInputStream bis = new ByteArrayInputStream ( b ) ;
   ObjectInputStream ois = new ObjectInputStream ( bis ) ;
   return ( ois ) ;
 }

 /**
  * This is the example.
  * We think that there is an ArrayList<java.lang.String> in
  * the InputStream.
  * How to cast it out without throwing warnings?
  **/
 private static void runExample ( ObjectInputStream ois ) throws
Exception
 {
   Object object = ois . readObject ( ) ;
   ArrayList list1 = ( ArrayList ) ( object ) ;
   ArrayList < String > list2 = nowarningCast ( String . class ,
list1 ) ;
   for ( String string : list2 )
     {
       System . out . println ( string ) ;
     }
 }

 /**
  * This is how I cast the ArrayList to
  * an ArrayList<java.lang.String> without
  * casting warnings.
  *
  * Take heed that this is not safe.
  **/
 private static < R > ArrayList < R > nowarningCast
   ( Class < R > clazz , ArrayList in )
 {
   ArrayList < R > out = new ArrayList < R > ( ) ;
   for ( Object val : in )
     {
       R r = clazz . cast ( val ) ;
       out . add ( r ) ;
     }
   return ( out ) ;
 }
}

Emory Merryman
External Concepts Guild
Lew - 30 Dec 2007 20:12 GMT
> There might be a way.  I think the following code performs a similar
> function to what you are trying to achieve.  The code compiles without
[quoted text clipped - 13 lines]
>   }
> }

That's not a cast, that's a copy.  But I'll bet you could do a cast with a
Class<ArrayList<R>> with a similar idiom.

Signature

Lew

External Concepts Guild - 31 Dec 2007 16:07 GMT
> > There might be a way.  I think the following code performs a similar
> > function to what you are trying to achieve.  The code compiles without
[quoted text clipped - 19 lines]
> --
> Lew

You are absolutely correct:  it is a copy not a cast.  I believe that
because of the nature of type erasure <http://java.sun.com/docs/books/
tutorial/java/generics/erasure.html> it is impossible to do a cast
(without at least generating a warning).  Thus to achieve the effect
that the OP desires, it is necessary to cheat (e.g. copying rather
than casting).

If you have control of the writing operation as well as the reading,
then you could solve this problem by using a nonparameterized
extension of ArrayList<R>.  For example, consider class MyArrayList
extends ArrayList<String> { }.  If you wrote a MyArrayList, then you
should be able to read it and cast it without warnings.
Zig - 31 Dec 2007 20:49 GMT
> You are absolutely correct:  it is a copy not a cast.  I believe that
> because of the nature of type erasure <http://java.sun.com/docs/books/
> tutorial/java/generics/erasure.html> it is impossible to do a cast
> (without at least generating a warning).  Thus to achieve the effect
> that the OP desires, it is necessary to cheat (e.g. copying rather
> than casting).

If I may rephrase your answer here with a bit of code; if we had a write  
method doing something like:

ObjectOutputStream out=...
ArrayList<FooBar> objs=new ArrayList<FooBar>();
objs.add(new FooBar());
out.writeObject(objs);

And later we read that back as:

ObjectInputStream in=...
ArrayList<AppToWatch> objs=(ArrayList<AppToWatch>) in.readObject();

Then the cast would succeed, but iterating through the list after it had  
been read would throw spurious ClassCastExceptions (hence the warning).

> If you have control of the writing operation as well as the reading,
> then you could solve this problem by using a nonparameterized
> extension of ArrayList<R>.  For example, consider class MyArrayList
> extends ArrayList<String> { }.  If you wrote a MyArrayList, then you
> should be able to read it and cast it without warnings.

Reading each object individually is a good solution, as it causes you to  
validate each object by casting it. Having a implementation with a fixed  
type works too: it makes one *wish* Collections.checkedList would return a  
public type, so you could ensure that

CheckedList.getCheckedType()==AppToWatch.class

But, the easiest solution to remove the warning is probably to decorate  
the read method with

@SuppressWarnings("unchecked")

Obviously, this works when you can safely say that the content of the  
input stream only comes from well known and trusted write methods (as it  
is not verifying the contents of the list for type accuracy).

HTH,

-Zig


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.