本文整理了Java中sun.misc.Unsafe.defineAnonymousClass()
方法的一些代码示例,展示了Unsafe.defineAnonymousClass()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Unsafe.defineAnonymousClass()
方法的具体详情如下:
包路径:sun.misc.Unsafe
类名称: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:
代码示例来源: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);
内容来源于网络,如有侵权,请联系作者删除!