43 |
|
volatile int anIntField; |
44 |
|
volatile Integer anIntegerField; |
45 |
|
|
46 |
< |
AtomicLongFieldUpdater aLongFieldUpdater() { |
46 |
> |
AtomicLongFieldUpdater<Atomic8Test> aLongFieldUpdater() { |
47 |
|
return AtomicLongFieldUpdater.newUpdater |
48 |
|
(Atomic8Test.class, "aLongField"); |
49 |
|
} |
50 |
|
|
51 |
< |
AtomicIntegerFieldUpdater anIntFieldUpdater() { |
51 |
> |
AtomicIntegerFieldUpdater<Atomic8Test> anIntFieldUpdater() { |
52 |
|
return AtomicIntegerFieldUpdater.newUpdater |
53 |
|
(Atomic8Test.class, "anIntField"); |
54 |
|
} |
501 |
|
() -> aLongFieldUpdater().getAndUpdate(this, null), |
502 |
|
() -> anIntFieldUpdater().getAndUpdate(this, null), |
503 |
|
() -> anIntegerFieldUpdater().getAndUpdate(this, null), |
504 |
– |
////() -> aLongFieldUpdater().getAndUpdate(null, Atomic8Test::addLong17), |
505 |
– |
////() -> anIntFieldUpdater().getAndUpdate(null, Atomic8Test::addInt17), |
506 |
– |
////() -> anIntegerFieldUpdater().getAndUpdate(null, Atomic8Test::addInteger17), |
504 |
|
}; |
505 |
|
assertThrows(NullPointerException.class, throwingActions); |
506 |
|
} |
561 |
|
assertThrows(NullPointerException.class, throwingActions); |
562 |
|
} |
563 |
|
|
564 |
+ |
/** |
565 |
+ |
* Object arguments for parameters of type T that are not |
566 |
+ |
* instances of the class passed to the newUpdater call will |
567 |
+ |
* result in a ClassCastException being thrown. |
568 |
+ |
*/ |
569 |
+ |
public void testFieldUpdaters_ClassCastException() { |
570 |
+ |
// Use raw types to allow passing wrong object type, provoking CCE |
571 |
+ |
final AtomicLongFieldUpdater longUpdater = aLongFieldUpdater(); |
572 |
+ |
final AtomicIntegerFieldUpdater intUpdater = anIntFieldUpdater(); |
573 |
+ |
final AtomicReferenceFieldUpdater refUpdater = anIntegerFieldUpdater(); |
574 |
+ |
final Object obj = new Object(); |
575 |
+ |
for (Object x : new Object[]{ new Object(), null }) { |
576 |
+ |
Runnable[] throwingActions = { |
577 |
+ |
() -> longUpdater.get(x), |
578 |
+ |
() -> intUpdater.get(x), |
579 |
+ |
() -> refUpdater.get(x), |
580 |
+ |
|
581 |
+ |
() -> longUpdater.set(x, 17L), |
582 |
+ |
() -> intUpdater.set(x, 17), |
583 |
+ |
() -> refUpdater.set(x, (Integer) 17), |
584 |
+ |
|
585 |
+ |
() -> longUpdater.addAndGet(x, 17L), |
586 |
+ |
() -> intUpdater.addAndGet(x, 17), |
587 |
+ |
|
588 |
+ |
() -> longUpdater.getAndUpdate(x, y -> y), |
589 |
+ |
() -> intUpdater.getAndUpdate(x, y -> y), |
590 |
+ |
() -> refUpdater.getAndUpdate(x, y -> y), |
591 |
+ |
|
592 |
+ |
() -> longUpdater.compareAndSet(x, 17L, 42L), |
593 |
+ |
() -> intUpdater.compareAndSet(x, 17, 42), |
594 |
+ |
() -> refUpdater.compareAndSet(x, (Integer) 17, (Integer) 42), |
595 |
+ |
}; |
596 |
+ |
assertThrows(ClassCastException.class, throwingActions); |
597 |
+ |
} |
598 |
+ |
} |
599 |
+ |
|
600 |
|
} |