use of sun.misc.Unsafe in project openj9 by eclipse.
the class Test_UnsafeFence method testLongFence.
/**
* @tests sun.misc.Unsafe.getAndAddLong()
* @tests sun.misc.Unsafe.loadFence()
* @tests sun.misc.Unsafe.storeFence()
* @tests sun.misc.Unsafe.fullFence()
*/
@Test
public void testLongFence() {
Unsafe unsafe = getUnsafe();
Field intFieldReflect;
Field longFieldReflect;
try {
longFieldReflect = getClass().getDeclaredField("longField");
long longFieldOffset = unsafe.objectFieldOffset(longFieldReflect);
AssertJUnit.assertEquals(unsafe.getAndAddLong(this, longFieldOffset, 2), 0);
AssertJUnit.assertEquals(unsafe.getAndAddLong(this, longFieldOffset, 3), 2);
AssertJUnit.assertEquals(unsafe.getAndSetLong(this, longFieldOffset, 7), 5);
AssertJUnit.assertEquals(unsafe.getAndAddLong(this, longFieldOffset, -3), 7);
AssertJUnit.assertEquals(unsafe.getAndSetLong(this, longFieldOffset, -9), 4);
AssertJUnit.assertEquals(longField, -9);
unsafe.loadFence();
AssertJUnit.assertEquals(longField, -9);
unsafe.storeFence();
AssertJUnit.assertEquals(longField, -9);
unsafe.fullFence();
AssertJUnit.assertEquals(longField, -9);
} catch (NoSuchFieldException e) {
Assert.fail("Missing field");
} catch (SecurityException e) {
Assert.fail("SecurityException: " + e.getMessage());
}
}
use of sun.misc.Unsafe in project openj9 by eclipse.
the class FrameIteratorTestGenerator method testFrameIteratorSkipAnnotationForNonBootstrapClass.
@Test
public void testFrameIteratorSkipAnnotationForNonBootstrapClass() throws Throwable {
final byte[] classBytes = FrameIteratorTestGenerator.dump();
/* Use reflect to get Unsafe so we can avoid the 'must be bootstrap' check */
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
Unsafe unsafe = (Unsafe) theUnsafe.get(null);
/* Force the class to be loaded in the Extension Loader so we can use sun.reflect.Reflection.getCallerClass() */
ClassLoader extLoader = Cmvc194781.class.getClassLoader().getParent();
Class<?> test = unsafe.defineClass("FrameIteratorTest", classBytes, 0, classBytes.length, extLoader, Cmvc194781.class.getProtectionDomain());
PrivilegedAction<Class<?>> instance = (PrivilegedAction<Class<?>>) test.newInstance();
Class<?> caller = (Class<?>) instance.run();
AssertJUnit.assertEquals(Cmvc194781.class, caller);
}
use of sun.misc.Unsafe in project openj9 by eclipse.
the class main method main.
public static void main(String[] args) throws Throwable {
Unsafe u = Unsafe.getUnsafe();
long offset;
Class cl = Class.forName(args[0]);
int alignment = com.ibm.oti.vm.VM.FJ9OBJECT_SIZE;
try {
offset = u.objectFieldOffset(cl.getDeclaredField("i"));
System.out.println(offset);
if (offset % 4 != 0) {
System.out.println("error: int is not aligned");
}
} catch (Exception e) {
}
try {
offset = u.objectFieldOffset(cl.getDeclaredField("l"));
System.out.println(offset);
if (offset % 8 != 0) {
System.out.println("error: long is not aligned");
}
} catch (Exception e) {
}
try {
offset = u.objectFieldOffset(cl.getDeclaredField("o"));
System.out.println(offset);
if (offset % alignment != 0) {
System.out.println("error: object is not aligned");
}
} catch (Exception e) {
}
try {
offset = u.objectFieldOffset(cl.getDeclaredField("d"));
System.out.println(offset);
if (offset % 8 != 0) {
System.out.println("error: double is not aligned");
}
} catch (Exception e) {
}
}
use of sun.misc.Unsafe in project lombok by rzwitserloot.
the class LombokProcessor method addOpensForLombok.
/**
* Useful from jdk9 and up; required from jdk16 and up. This code is supposed to gracefully do nothing on jdk8 and below, as this operation isn't needed there.
*/
public static void addOpensForLombok() {
Class<?> cModule;
try {
cModule = Class.forName("java.lang.Module");
} catch (ClassNotFoundException e) {
// jdk8-; this is not needed.
return;
}
Unsafe unsafe = getUnsafe();
Object jdkCompilerModule = getJdkCompilerModule();
Object ownModule = getOwnModule();
String[] allPkgs = { "com.sun.tools.javac.code", "com.sun.tools.javac.comp", "com.sun.tools.javac.file", "com.sun.tools.javac.main", "com.sun.tools.javac.model", "com.sun.tools.javac.parser", "com.sun.tools.javac.processing", "com.sun.tools.javac.tree", "com.sun.tools.javac.util", "com.sun.tools.javac.jvm" };
try {
Method m = cModule.getDeclaredMethod("implAddOpens", String.class, cModule);
long firstFieldOffset = getFirstFieldOffset(unsafe);
unsafe.putBooleanVolatile(m, firstFieldOffset, true);
for (String p : allPkgs) m.invoke(jdkCompilerModule, p, ownModule);
} catch (Exception ignore) {
}
}
use of sun.misc.Unsafe in project carbondata by apache.
the class CarbondataConnectorFactory method addHiveStorageFormatsForCarbondata.
private static void addHiveStorageFormatsForCarbondata(String storedAs) throws Exception {
Constructor<?> constructor = Unsafe.class.getDeclaredConstructors()[0];
constructor.setAccessible(true);
Unsafe unsafe = (Unsafe) constructor.newInstance();
HiveStorageFormat enumValue = (HiveStorageFormat) unsafe.allocateInstance(HiveStorageFormat.class);
Field nameField = Enum.class.getDeclaredField("name");
makeAccessible(nameField);
nameField.set(enumValue, storedAs);
Field ordinalField = Enum.class.getDeclaredField("ordinal");
makeAccessible(ordinalField);
ordinalField.setInt(enumValue, HiveStorageFormat.values().length);
Field serdeField = HiveStorageFormat.class.getDeclaredField("serde");
makeAccessible(serdeField);
serdeField.set(enumValue, CarbonHiveSerDe.class.getName());
Field inputFormatField = HiveStorageFormat.class.getDeclaredField("inputFormat");
makeAccessible(inputFormatField);
inputFormatField.set(enumValue, MapredCarbonInputFormat.class.getName());
Field outputFormatField = HiveStorageFormat.class.getDeclaredField("outputFormat");
makeAccessible(outputFormatField);
outputFormatField.set(enumValue, MapredCarbonOutputFormat.class.getName());
Field estimatedWriterSystemMemoryUsageField = HiveStorageFormat.class.getDeclaredField("estimatedWriterSystemMemoryUsage");
makeAccessible(estimatedWriterSystemMemoryUsageField);
estimatedWriterSystemMemoryUsageField.set(enumValue, new DataSize((long) 256, MEGABYTE));
Field values = HiveStorageFormat.class.getDeclaredField("$VALUES");
makeAccessible(values);
HiveStorageFormat[] hiveStorageFormats = new HiveStorageFormat[HiveStorageFormat.values().length + 1];
HiveStorageFormat[] src = (HiveStorageFormat[]) values.get(null);
System.arraycopy(src, 0, hiveStorageFormats, 0, src.length);
hiveStorageFormats[src.length] = enumValue;
values.set(null, hiveStorageFormats);
}
Aggregations