[concurrency-interest] LinkedBlockingQueue does not throw ClassCastException

David Holmes dcholmes at optusnet.com.au
Mon Apr 16 18:18:37 EDT 2007


And you'll get the ClassCastException when you attempt to extract the String
as an Integer.

The line in Collection.add that says "... others will impose restrictions on
the type of elements that may be added." is not referring to Generics, but
specific concrete implementations that might impose their own direct
instanceof checks  eg pre-generic ListInteger, SetString etc.

David Holmes

> -----Original Message-----
> From: concurrency-interest-bounces at cs.oswego.edu
> [mailto:concurrency-interest-bounces at cs.oswego.edu]On Behalf Of Mike
> Quilleash
> Sent: Tuesday, 17 April 2007 5:31 AM
> To: Szabolcs Ferenczi; Concurrency-interest at cs.oswego.edu
> Subject: Re: [concurrency-interest] LinkedBlockingQueue does not throw
> ClassCastException
>
>
> Generics only offer compile-time checking.  During compilation the
> <Integer> of the LBQ is "erased" from the class definition so the
> internal implementation has no knowledge of what type the LBQ was
> declared as.  Just pretend at runtime all generic method
> parameters/returns and fields become Object.  The only way to achieve
> what you are asking would be to explicitly pass in Integer.class to the
> LBQ during constructor and have it do a clazz.isInstance() check for
> each add.
>
> I imagine the setUp() line is throwing a compile warning about
> unsafe/unchecked assignment indicating you have lost the compile-time
> type-safety of the <Integer>.
>
> HTH.
>
> -----Original Message-----
> From: concurrency-interest-bounces at cs.oswego.edu
> [mailto:concurrency-interest-bounces at cs.oswego.edu] On Behalf Of
> Szabolcs Ferenczi
> Sent: 16 April 2007 20:00
> To: Concurrency-interest at cs.oswego.edu
> Subject: [concurrency-interest] LinkedBlockingQueue does not throw
> ClassCastException
>
> I am trying to test LinkedBlockingQueue and I cannot get the class cast
> test for method add through:
>
> public class lbqTest {
>
>     LinkedBlockingQueue q;
>
>     @Before
>     public void setUp() {
> 	q = new LinkedBlockingQueue<Integer>(3);
>     }
>
>     @Test (expected=ClassCastException.class)
>     public void seqAddNonmatchingArg() {
> 	q.add(new String("x"));
>     }
> ...
> }
>
> What I receive is this:
>
> There was 1 failure:
> 1) seqAddNonmatchingArg(lbqTest.lbqTest)
> java.lang.AssertionError: Expected exception:
> java.lang.ClassCastException
>
> I would expect a successful test according to the documentation of the
> method.
>
> What is wrong? How can I get the test pass?
>
> Best Regards,
> Szabolcs
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at altair.cs.oswego.edu
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>
>  This e-mail is bound by the terms and conditions described at
http://www.subexazure.com/mail-disclaimer.html


_______________________________________________
Concurrency-interest mailing list
Concurrency-interest at altair.cs.oswego.edu
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest



More information about the Concurrency-interest mailing list