Skip to main content



      Home
Home » Newcomers » Newcomers » Many Warnings in Eclipse Tutorial Code
Many Warnings in Eclipse Tutorial Code [message #255224] Thu, 01 May 2008 15:11 Go to next message
Eclipse UserFriend
Originally posted by: Bob.Davidson.uticanational.com

I recently installed Eclipse version 3.3.2.
I am currently taking the Java Development Tutorial from the Eclipse Help
Contents.

The following is a piece of code from a file called VectorTest.java from
within the JUnit project in the tutorial. JUnit was downloaded from
Eclipse.org.

1. public class VectorTest extends TestCase {
2. protected Vector fEmpty;
3. protected Vector fFull;
4.
5. public static void main (String[] args) {
6. junit.textui.TestRunner.run (suite());
7. }
8. protected void setUp() {
9. fEmpty= new Vector();
10. fFull= new Vector();
11. fFull.addElement(new Integer(1));
12. fFull.addElement(new Integer(2));
13. fFull.addElement(new Integer(3));
14. }

Lines 2, 3, 9, and 10 generate the following warning:
Vector is a raw type. References to generic type Vector<E> should be
parameterized.

Lines 11, 12, and 13 generate the following warning:
Type safety: The method addElement(Object) belongs to the raw type
Vector. References to generic type Vector<E> should be parameterized.

I have two questions:
1) Why would sample code in the tutorial generate so many warnings? It
seems that the large number of warnings in the sample project tends to
make the concept of warnings somewhat useless.

2) How can I modify the code to eliminate these warnings?
-------
Any help on this would be greatly appreciated.
Re: Many Warnings in Eclipse Tutorial Code [message #255228 is a reply to message #255224] Thu, 01 May 2008 15:33 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

Bob,

Comments below.


Bob Davidson wrote:
> I recently installed Eclipse version 3.3.2.
> I am currently taking the Java Development Tutorial from the Eclipse
> Help Contents.
>
> The following is a piece of code from a file called VectorTest.java
> from within the JUnit project in the tutorial. JUnit was downloaded
> from Eclipse.org.
>
> 1. public class VectorTest extends TestCase {
> 2. protected Vector fEmpty;
> 3. protected Vector fFull;
> 4.
> 5. public static void main (String[] args) {
> 6. junit.textui.TestRunner.run (suite());
> 7. }
> 8. protected void setUp() {
> 9. fEmpty= new Vector();
> 10. fFull= new Vector();
> 11. fFull.addElement(new Integer(1));
> 12. fFull.addElement(new Integer(2));
> 13. fFull.addElement(new Integer(3));
> 14. }
>
> Lines 2, 3, 9, and 10 generate the following warning:
> Vector is a raw type. References to generic type Vector<E> should be
> parameterized.
Welcome to Java 5.0.
>
> Lines 11, 12, and 13 generate the following warning:
> Type safety: The method addElement(Object) belongs to the raw type
> Vector. References to generic type Vector<E> should be parameterized.
>
> I have two questions:
> 1) Why would sample code in the tutorial generate so many warnings? It
> seems that the large number of warnings in the sample project tends
> to make the concept of warnings somewhat useless.
The warnings themselves are configurable via Java preferences either
globally or with project specific settings. Or, if you set the whole
project to use Java 1.4 source compatibility, all these warnings will
disappear, because they are only applicable for Java 5.0.
>
> 2) How can I modify the code to eliminate these warnings?
One way is to add @SuppressWarnings("unchecked"); there are quick fixes
to do this easily per warning. A better way is to use Vector<Integer>;
there are wizards to help deduce these things...

Of course if you aren't interested in generics at all, switch back to
1.4 source compatibilities.
> -------
> Any help on this would be greatly appreciated.
>
>
Re: Many Warnings in Eclipse Tutorial Code [message #255232 is a reply to message #255228] Thu, 01 May 2008 16:12 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Bob.Davidson.uticanational.com

Thank you for solving my problem.

It's nice to know that I can set the various warnings in Java preferences.

Also, It's good to know how to modify the code to remove those warnings.

I guess I'm not so sure if I'm going to like Java 5.0. I'm an old C
programmer, and I don't care to much for languages that think I don't know
what I am doing, or think I am too stupid to use the correct data types.

I modified the code in question, and the warnings disappeared.

I couldn't fix the following code, though (at least not yet.)

This code
1. public void testClone() {
2. Vector clone= (Vector) fFull.clone();
3. assertTrue(clone.size() == fFull.size());
4. assertTrue(clone.contains(new Integer(1)));
5. }

produced these two warnings for line 2:
Vector is a raw type. References to generic type Vector<E> should be
parameterized.
Vector is a raw type. References to generic type Vector<E> should be
parameterized.

So, I changed the code to this:

1. public void testClone() {
2. Vector<Integer> clone= (Vector<Integer>) fFull.clone();
3. assertTrue(clone.size() == fFull.size());
4. assertTrue(clone.contains(new Integer(1)));
5. }

and then I got the following warning on line 2:
Type Safety: Unchecked cast from Object to Vector<Integer>

I guess I'll just keep on keeping track of my own type safety.

Thanks again for your help. It was most useful!
Re: Many Warnings in Eclipse Tutorial Code [message #255240 is a reply to message #255232] Thu, 01 May 2008 16:46 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

Bob,

Comments below.

Bob Davidson wrote:
> Thank you for solving my problem.
>
> It's nice to know that I can set the various warnings in Java
> preferences.
>
> Also, It's good to know how to modify the code to remove those warnings.
>
> I guess I'm not so sure if I'm going to like Java 5.0.
I sure didn't like it at first. The joys of Vector<Object>, Vector<?
extends Object>, Vector<?> all being subtly different still await you.
> I'm an old C programmer, and I don't care to much for languages that
> think I don't know what I am doing, or think I am too stupid to use
> the correct data types.
I wouldn't interpret it that way though. What's nice about generics is
it helps you catch type errors at compile time rather than at runtime.
>
> I modified the code in question, and the warnings disappeared.
>
> I couldn't fix the following code, though (at least not yet.)
>
> This code
> 1. public void testClone() {
> 2. Vector clone= (Vector) fFull.clone(); 3.
> assertTrue(clone.size() == fFull.size());
> 4. assertTrue(clone.contains(new Integer(1)));
> 5. }
>
> produced these two warnings for line 2:
> Vector is a raw type. References to generic type Vector<E> should be
> parameterized.
> Vector is a raw type. References to generic type Vector<E> should be
> parameterized.
>
> So, I changed the code to this:
>
> 1. public void testClone() {
> 2. Vector<Integer> clone= (Vector<Integer>) fFull.clone();
> 3. assertTrue(clone.size() == fFull.size());
> 4. assertTrue(clone.contains(new Integer(1)));
> 5. }
>
> and then I got the following warning on line 2:
> Type Safety: Unchecked cast from Object to Vector<Integer>
There's actually no way to make that error go away except to use
@SuppressWarnings("unchecked"). The reason for this warning is that the
cast does not actually walk through the Vector and ensure that each
element in the fact is of type integer. Well, I suppose what I say
isn't entirely true, since I'm pretty sure you can use Vector<?> in this
case.
>
> I guess I'll just keep on keeping track of my own type safety.
I've actually grown quite fond of generics after my first feeling of it
being a truly a horror. It requires a great deal of re-educating your
intuition and being kind of a programming language junky, I felt it
should have been easier to learn than it was. But, as with all things,
what seems horribly complex when you first learn it, starts to seem
simple and obvious later on. (I still have a hard time with "? super T"
mostly because I've just not needed to use it except for some mind benders.)
> Thanks again for your help. It was most useful!
>
>
Re: Many Warnings in Eclipse Tutorial Code [message #255290 is a reply to message #255240] Fri, 02 May 2008 05:33 Go to previous messageGo to next message
Eclipse UserFriend
> There's actually no way to make that error go away except to use
> @SuppressWarnings("unchecked"). The reason for this warning is that the
> cast does not actually walk through the Vector and ensure that each
> element in the fact is of type integer. Well, I suppose what I say
> isn't entirely true, since I'm pretty sure you can use Vector<?> in this
> case.

I don't completely understand that.

vector.clone() has "Object" as its return signature. The only way for the
compiler to check the cast is to look into the method body of vector.clone and
derive the actual type of the returned value? I had thought that the compiler
only looks at the method signature..


Felix
Re: Many Warnings in Eclipse Tutorial Code [message #255292 is a reply to message #255290] Fri, 02 May 2008 05:58 Go to previous messageGo to next message
Eclipse UserFriend
I found quite a good Faq on internals...

http://www.angelikalanger.com/GenericsFAQ/FAQSections/Techni calDetails.html#Compiler%20Messages
Re: Many Warnings in Eclipse Tutorial Code [message #255315 is a reply to message #255290] Fri, 02 May 2008 07:17 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

Felix,

Comments below.

Felix Dorner wrote:
>
>> There's actually no way to make that error go away except to use
>> @SuppressWarnings("unchecked"). The reason for this warning is that
>> the cast does not actually walk through the Vector and ensure that
>> each element in the fact is of type integer. Well, I suppose what I
>> say isn't entirely true, since I'm pretty sure you can use Vector<?>
>> in this case.
>
> I don't completely understand that.
>
> vector.clone() has "Object" as its return signature.
Yes, so you'd need to cast it as has always been the case. After all,
the clone might be an instance a type derived from the object's apparent
static type...
> The only way for the compiler to check the cast is to look into the
> method body of vector.clone and derive the actual type of the returned
> value?
Cast doesn't ever look in method bodies. The best it can do is call
object.getClass() to get the class and do some testing on that.
> I had thought that the compiler only looks at the method signature..
Yes, the compiler only looks at static types, so it knows clone is
returning java.lang.Object. You can cast to (Vector) and it will
complain about using a raw type. You can cast to (Vector<Integer>) and
it will complain that at runtime, it's only going to check the
object.getClass() to confirm it's a Vector, but it's not going to do
anything to confirm that each object in the Vector is an Integer, so it
warns you at compile time that you're making an unsafe assumption. Of
course at runtime, any time you try to pull out an object from
Vector<Integer>, an implicit cast is inserted and if the object isn't an
Integer, you'll get a runtime failure, but that comes later, not at the
point of the Vector<Integer> cast. And finally, you can cast to
(Vector<?>), which is like the first case, but no complaints about raw
types. Any data you pull out of this Vector will be considered to have
type java.lang.Object; you'd need to cast to make it an Integer. You
won't be able to put any data into the Vector (except null, because
that's a value of every type).
>
>
> Felix
Re: Many Warnings in Eclipse Tutorial Code [message #255342 is a reply to message #255290] Fri, 02 May 2008 10:27 Go to previous message
Eclipse UserFriend
Originally posted by: eclipse-news.rizzoweb.com

Felix Dorner wrote:
>
>> There's actually no way to make that error go away except to use
>> @SuppressWarnings("unchecked"). The reason for this warning is that
>> the cast does not actually walk through the Vector and ensure that
>> each element in the fact is of type integer. Well, I suppose what I
>> say isn't entirely true, since I'm pretty sure you can use Vector<?>
>> in this case.
>
> I don't completely understand that.
>
> vector.clone() has "Object" as its return signature. The only way for
> the compiler to check the cast is to look into the method body of
> vector.clone and derive the actual type of the returned value? I had
> thought that the compiler only looks at the method signature..

Most non-intuitive aspects of Java parameterized types (other than the
ugly syntax) can be traced back to its use of type erasure. It is a
fundamentally flawed implementation of the "generics" concept. Bruce
Eckel has written a lot about it, as have others. For example,
http://www.mindview.net/WebLog/log-0050

Eric
Previous Topic:Auto execute a script before running an application within eclipse
Next Topic:double hints
Goto Forum:
  


Current Time: Thu Jun 05 02:10:53 EDT 2025

Powered by FUDForum. Page generated in 0.07947 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top