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

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

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

Unsafe.staticFieldOffset介绍

[英]Report the location of a given field in the storage allocation of its class. Do not expect to perform any sort of arithmetic on this offset; it is just a cookie which is passed to the unsafe heap memory accessors.

Any given field will always have the same offset and base, and no two distinct fields of the same class will ever have the same offset and base.

As of 1.4.1, offsets for fields are represented as long values, although the Sun JVM does not use the most significant 32 bits. However, JVM implementations which store static fields at absolute addresses can use long offsets and null base pointers to express the field locations in a form usable by #getInt(Object,long). Therefore, code which will be ported to such JVMs on 64-bit platforms must preserve all bits of static field offsets.
[中]报告给定字段在其类的存储分配中的位置。不要期望对该偏移量执行任何类型的算术运算;它只是一个传递给不安全堆内存访问器的cookie。
任何给定字段都将始终具有相同的偏移量和基数,同一类的两个不同字段都不会具有相同的偏移量和基数。
从1.4.1开始,字段的偏移量表示为长值,尽管Sun JVM不使用最高有效32位。然而,以绝对地址存储静态字段的JVM实现可以使用长偏移量和空基指针,以#getInt(Object,long)可用的形式表示字段位置。因此,在64位平台上移植到此类JVM的代码必须保留所有静态字段偏移量。

代码示例

代码示例来源:origin: apache/ignite

/**
 * Returns static field offset.
 *
 * @param field Field.
 * @return Static field offset.
 */
public static long staticFieldOffset(Field field) {
  return UNSAFE.staticFieldOffset(field);
}

代码示例来源:origin: prestodb/presto

private static int computeOffset(Field field) {
  if (Modifier.isStatic(field.getModifiers())) {
    return (int) VMSupport.U.staticFieldOffset(field);
  } else {
    return (int) VMSupport.U.objectFieldOffset(field);
  }
}

代码示例来源:origin: org.projectlombok/lombok

@SuppressWarnings({"sunapi", "all"})
private void disableJava9SillyWarning() {
  // JVM9 complains about using reflection to access packages from a module that aren't exported. This makes no sense; the whole point of reflection
  // is to get past such issues. The only comment from the jigsaw team lead on this was some unspecified mumbling about security which makes no sense,
  // as the SecurityManager is invoked to check such things. Therefore this warning is a bug, so we shall patch java to fix it.
  
  try {
    Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
    theUnsafe.setAccessible(true);
    Unsafe u = (Unsafe) theUnsafe.get(null);
    
    Class<?> cls = Class.forName("jdk.internal.module.IllegalAccessLogger");
    Field logger = cls.getDeclaredField("logger");
    u.putObjectVolatile(cls, u.staticFieldOffset(logger), null);
  } catch (Throwable t) {
    // We shall ignore it; the effect of this code failing is that the user gets to see a warning they remove with various --add-opens magic.
  }
}

代码示例来源:origin: jphp-group/jphp

@SuppressWarnings("unchecked")
public static void disableWarning() {
  try {
    Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
    theUnsafe.setAccessible(true);
    Unsafe u = (Unsafe) theUnsafe.get(null);
    Class cls = Class.forName("jdk.internal.module.IllegalAccessLogger");
    Field logger = cls.getDeclaredField("logger");
    u.putObjectVolatile(cls, u.staticFieldOffset(logger), null);
  } catch (Exception e) {
    // ignore
  }
}

代码示例来源:origin: wildfly/wildfly

private static ObjectStreamField[] getDeclaredSerialPersistentFields(Class<?> clazz) {
  final Field field;
  try {
    field = clazz.getDeclaredField("serialPersistentFields");
  } catch (NoSuchFieldException e) {
    return null;
  }
  if (field == null) {
    return null;
  }
  final int requiredModifiers = Modifier.STATIC | Modifier.PRIVATE | Modifier.FINAL;
  if ((field.getModifiers() & requiredModifiers) != requiredModifiers) {
    return null;
  }
  try {
    return (ObjectStreamField[]) unsafe.getObject(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field));
  } catch (ClassCastException e) {
    return null;
  }
}

代码示例来源:origin: com.google.protobuf/protobuf-java

@Override
 public Object getStaticObject(Field field) {
  return getObject(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field));
 }
}

代码示例来源:origin: aaberg/sql2o

? theUnsafe.staticFieldOffset(field)
: theUnsafe.objectFieldOffset(field);

代码示例来源:origin: aaberg/sql2o

? theUnsafe.staticFieldOffset(field)
: theUnsafe.objectFieldOffset(field);

代码示例来源:origin: org.jboss.openjdk-orb/openjdk-orb-jdk9-supplement

/**
 * Returns the offset of a static field.
 */
public final long staticFieldOffset(Field f)
{
  return unsafe.staticFieldOffset( f ) ;
}

代码示例来源:origin: org.apache.ignite/ignite-core

/**
 * Returns static field offset.
 *
 * @param field Field.
 * @return Static field offset.
 */
public static long staticFieldOffset(Field field) {
  return UNSAFE.staticFieldOffset(field);
}

代码示例来源:origin: org.jboss.openjdk-orb/openjdk-orb

/**
 * Returns the offset of a static field.
 */
public final long staticFieldOffset(Field f)
{
  return unsafe.staticFieldOffset( f ) ;
}

代码示例来源:origin: org.openjdk.jol/jol-core

@Override
public long fieldOffset(Field field) {
  if (Modifier.isStatic(field.getModifiers())) {
    return U.staticFieldOffset(field);
  } else {
    return U.objectFieldOffset(field);
  }
}

代码示例来源:origin: org.apache.yoko/yoko-rmi-impl

public Field(java.lang.reflect.Field f) {
  if (Modifier.isStatic(f.getModifiers()))
    fieldID = unsafe.staticFieldOffset(f);
  else
    fieldID = unsafe.objectFieldOffset(f);
}

代码示例来源:origin: com.facebook.presto/presto-jdbc

private static int computeOffset(Field field) {
  if (Modifier.isStatic(field.getModifiers())) {
    return (int) VMSupport.U.staticFieldOffset(field);
  } else {
    return (int) VMSupport.U.objectFieldOffset(field);
  }
}

代码示例来源:origin: OpenHFT/Java-Lang

private static long offset(Field field) {
    return isStatic(field) ?
        UNSAFE.staticFieldOffset(field) :
        UNSAFE.objectFieldOffset(field);
  }
}

代码示例来源:origin: io.fastjson/boon

protected UnsafeField ( Field field  )  {
  super(field);
  if ( super.isStatic() ) {
    base = unsafe.staticFieldBase( field );
    offset = unsafe.staticFieldOffset( field );
  } else {
    offset = unsafe.objectFieldOffset( field );
    base = null;
  }
  this.field = field;
}

代码示例来源:origin: boonproject/boon

protected UnsafeField ( Field field  )  {
  super(field);
  if ( super.isStatic() ) {
    base = unsafe.staticFieldBase( field );
    offset = unsafe.staticFieldOffset( field );
  } else {
    offset = unsafe.objectFieldOffset( field );
    base = null;
  }
  this.field = field;
}

代码示例来源:origin: boonproject/boon

protected UnsafeField ( Field field  )  {
  super(field);
  if ( super.isStatic() ) {
    base = unsafe.staticFieldBase( field );
    offset = unsafe.staticFieldOffset( field );
  } else {
    offset = unsafe.objectFieldOffset( field );
    base = null;
  }
  this.field = field;
}

代码示例来源:origin: com.github.os72/protobuf-java-shaded-351

@Override
 public Object getStaticObject(Field field) {
  return getObject(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field));
 }
}

代码示例来源:origin: io.bitsensor/proto

@Override
 public Object getStaticObject(Field field) {
  return getObject(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field));
 }
}

相关文章

Unsafe类方法