38 |
|
} |
39 |
|
|
40 |
|
public void checkCompareAndSetProtectedSub() { |
41 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
42 |
< |
a = updaterFor("protectedField"); |
43 |
< |
protectedField = 1; |
41 |
> |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = |
42 |
> |
AtomicIntegerFieldUpdater.newUpdater |
43 |
> |
(AtomicIntegerFieldUpdaterTest.class, "protectedField"); |
44 |
> |
this.protectedField = 1; |
45 |
|
assertTrue(a.compareAndSet(this, 1, 2)); |
46 |
|
assertTrue(a.compareAndSet(this, 2, -4)); |
47 |
|
assertEquals(-4, a.get(this)); |
53 |
|
} |
54 |
|
|
55 |
|
static class UnrelatedClass { |
56 |
< |
public void checkPrivateAccess() { |
57 |
< |
Exception ex = null; |
56 |
> |
public void checkPackageAccess(AtomicIntegerFieldUpdaterTest obj) { |
57 |
> |
obj.x = 72; |
58 |
> |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = |
59 |
> |
AtomicIntegerFieldUpdater.newUpdater |
60 |
> |
(AtomicIntegerFieldUpdaterTest.class, "x"); |
61 |
> |
assertEquals(72, a.get(obj)); |
62 |
> |
assertTrue(a.compareAndSet(obj, 72, 73)); |
63 |
> |
assertEquals(73, a.get(obj)); |
64 |
> |
} |
65 |
> |
|
66 |
> |
public void checkPrivateAccess(AtomicIntegerFieldUpdaterTest obj) { |
67 |
|
try { |
68 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = |
69 |
|
AtomicIntegerFieldUpdater.newUpdater |
70 |
< |
(AtomicIntegerFieldUpdaterTest.class, "x"); |
71 |
< |
} catch (RuntimeException rex) { |
72 |
< |
ex = rex; |
70 |
> |
(AtomicIntegerFieldUpdaterTest.class, "privateField"); |
71 |
> |
throw new AssertionError("should throw"); |
72 |
> |
} catch (RuntimeException success) { |
73 |
> |
assertNotNull(success.getCause()); |
74 |
|
} |
64 |
– |
if (ex != null) throw new Error(); |
75 |
|
} |
76 |
|
} |
77 |
|
|
122 |
|
} |
123 |
|
|
124 |
|
/** |
125 |
< |
* construction from unrelated class throws RuntimeException |
125 |
> |
* construction from unrelated class; package access is allowed, |
126 |
> |
* private access is not |
127 |
|
*/ |
128 |
|
public void testUnrelatedClassAccess() { |
129 |
< |
UnrelatedClass s = new UnrelatedClass(); |
130 |
< |
s.checkPrivateAccess(); |
129 |
> |
new UnrelatedClass().checkPackageAccess(this); |
130 |
> |
new UnrelatedClass().checkPrivateAccess(this); |
131 |
|
} |
132 |
|
|
133 |
|
/** |