Java Code Examples for sun.misc.Unsafe#getAndSetObject()

The following examples show how to use sun.misc.Unsafe#getAndSetObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestSolutionDefaultConstructorInvocation.java    From java-katas with MIT License 5 votes vote down vote up
@Test
@Tag("PASSING")
@Order(2)
public void unsafeNoParamConstructor() {

    String expectedOutput = "[I am Unsafe.] - Default constructor via Unsafe";

    try {

        Field theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe");
        theUnsafeInstance.setAccessible(true);
        final Unsafe unsafe = (Unsafe) theUnsafeInstance.get(null);

        // Allocate instance does not go through initialization process, no constructor called.
        DemoClass demoClass = (DemoClass)
                unsafe.allocateInstance(DemoClass.class);

        // Get a handle to the "name" field of DemoClass
        Field nameFieldOfDemoClass = DemoClass.class.getDeclaredField("name");

        // Determine the memory offset location of the field in any instance of DemoClass.
        final long offset = unsafe.objectFieldOffset(nameFieldOfDemoClass);

        // Get the field for the DemoClass instance created above & set its value.
        unsafe.getAndSetObject(demoClass, offset, "I am Unsafe.");

        assertEquals(expectedOutput,
                demoClass.printStuff("Default constructor via Unsafe"),
                "Unsafe invocation failed");

    } catch (InstantiationException | IllegalAccessException | NoSuchFieldException e) {

        fail(UNSAFE_FAILURE.getValue() + e.getMessage());
    }
}
 
Example 2
Source File: TestKataDefaultConstructorInvocation.java    From java-katas with MIT License 5 votes vote down vote up
@Test
@Tag("PASSING")
@Order(2)
public void unsafeNoParamConstructor() {

    String expectedOutput = "[I am Unsafe.] - Default constructor via Unsafe";

    try {

        Field theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe");
        theUnsafeInstance.setAccessible(true);
        final Unsafe unsafe = (Unsafe) theUnsafeInstance.get(null);

        // Allocate instance does not go through initialization process, no constructor called.
        DemoClass demoClass = (DemoClass)
                unsafe.allocateInstance(DemoClass.class);

        // Get a handle to the "name" field of DemoClass
        Field nameFieldOfDemoClass = DemoClass.class.getDeclaredField("name");

        // Determine the memory offset location of the field in any instance of DemoClass.
        final long offset = unsafe.objectFieldOffset(nameFieldOfDemoClass);

        // Get the field for the DemoClass instance created above & set its value.
        unsafe.getAndSetObject(demoClass, offset, "I am Unsafe.");

        assertEquals(expectedOutput,
                demoClass.printStuff("Default constructor via Unsafe"),
                "Unsafe invocation failed");

    } catch (InstantiationException | IllegalAccessException | NoSuchFieldException e) {

        fail(UNSAFE_FAILURE.getValue() + e.getMessage());
    }
}