| 5 | import org.junit.Test; |
| 6 | |
| 7 | public class UnsafeTest { |
| 8 | |
| 9 | class TestClass { |
| 10 | private volatile Object field = null; |
| 11 | |
| 12 | public Object getField() { |
| 13 | return field; |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | @Test |
| 18 | public void unsafeInstance() { |
| 19 | assertEquals(Unsafe.instance, Unsafe.getDeclaredStaticField(sun.misc.Unsafe.class, "theUnsafe")); |
| 20 | } |
| 21 | |
| 22 | @Test(expected = RuntimeException.class) |
| 23 | public void getDeclaredStaticField() { |
| 24 | Unsafe.getDeclaredStaticField(TestClass.class, "wrongField"); |
| 25 | } |
| 26 | |
| 27 | @Test |
| 28 | public void objectFieldOffset() throws NoSuchFieldException, SecurityException { |
| 29 | long actual = Unsafe.objectFieldOffset(TestClass.class, "field"); |
| 30 | long expected = Unsafe.instance.objectFieldOffset(TestClass.class.getDeclaredField("field")); |
| 31 | assertEquals(actual, expected); |
| 32 | } |
| 33 | |
| 34 | @Test(expected = RuntimeException.class) |
| 35 | public void objectFieldOffsetException() { |
| 36 | Unsafe.objectFieldOffset(TestClass.class, "wrongField"); |
| 37 | } |
| 38 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…