[Fwd: Code review request: 7072353 JNDI libraries do not build with javac -Xlint:all -Werror]

Alexandre Boulgakov alexandre.boulgakov at oracle.com
Wed Aug 3 18:06:11 UTC 2011


On 8/3/2011 10:51 AM, Dr Andrew John Hughes wrote:
> On 09:24 Wed 03 Aug     , Joe Darcy wrote:
>> On 8/3/2011 12:42 AM, David Holmes wrote:
>>> Alexandre Boulgakov said the following on 08/03/11 04:44:
>>>> On 8/2/2011 2:19 AM, Xuelei Fan wrote:
>>>>> 3017 Vector<Object>   temp = (Vector)extractURLs(res.errorMessage);
>>>>>      You may not need the conversion any more, the return value of
>>>>> extractURLs() has been updated to
>>>>>      2564     private static Vector<String>   extractURLs(String
>>>>> refString)
>>>> The cast is needed to go from Vector<String>  to Vector<Object>.
>>> Raw types should be avoided (here and elsewhere there are casts to raw
>>> Vector). I'm surprised (generics continue to surprise me) that despite
>>> all our advances in type-inference etc that the compiler can not tell
>>> that a Vector<T>  is-a Vector<Object>. :(
>> That is because in general a Vector<T>  is not a Vector<Object>  because
>> of the way subtyping works.  As with arrays, it all looks fine until you
>> want to change the container; consider
>>
>> Vector<String>  vs = new Vector<>();
>> ...
>> Vector<Object>  vo = vs; // Assume this was okay to alias an object
>> vector and a string vector
>>
>> vo.add(new Integer(1));  // Add an Integer to a list of strings, boom!
>>
>> Using wildcards makes the subtyping work along the type argument axis.
>>
> Exactly.  What I wondered on reading this is why it needs to be cast to a
> Vector<Object>  anyway.  That would only reduce type safety as you say.
The current code uses it to store Strings and Vector<String>s. The most 
specific common base type is Object, so I don't think we can do any 
better than that.
>
>> -Joe
>>



More information about the core-libs-dev mailing list