'warning: [unchecked]' wrt java.util.List getSubplots()

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
RichardWest
Posts: 844
Joined: Fri Oct 13, 2006 9:29 pm
Location: Sunnyvale, CA

'warning: [unchecked]' wrt java.util.List getSubplots()

Post by RichardWest » Wed Oct 31, 2007 6:07 am

I am trying to extend the various combined plot classes (e.g. CombinedRangeXYPlot) to add functionality. Part of this functionality requires me to create an ArrayList containing the combined plot's subplots. When compiling with jdk-1.5.0_05 (Java 5), I am getting the following warning:

Code: Select all

warning: [unchecked] unchecked call to ArrayList(java.util.Collection<? extends E>) as a member of the raw type java.util.ArrayList
        ArrayList l_subplots = new ArrayList(getSubplots());
I know what the warning is, what it means, and how I normally address them. However, I do not know how to fix the warning in this case since getSubplots() returns a raw java.util.List. Without changing the JFreeChart source (we need to keep it backwards compilable, how can I address this warning? Before you suggest it, suppressing the warning is not an option.
Richard West
Design Engineer II
Advanced Micro Devices
Sunnyvale, CA

jwenting
Posts: 157
Joined: Sat Jul 15, 2006 7:46 am

Post by jwenting » Wed Oct 31, 2007 7:24 am

You can't. There are only 3 options that will get rid of those warnings:
1) genericise the code
2) add the @SuppressWarnings("unchecked") annotation
3) suppress the warning at compiler level using the -Xlint:-unchecked flag.

As you don't want to do any of the above, you'll have to live with it.

dtopolsek
Posts: 8
Joined: Wed Oct 10, 2007 10:46 am
Contact:

Post by dtopolsek » Wed Oct 31, 2007 10:14 am

declare, in fact define your l_subplots ArrayList as ArrayList<? extends E> l_subplost = new ArrayList<? extends E>(getSubplots()).


? helped ?

RichardWest
Posts: 844
Joined: Fri Oct 13, 2006 9:29 pm
Location: Sunnyvale, CA

Post by RichardWest » Wed Oct 31, 2007 6:35 pm

dtopolsek wrote:declare, in fact define your l_subplots ArrayList as ArrayList<? extends E> l_subplost = new ArrayList<? extends E>(getSubplots()).


? helped ?
Of course it does not help. E is a placeholder for either a class or interface (similar to the preverbial T in C++ templates). What you suggested would not even compile.
Richard West
Design Engineer II
Advanced Micro Devices
Sunnyvale, CA

RichardWest
Posts: 844
Joined: Fri Oct 13, 2006 9:29 pm
Location: Sunnyvale, CA

Re: 'warning: [unchecked]' wrt java.util.List getSubplots()

Post by RichardWest » Wed Oct 31, 2007 10:11 pm

RichardWest wrote:

Code: Select all

warning: [unchecked] unchecked call to ArrayList(java.util.Collection<? extends E>) as a member of the raw type java.util.ArrayList
        ArrayList l_subplots = new ArrayList(getSubplots());
It looks like the only way to code this without the warnings is:

Code: Select all

Object[] l_subplots = getSubplots().toArray();
((<class>)l_supplots[<index>]).doSomething();
Trying to pass any type information to the toArray() method throws a warning.
Richard West
Design Engineer II
Advanced Micro Devices
Sunnyvale, CA

jwenting
Posts: 157
Joined: Sat Jul 15, 2006 7:46 am

Post by jwenting » Thu Nov 01, 2007 1:04 pm

logical. As soon as you pass a specific type you're triggering the warning by having a typed collection to which you're adding elements with no specific type in the (1.4 compliant) code.

By telling it to use Object[] you're effectively creating an untyped collection which will fit anything.
That's the problem of mixing generics aware code with pre-generics code. Something has to give, and usually it's the implicit casts and type safety.

Locked