Search in sources :

Example 1 with ReferencedDefinableClassLoader

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);
}
Also used : TestEventManager(org.spongepowered.common.test.TestEventManager) DefineableClassLoader(org.spongepowered.common.event.gen.DefineableClassLoader) PluginContainer(org.spongepowered.plugin.PluginContainer) Listener(org.spongepowered.api.event.Listener) ReferencedDefinableClassLoader(org.spongepowered.common.util.ReferencedDefinableClassLoader) EventManager(org.spongepowered.api.event.EventManager) TestEventManager(org.spongepowered.common.test.TestEventManager) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)1 ClassWriter (org.objectweb.asm.ClassWriter)1 MethodVisitor (org.objectweb.asm.MethodVisitor)1 EventManager (org.spongepowered.api.event.EventManager)1 Listener (org.spongepowered.api.event.Listener)1 DefineableClassLoader (org.spongepowered.common.event.gen.DefineableClassLoader)1 TestEventManager (org.spongepowered.common.test.TestEventManager)1 ReferencedDefinableClassLoader (org.spongepowered.common.util.ReferencedDefinableClassLoader)1 PluginContainer (org.spongepowered.plugin.PluginContainer)1