use of org.spongepowered.common.util.ReferencedDefinableClassLoader in project SpongeCommon by SpongePowered.
the class EventManagerRegistrationTest method successfulRegistrationWithAsmDefinedClass.
@Test
public void successfulRegistrationWithAsmDefinedClass() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
final EventManager eventManager = new TestEventManager();
final PluginContainer mock = Mockito.mock(PluginContainer.class);
final ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
writer.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "org/spongepowered/common/test/event/BombDummy", null, "java/lang/Object", null);
final MethodVisitor ctor = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
ctor.visitCode();
ctor.visitVarInsn(Opcodes.ALOAD, 0);
ctor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
ctor.visitInsn(Opcodes.RETURN);
ctor.visitMaxs(1, 1);
final MethodVisitor el = writer.visitMethod(Opcodes.ACC_PUBLIC, "onBlockChange", "(Lorg/spongepowered/api/event/block/ChangeBlockEvent;)V", null, null);
el.visitCode();
el.visitAnnotation(Type.getDescriptor(Listener.class), true);
el.visitInsn(Opcodes.RETURN);
final MethodVisitor bomb = writer.visitMethod(Opcodes.ACC_PUBLIC, "throwup", "(Lcom/example/doesnt/Exist;)V", null, null);
bomb.visitCode();
bomb.visitInsn(Opcodes.RETURN);
final DefineableClassLoader loader = new ReferencedDefinableClassLoader(this.getClass().getClassLoader());
final Class<?> clazz = loader.defineClass("org.spongepowered.common.test.event.BombDummy", writer.toByteArray());
final Object o = clazz.getConstructor().newInstance();
eventManager.registerListeners(mock, o);
}
Aggregations