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 / November 2005

Tip: Looking for answers? Try searching our database.

Old code, collections and generics warning

Thread view: 
Nicolai Henriksen - 08 Nov 2005 13:21 GMT
Hi,

I have some old java code that use the collection classes. Now compiling
with 1.5 I get warnings like

   warning: [unchecked] unchecked call to add(E) as a member of the raw
type java.util.List

My question is: Is there a way to avoid these warnings? Is there a way
to use 1.5 collection classes "the old way" that is without caring about
generics?

I like the 1.4 style of coding with collections. I do not like generics
and do not want to use them at all but is this possible with 1.5
collections?

I know I can just ignore the warnings but I am generally not interested
in ignoring warnings - I just do not see these warnings as meaningfull.
If it worked ok on 1.4 it will still work on 1.5 right?

 - Nicolai
Roedy Green - 08 Nov 2005 13:30 GMT
On Tue, 08 Nov 2005 14:21:22 +0100, Nicolai Henriksen
<nicolai@drengene.dk> wrote, quoted or indirectly quoted someone who
said :

>My question is: Is there a way to avoid these warnings? Is there a way
>to use 1.5 collection classes "the old way" that is without caring about
>generics?

Yes. But you have to ignore the warnings.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Nicolai Henriksen - 08 Nov 2005 13:33 GMT
:-(  It messes up our nightly build so I cannot live with the warnings.
Is there any way to suppress this exact warning without supress all
warnings?

 - Nicolai

> On Tue, 08 Nov 2005 14:21:22 +0100, Nicolai Henriksen
> <nicolai@drengene.dk> wrote, quoted or indirectly quoted someone who
[quoted text clipped - 5 lines]
>
> Yes. But you have to ignore the warnings.
Roedy Green - 08 Nov 2005 14:31 GMT
On Tue, 08 Nov 2005 14:33:05 +0100, Nicolai Henriksen
<nicolai@drengene.dk> wrote, quoted or indirectly quoted someone who
said :

>Is there any way to suppress this exact warning without supress all
>warnings?

It's coming.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Oliver Wong - 08 Nov 2005 17:14 GMT
> On Tue, 08 Nov 2005 14:33:05 +0100, Nicolai Henriksen
> <nicolai@drengene.dk> wrote, quoted or indirectly quoted someone who
[quoted text clipped - 4 lines]
>
> It's coming.

   What Roedy is referring to is the @SuppressWarning annotation which Sun
has defined, but hasn't implemented in their javac compiler yet. If your
build system is aware of this annotation, then you can use it right away
(Eclipse, for example, is aware of this annotation). If your build system
uses Sun's JavaC compiler, then you'll have to wait for Sun to implement it.

   - Oliver
Nicolai Henriksen - 09 Nov 2005 08:00 GMT
Hm no luck for me then. We are using ant and even with -Xlint:-unchecked
ant marks it as an error in the html output - not even as an warning.

It is hard for me to believe (although I am beginning to) that sun has
removed this excellent feature from java (the ability to do collections
in a weakly typed way). Sigh.

 - Nicolai

>>On Tue, 08 Nov 2005 14:33:05 +0100, Nicolai Henriksen
>><nicolai@drengene.dk> wrote, quoted or indirectly quoted someone who
[quoted text clipped - 13 lines]
>
>     - Oliver
HalcyonWild - 09 Nov 2005 10:57 GMT
> Hm no luck for me then. We are using ant and even with -Xlint:-unchecked
> ant marks it as an error in the html output - not even as an warning.
>
> It is hard for me to believe (although I am beginning to) that sun has
> removed this excellent feature from java (the ability to do collections
> in a weakly typed way). Sigh.

Try Collection c = new Vector<Object>(); or something like that.
AFAIK, you can create a Vector of Strings, or Vector of Integers, with
Generics.
So you can definitely create a Vector of Objects. And continue with it
the old way. Am I right. See if that works, its some kind of a bad
workaround though. I dont know if it is a good thing to do.

I am not sure about syntax. I have never used jdk 1.5 ( and I dont
intend to, unless the bosses decide to shift to 1.5 ).
Nicolai Henriksen - 09 Nov 2005 13:12 GMT
Oh yes. That might let me continue the coding style although still
polluting the code a little. But definitely worth a try. Thanks.

 - Nicolai

> Try Collection c = new Vector<Object>(); or something like that.
> AFAIK, you can create a Vector of Strings, or Vector of Integers, with
[quoted text clipped - 5 lines]
> I am not sure about syntax. I have never used jdk 1.5 ( and I dont
> intend to, unless the bosses decide to shift to 1.5 ).
Oliver Wong - 09 Nov 2005 15:40 GMT
> Hm no luck for me then. We are using ant and even with -Xlint:-unchecked
> ant marks it as an error in the html output - not even as an warning.
>
> It is hard for me to believe (although I am beginning to) that sun has
> removed this excellent feature from java (the ability to do collections in
> a weakly typed way). Sigh.

   I wouldn't say they removed this "feature"; rather, they're just
discouraging it (which is why it's reported as a warning and not an error).

   - Oliver
Monique Y. Mudama - 09 Nov 2005 18:25 GMT
> Hm no luck for me then. We are using ant and even with
> -Xlint:-unchecked ant marks it as an error in the html output - not
[quoted text clipped - 3 lines]
> has removed this excellent feature from java (the ability to do
> collections in a weakly typed way). Sigh.

It sounds more like you have a project policy or build system that
disallows warnings, and *that* is the problem?

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Nicolai Henriksen - 10 Nov 2005 09:04 GMT
He he. Well I do not mind the warnings that much. The thing is that ant
flags them as errors making it impossible to see with a quick glance
whether the nightly build failed or not.

 - Nicolai

> It sounds more like you have a project policy or build system that
> disallows warnings, and *that* is the problem?
Oliver Wong - 10 Nov 2005 15:42 GMT
> He he. Well I do not mind the warnings that much. The thing is that ant
> flags them as errors making it impossible to see with a quick glance
> whether the nightly build failed or not.

   So use a better tool than Ant?

   I haven't used Ant myself, but I hear it's a good product. Are you sure
there aren't some sort of configuration setting or something that you can't
edit so that Ant treats warnings as warnings instead of as errors?

   - Oliver
Nicolai Henriksen - 11 Nov 2005 08:11 GMT
He he. Well otherwise we are very satisfied with Ant. It even does our
C++ build very efficiently and on multiple platforms one of which is
embedded cross-compile - all with exactly one build system across.

But even so. They actually have a bug in their bug-tracking system
stating this exact problem. Due to the very general nature of the way
ant handles "tools" everything on stderr is treated as an error as far
as I know.

But I think we will go for some king of post-processing of the output -
we are running the output through XSLT anyway so it might be safe to
filter out the "Note:" lines.

So configuration? Well yes. Just configuration of the kind have a .java
or .xsl extension :-)

 - Nicolai

>     So use a better tool than Ant?
>
[quoted text clipped - 3 lines]
>
>     - Oliver
Bjorn Abelli - 08 Nov 2005 14:47 GMT
"Nicolai Henriksen" wrote...

> I have some old java code that use the collection classes.
> Now compiling with 1.5 I get warnings like
[quoted text clipped - 5 lines]
> Is there a way to use 1.5 collection classes "the old way"
> that is without caring about generics?

If you don't use anything else of the features in 1.5 either (e.g.
foreach-loops, etc), you can suppress the warnings with the compiler switch

 -source 1.4

But then you'll probably walk into troble somewhere in the future where some
developer begins to use 1.5 features.

There's also the possibility to use the switch

 -Xlint:-unchecked

The latter suppresses the specific warnings but you'll still get a
message...

 Note: C:\X\Y.java uses unchecked or unsafe operations.
 Note: Recompile with -Xlint:unchecked for details.

// Bjorn A
Thomas G. Marshall - 08 Nov 2005 15:19 GMT
Nicolai Henriksen coughed up:
> Hi,
>
[quoted text clipped - 11 lines]
> and do not want to use them at all but is this possible with 1.5
> collections?

If this is an issue of /comfort/ then I don't believe anyone without similar
prior experience is comfortable reading through generics laden source.  You
are most likely going to be faced with large amounts of it sooner or later.
Bite the bullet now.

> I know I can just ignore the warnings but I am generally not interested
> in ignoring warnings - I just do not see these warnings as meaningfull.
> If it worked ok on 1.4 it will still work on 1.5 right?
>
>  - Nicolai

Signature

Doesn't /anyone/ know where I can find a credit card company that emails me
the minute something is charged to my account?



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.