本文整理了Java中sun.misc.Unsafe.ensureClassInitialized()
方法的一些代码示例,展示了Unsafe.ensureClassInitialized()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Unsafe.ensureClassInitialized()
方法的具体详情如下:
包路径:sun.misc.Unsafe
类名称:Unsafe
方法名:ensureClassInitialized
[英]Ensure the given class has been initialized. This is often needed in conjunction with obtaining the static field base of a class.
[中]确保给定的类已初始化。这通常需要与获取类的静态字段基结合使用。
代码示例来源:origin: btraceio/btrace
private Class defineClassImpl(byte[] code, boolean mustBeBootstrap) {
ClassLoader loader = null;
if (! mustBeBootstrap) {
loader = new ClassLoader(null) {};
}
Class cl = unsafe.defineClass(className, code, 0, code.length, loader, null);
unsafe.ensureClassInitialized(cl);
return cl;
}
代码示例来源:origin: org.jboss.openjdk-orb/openjdk-orb-jdk9-supplement
public static JavaCorbaAccess getJavaCorbaAccess() {
if (javaCorbaAccess == null) {
// Ensure ValueUtility is initialized; we know that that class
// provides the shared secret
unsafe.ensureClassInitialized(ValueUtility.class);
}
return javaCorbaAccess;
}
代码示例来源:origin: org.jboss.openjdk-orb/openjdk-orb-jdk9-supplement
/**
* Ensure that the class has been initalized.
* @param cl the class to ensure is initialized
*/
public final void ensureClassInitialized(Class<?> cl) {
unsafe.ensureClassInitialized(cl);
}
代码示例来源:origin: com.tomitribe.tribestream/tribestream-metrics-bytecode
public static void ensureClassInitialized(final Class<?> aClass) {
UNSAFE.ensureClassInitialized(aClass);
}
代码示例来源:origin: org.jboss.openjdk-orb/openjdk-orb
/**
* Ensure that the class has been initalized.
* @param cl the class to ensure is initialized
*/
public final void ensureClassInitialized(Class<?> cl) {
unsafe.ensureClassInitialized(cl);
}
代码示例来源:origin: jbachorik/btrace2
private Class defineClassImpl(byte[] code, boolean mustBeBootstrap) {
ClassLoader loader = null;
if (!mustBeBootstrap) {
loader = new ClassLoader(null) {
};
}
Class cl = unsafe.defineClass(className, code, 0, code.length, loader, null);
unsafe.ensureClassInitialized(cl);
return cl;
}
代码示例来源:origin: org.gridkit.3rd.btrace/runtime
private Class defineClassImpl(byte[] code, boolean mustBeBootstrap) {
ClassLoader loader = null;
if (!mustBeBootstrap) {
loader = new ClassLoader(null) {
};
}
Class cl = unsafe.defineClass(className, code, 0, code.length, loader, null);
unsafe.ensureClassInitialized(cl);
return cl;
}
代码示例来源:origin: org.jboss.windup.decompiler.procyon/procyon-core
public static void ensureClassInitialized(final Class<?> clazz) {
getUnsafeInstance().ensureClassInitialized(VerifyArgument.notNull(clazz, "clazz"));
}
代码示例来源:origin: org.jboss.windup.decompiler.procyon/windup-procyon-core
public static void ensureClassInitialized(final Class<?> clazz) {
getUnsafeInstance().ensureClassInitialized(VerifyArgument.notNull(clazz, "clazz"));
}
代码示例来源:origin: org.bitbucket.mstrobel/procyon-core
public static void ensureClassInitialized(final Class<?> clazz) {
getUnsafeInstance().ensureClassInitialized(VerifyArgument.notNull(clazz, "clazz"));
}
代码示例来源:origin: com.meterware.simplestub/simplestub
private static void setFieldValue(Class aClass, String fieldName, Object value) throws NoSuchFieldException {
unsafe.ensureClassInitialized(aClass);
Field field = aClass.getDeclaredField(fieldName);
if (!field.getType().isPrimitive())
unsafe.putObject(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field), value);
else if (field.getType().equals(boolean.class) && value instanceof Boolean)
unsafe.putBoolean(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field), (Boolean) value);
else if (field.getType().equals(char.class) && value instanceof Character)
unsafe.putChar(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field), ((Character) value));
else if (field.getType().equals(byte.class) && value instanceof Number)
unsafe.putByte(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field), ((Number) value).byteValue());
else if (field.getType().equals(short.class) && value instanceof Number)
unsafe.putShort(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field), ((Number) value).shortValue());
else if (field.getType().equals(int.class) && value instanceof Number)
unsafe.putInt(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field), ((Number) value).intValue());
else if (field.getType().equals(long.class) && value instanceof Number)
unsafe.putLong(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field), ((Number) value).longValue());
else if (field.getType().equals(float.class) && value instanceof Number)
unsafe.putFloat(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field), ((Number) value).floatValue());
else if (field.getType().equals(double.class) && value instanceof Number)
unsafe.putDouble(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field), ((Number) value).doubleValue());
else
throw new IllegalArgumentException(String.format("Can not set final static %s field %s.%s to %s", field.getType(), aClass.getName(), fieldName, value.getClass().getName()));
}
代码示例来源:origin: com.oracle.substratevm/svm
@Override
public void rerunClassInitialization(Class<?>[] classes) {
for (Class<?> clazz : classes) {
checkEagerInitialization(clazz);
try {
UnsafeAccess.UNSAFE.ensureClassInitialized(clazz);
} catch (Throwable ex) {
throw UserError.abort("Class initialization failed: " + clazz.getTypeName(), ex);
}
/*
* Propagate possible existing DELAY registration from a superclass, so that we can
* check for user errors below.
*/
computeInitKindAndMaybeInitializeClass(clazz, false);
InitKind previousKind = classInitKinds.put(clazz, InitKind.RERUN);
if (previousKind == InitKind.EAGER) {
throw UserError.abort("The information that the class should be initialized during image building has already been used, " +
"so it is too late to register re-running the class initializer: " + clazz.getTypeName());
} else if (previousKind == InitKind.DELAY) {
throw UserError.abort("Class or a superclass is already registered for delaying the class initializer, " +
"so it is too late to register re-running the class initializer: " + clazz.getTypeName());
}
}
}
代码示例来源:origin: com.meterware.simplestub/simplestub
private static Object getFieldValue(Class aClass, String fieldName) throws NoSuchFieldException {
unsafe.ensureClassInitialized(aClass);
Field field = aClass.getDeclaredField(fieldName);
if (!field.getType().isPrimitive())
return unsafe.getObject(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field));
else if (field.getType().equals(boolean.class))
return unsafe.getBoolean(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field));
else if (field.getType().equals(char.class))
return unsafe.getChar(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field));
else if (field.getType().equals(byte.class))
return unsafe.getByte(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field));
else if (field.getType().equals(short.class))
return unsafe.getShort(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field));
else if (field.getType().equals(int.class))
return unsafe.getInt(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field));
else if (field.getType().equals(long.class))
return unsafe.getLong(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field));
else if (field.getType().equals(float.class))
return unsafe.getFloat(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field));
else if (field.getType().equals(double.class))
return unsafe.getDouble(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field));
else
throw new IllegalArgumentException(String.format("Can not get value for static %s field %s.%s", field.getType(), aClass.getName(), fieldName));
}
代码示例来源:origin: com.oracle.substratevm/svm
UnsafeAccess.UNSAFE.ensureClassInitialized(clazz);
return InitKind.EAGER;
代码示例来源:origin: org.graalvm.compiler/compiler
public static <T> NodeClass<T> get(Class<T> clazz) {
int numTries = 0;
while (true) {
boolean shouldBeInitializedBefore = UnsafeAccess.UNSAFE.shouldBeInitialized(clazz);
NodeClass<T> result = getUnchecked(clazz);
if (result != null || clazz == NODE_CLASS) {
return result;
}
/*
* GR-9537: We observed a transient problem with TYPE fields being null. Retry a couple
* of times and print something to the log so that we can gather more diagnostic
* information without failing gates.
*/
numTries++;
boolean shouldBeInitializedAfter = UnsafeAccess.UNSAFE.shouldBeInitialized(clazz);
String msg = "GR-9537 Reflective field access of TYPE field returned null. This is probably a bug in HotSpot class initialization. " +
" clazz: " + clazz.getTypeName() + ", numTries: " + numTries +
", shouldBeInitializedBefore: " + shouldBeInitializedBefore + ", shouldBeInitializedAfter: " + shouldBeInitializedAfter;
if (numTries <= 100) {
TTY.println(msg);
UnsafeAccess.UNSAFE.ensureClassInitialized(clazz);
} else {
throw GraalError.shouldNotReachHere(msg);
}
return result;
}
}
内容来源于网络,如有侵权,请联系作者删除!