Re: Generics warning message.



On Dec 30, 12:25 pm, Roedy Green <see_webs...@xxxxxxxxxxxxxxxxxxxx>
wrote:
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();
--
Roedy Green Canadian Mind Products
The Java Glossaryhttp://mindprod.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQFHd99XVQdj5Q2e9q0RAgPmAJ9rPJDnt0ebyjVFd2hCLVCU0sjJQACdE7Aj
hniuYd99CXmdjmT1JVk5w+o=
=2Un1
-----END PGP SIGNATURE-----

.



Relevant Pages

  • Re: void * vs char *
    ... That doesn't mean never cast, but it does mean that if the compiler is warning you, you should generally assume it's right. ... and not some optional lint tool to pick through my code for places ... If you don't like your compiler being helpful, either turn off or ignore the warnings. ...
    (comp.lang.c)
  • Re: shame on MISRA
    ... of warnings about "explicit cast required" for assignments and ... with a cast. ... GCC with no warnings. ...
    (comp.arch.embedded)
  • Re: Generics and ClassLoaders
    ... to Classless so - after all I am explicitly casting. ... It's unfortunately not really possible to develop with generics and warnings for unchecked casts enabled, without just ignoring certain compiler warnings. ... I've returned to just using Classand using a cast at newInstance. ...
    (comp.lang.java.programmer)
  • Re: Generics warning message.
    ... The code compiles without ... That's not a cast, that's a copy. ... that the OP desires, it is necessary to cheat (e.g. copying rather ... should be able to read it and cast it without warnings. ...
    (comp.lang.java.programmer)
  • Re: Type-casting void pointers?
    ... There are some lousy compilers out there. ... delivering code with hundreds of warnings is not good for business). ... The cast, where it is obvious, can also be used as documentation: ...
    (comp.lang.c)