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

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

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

Unsafe.defineAnonymousClass介绍

[英]Define a class but do not make it known to the class loader or system dictionary.

For each CP entry, the corresponding CP patch must either be null or have the a format that matches its tag:

  • Integer, Long, Float, Double: the corresponding wrapper object type from java.lang
  • Utf8: a string (must have suitable syntax if used as signature or name)
  • Class: any java.lang.Class object
  • String: any object (not just a java.lang.String)
  • InterfaceMethodRef: (NYI) a method handle to invoke on that call site's arguments
    [中]定义一个类,但不要让类加载器或系统字典知道它。
    对于每个CP条目,相应的CP修补程序必须为空或具有与其标记匹配的a格式:
    *Integer、Long、Float、Double:java中相应的包装器对象类型。朗
    *Utf8:字符串(如果用作签名或名称,则必须具有合适的语法)
    *类别:任何java。lang.类对象
    *字符串:任何对象(不仅仅是java.lang.String)
    *InterfaceMethodRef:(NYI)在调用站点的参数上调用的方法句柄

代码示例

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

final Class<?> proxyClass = UNSAFE.defineAnonymousClass(targetClass, writer.toByteArray(), null);

代码示例来源:origin: anba/es6draft

Class<?> defineClass(String className, byte[] bytes) {
    return UNSAFE.defineAnonymousClass(hostClass, bytes, null);
  }
}

代码示例来源:origin: bluestreak01/questdb

@SuppressWarnings("unchecked")
public <T> Class<T> loadClass(Class<?> host) {
  byte[] b = new byte[position()];
  System.arraycopy(buf.array(), 0, b, 0, b.length);
  return (Class<T>) Unsafe.getUnsafe().defineAnonymousClass(host, b, null);
}

代码示例来源:origin: stackoverflow.com

// Generate subclass which extends existing Skill.class
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
String superClassName = Skill.class.getName().replace('.', '/');
cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER + Opcodes.ACC_FINAL
    + Opcodes.ACC_SYNTHETIC, "anonymous", null, superClassName, null);
// Create constructor which calls superclass constructor
MethodVisitor ctor = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
ctor.visitCode();
// load this
ctor.visitVarInsn(Opcodes.ALOAD, 0);
// call superclass constructor
ctor.visitMethodInsn(Opcodes.INVOKESPECIAL, superClassName, "<init>", "()V", false);
// return
ctor.visitInsn(Opcodes.RETURN);
ctor.visitMaxs(-1, -1);
ctor.visitEnd();
cw.visitEnd();

// Get the Unsafe object
Field field = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
sun.misc.Unsafe UNSAFE = (sun.misc.Unsafe) field.get(null);

// Create new anonymous class
Class<? extends Skill> clazz = UNSAFE.defineAnonymousClass(Skill.class,
    cw.toByteArray(), null).asSubclass(Skill.class);
// instantiate new class and call the `get()` method
clazz.newInstance().get();

代码示例来源:origin: org.robolectric/robolectric-sandbox

final Class<?> proxyClass = UNSAFE.defineAnonymousClass(targetClass, writer.toByteArray(), null);

相关文章

Unsafe类方法