Search in sources :

Example 1 with CHECKCAST

use of net.runelite.asm.attributes.code.InstructionType.CHECKCAST in project runelite by runelite.

the class InjectSetterTest method testInjectSetterInt.

@Test
public void testInjectSetterInt() throws NoSuchMethodException {
    Inject inject = mock(Inject.class);
    when(inject.findImportMethodOnApi(any(Class.class), anyString(), any(Boolean.class))).thenReturn(APIClass.class.getDeclaredMethod("setTest", int.class));
    when(inject.createLoadForTypeIndex(any(Instructions.class), any(Type.class), anyInt())).thenReturn(mock(Instruction.class));
    InjectSetter instance = new InjectSetter(inject);
    ClassFile targetClass = new ClassFile();
    targetClass.setName("test");
    Field field = new Field(targetClass, "test", Type.INT);
    targetClass.addField(field);
    instance.injectSetter(targetClass, APIClass.class, field, "test", null);
    Method injectedMethod = targetClass.findMethod("setTest");
    assertNotNull(injectedMethod);
    Code code = injectedMethod.getCode();
    Instructions instructions = code.getInstructions();
    assertFalse(instructions.getInstructions().stream().filter(i -> i.getType() == CHECKCAST).findAny().isPresent());
}
Also used : Assert.assertNotNull(org.junit.Assert.assertNotNull) Field(net.runelite.asm.Field) Assert.assertTrue(org.junit.Assert.assertTrue) Code(net.runelite.asm.attributes.Code) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Type(net.runelite.asm.Type) Matchers.anyString(org.mockito.Matchers.anyString) Matchers.any(org.mockito.Matchers.any) ClassFile(net.runelite.asm.ClassFile) CHECKCAST(net.runelite.asm.attributes.code.InstructionType.CHECKCAST) Method(net.runelite.asm.Method) Assert.assertFalse(org.junit.Assert.assertFalse) Instructions(net.runelite.asm.attributes.code.Instructions) Matchers.anyInt(org.mockito.Matchers.anyInt) Instruction(net.runelite.asm.attributes.code.Instruction) Mockito.mock(org.mockito.Mockito.mock) ClassFile(net.runelite.asm.ClassFile) Instructions(net.runelite.asm.attributes.code.Instructions) Method(net.runelite.asm.Method) Instruction(net.runelite.asm.attributes.code.Instruction) Code(net.runelite.asm.attributes.Code) Field(net.runelite.asm.Field) Type(net.runelite.asm.Type) Test(org.junit.Test)

Example 2 with CHECKCAST

use of net.runelite.asm.attributes.code.InstructionType.CHECKCAST in project runelite by runelite.

the class InjectSetterTest method testInjectSetterObject.

@Test
public void testInjectSetterObject() throws NoSuchMethodException {
    Inject inject = mock(Inject.class);
    when(inject.findImportMethodOnApi(any(Class.class), anyString(), any(Boolean.class))).thenReturn(APIClass.class.getDeclaredMethod("setTestObject", Object.class));
    when(inject.createLoadForTypeIndex(any(Instructions.class), any(Type.class), anyInt())).thenReturn(mock(Instruction.class));
    InjectSetter instance = new InjectSetter(inject);
    ClassFile targetClass = new ClassFile();
    targetClass.setName("test");
    Field field = new Field(targetClass, "testObject", Type.STRING);
    targetClass.addField(field);
    instance.injectSetter(targetClass, APIClass.class, field, "testObject", null);
    Method injectedMethod = targetClass.findMethod("setTestObject");
    assertNotNull(injectedMethod);
    Code code = injectedMethod.getCode();
    Instructions instructions = code.getInstructions();
    assertTrue(instructions.getInstructions().stream().filter(i -> i.getType() == CHECKCAST).findAny().isPresent());
}
Also used : Assert.assertNotNull(org.junit.Assert.assertNotNull) Field(net.runelite.asm.Field) Assert.assertTrue(org.junit.Assert.assertTrue) Code(net.runelite.asm.attributes.Code) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Type(net.runelite.asm.Type) Matchers.anyString(org.mockito.Matchers.anyString) Matchers.any(org.mockito.Matchers.any) ClassFile(net.runelite.asm.ClassFile) CHECKCAST(net.runelite.asm.attributes.code.InstructionType.CHECKCAST) Method(net.runelite.asm.Method) Assert.assertFalse(org.junit.Assert.assertFalse) Instructions(net.runelite.asm.attributes.code.Instructions) Matchers.anyInt(org.mockito.Matchers.anyInt) Instruction(net.runelite.asm.attributes.code.Instruction) Mockito.mock(org.mockito.Mockito.mock) ClassFile(net.runelite.asm.ClassFile) Instructions(net.runelite.asm.attributes.code.Instructions) Method(net.runelite.asm.Method) Instruction(net.runelite.asm.attributes.code.Instruction) Code(net.runelite.asm.attributes.Code) Field(net.runelite.asm.Field) Type(net.runelite.asm.Type) Test(org.junit.Test)

Aggregations

ClassFile (net.runelite.asm.ClassFile)2 Field (net.runelite.asm.Field)2 Method (net.runelite.asm.Method)2 Type (net.runelite.asm.Type)2 Code (net.runelite.asm.attributes.Code)2 Instruction (net.runelite.asm.attributes.code.Instruction)2 CHECKCAST (net.runelite.asm.attributes.code.InstructionType.CHECKCAST)2 Instructions (net.runelite.asm.attributes.code.Instructions)2 Assert.assertFalse (org.junit.Assert.assertFalse)2 Assert.assertNotNull (org.junit.Assert.assertNotNull)2 Assert.assertTrue (org.junit.Assert.assertTrue)2 Test (org.junit.Test)2 Matchers.any (org.mockito.Matchers.any)2 Matchers.anyInt (org.mockito.Matchers.anyInt)2 Matchers.anyString (org.mockito.Matchers.anyString)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.when (org.mockito.Mockito.when)2