[concurrency-interest] LinkedBlockingQueue does not throw ClassCastException

Mike Quilleash mike.quilleash at subexazure.com
Mon Apr 16 15:31:00 EDT 2007


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




More information about the Concurrency-interest mailing list