Old code, collections and generics warning

  • Thread starter Nicolai Henriksen
  • Start date
N

Nicolai Henriksen

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
 
R

Roedy Green

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

Nicolai Henriksen

:-( 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
 
B

Bjorn Abelli

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

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
 
T

Thomas G. Marshall

Nicolai Henriksen coughed up:
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?

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

Oliver Wong

Roedy Green said:
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
 
N

Nicolai Henriksen

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
 
H

HalcyonWild

Nicolai said:
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 ).
 
N

Nicolai Henriksen

Oh yes. That might let me continue the coding style although still
polluting the code a little. But definitely worth a try. Thanks.

- Nicolai
 
O

Oliver Wong

Nicolai Henriksen said:
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
 
M

Monique Y. Mudama

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.

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

Nicolai Henriksen

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
 
O

Oliver Wong

Nicolai Henriksen said:
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
 
N

Nicolai Henriksen

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
 

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

Forum statistics

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

Latest Threads

Top