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

The following examples show how to use sun.misc.Unsafe#allocateInstance() . 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: JStructPlugin.java    From junion with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void wrapCompiler(Context c) throws Exception {
	Context.Key<JavaCompiler> compilerKey = read(JavaCompiler.class, "compilerKey");
	JavaCompiler comp = c.get(compilerKey);
	
	Unsafe u = getUnsafe();
	JavaCompilerWrapper jcw = (JavaCompilerWrapper)u.allocateInstance(JavaCompilerWrapper.class);
	//now gotta copy all the nonstatic fields
	Class jc = JavaCompiler.class;
	Field[] fs = jc.getDeclaredFields();
	for(Field f : fs) {
		if(!Modifier.isStatic(f.getModifiers())) {
			f.setAccessible(true);
			f.set(jcw, f.get(comp));
		}
	}
	c.put(compilerKey, (JavaCompiler)null);
	c.put(compilerKey, (JavaCompiler)jcw);
}
 
Example 2
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 3
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());
    }
}
 
Example 4
Source File: JavassistProxyTest.java    From gadtry with Apache License 2.0 5 votes vote down vote up
@Test
public void proxyHashMapTest()
        throws InstantiationException
{
    Unsafe unsafe = UnsafeHelper.getUnsafe();
    Class<?> aClass = JavassistProxy.getProxyClass(null, HashMap.class);
    Object obj = unsafe.allocateInstance(aClass);

    Assert.assertEquals(true, HashMap.class.isInstance(obj));
}
 
Example 5
Source File: InstantiationAndConstructorTest.java    From JavaBase with MIT License 5 votes vote down vote up
@Test
public void testInitByUnsafe() throws Exception {
  Field field = Unsafe.class.getDeclaredField("theUnsafe");
  field.setAccessible(true);
  Unsafe unsafe = (Unsafe) field.get(null);
  InstantiationAndConstructor instance = (InstantiationAndConstructor) unsafe
      .allocateInstance(InstantiationAndConstructor.class);
  assertThat(instance.getName(), equalTo(null));
  instance.setName("name");
  assertThat(instance.getName(), equalTo("name"));
}