sun.misc.Unsafe.shouldBeInitialized()方法的使用及代码示例

x33g5p2x  于2022-01-31 转载在 其他  
字(3.0k)|赞(0)|评价(0)|浏览(159)

本文整理了Java中sun.misc.Unsafe.shouldBeInitialized()方法的一些代码示例,展示了Unsafe.shouldBeInitialized()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Unsafe.shouldBeInitialized()方法的具体详情如下:
包路径:sun.misc.Unsafe
类名称:Unsafe
方法名:shouldBeInitialized

Unsafe.shouldBeInitialized介绍

[英]Detect if the given class may need to be initialized. This is often needed in conjunction with obtaining the static field base of a class.
[中]检测给定类是否需要初始化。这通常需要与获取类的静态字段基结合使用。

代码示例

代码示例来源:origin: com.oracle.substratevm/svm

private void checkDelayedInitialization() {
  /*
   * We check all registered classes here, regardless if the AnalysisType got actually marked
   * as used. Class initialization can have side effects on other classes without the class
   * being used itself, e.g., a class initializer can write a static field in another class.
   */
  for (Map.Entry<Class<?>, InitKind> entry : classInitKinds.entrySet()) {
    if (entry.getValue() == InitKind.DELAY && !UnsafeAccess.UNSAFE.shouldBeInitialized(entry.getKey())) {
      throw UserError.abort("Class that is marked for delaying initialization to run time got initialized during image building: " + entry.getKey().getTypeName());
    }
  }
}

代码示例来源: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;
  }
}

代码示例来源:origin: com.oracle.substratevm/svm

@Override
public void delayClassInitialization(Class<?>[] classes) {
  for (Class<?> clazz : classes) {
    checkEagerInitialization(clazz);
    if (!UnsafeAccess.UNSAFE.shouldBeInitialized(clazz)) {
      throw UserError.abort("Class is already initialized, so it is too late to register delaying class initialization: " + clazz.getTypeName());
    }
    /*
     * Propagate possible existing RERUN registration from a superclass, so that we can
     * check for user errors below.
     */
    computeInitKindAndMaybeInitializeClass(clazz, false);
    InitKind previousKind = classInitKinds.put(clazz, InitKind.DELAY);
    if (previousKind == InitKind.EAGER) {
      throw UserError.abort("Class is already initialized, so it is too late to register delaying class initialization: " + clazz.getTypeName());
    } else if (previousKind == InitKind.RERUN) {
      throw UserError.abort("Class is registered both for delaying and rerunning the class initializer: " + clazz.getTypeName());
    }
  }
}

相关文章

Unsafe类方法