use of net.runelite.asm.attributes.Code in project runelite by runelite.
the class ClassGroupFactory method generateGroup.
public static ClassGroup generateGroup() {
ClassGroup group = new ClassGroup();
ClassFile cf = new ClassFile(group);
cf.setName("test");
cf.setSuperName("java/lang/Object");
group.addClass(cf);
Field field = new Field(cf, "field", Type.INT);
field.setStatic();
cf.addField(field);
Method method = new Method(cf, "func", new Signature("()V"));
method.setStatic();
cf.addMethod(method);
Code code = new Code(method);
method.setCode(code);
{
method = new Method(cf, "func2", new Signature("(III)V"));
method.setStatic();
cf.addMethod(method);
code = new Code(method);
method.setCode(code);
Instructions ins = code.getInstructions();
ins.addInstruction(new VReturn(ins));
}
addVoidMethod(cf, "void1");
addVoidMethod(cf, "void2");
addVoidMethod(cf, "void3");
addVoidMethod(cf, "void4");
return group;
}
use of net.runelite.asm.attributes.Code in project runelite by runelite.
the class InjectGetter method injectGetter.
public void injectGetter(ClassFile clazz, java.lang.reflect.Method method, Field field, Number getter) {
assert clazz.findMethod(method.getName()) == null;
assert field.isStatic() || field.getClassFile() == clazz;
Signature sig = new Signature.Builder().setReturnType(Inject.classToType(method.getReturnType())).build();
Method getterMethod = new Method(clazz, method.getName(), sig);
getterMethod.setAccessFlags(ACC_PUBLIC);
// create code
Code code = new Code(getterMethod);
getterMethod.setCode(code);
Instructions instructions = code.getInstructions();
List<Instruction> ins = instructions.getInstructions();
if (field.isStatic()) {
code.setMaxStack(1);
ins.add(new GetStatic(instructions, field.getPoolField()));
} else {
code.setMaxStack(2);
ins.add(new ALoad(instructions, 0));
ins.add(new GetField(instructions, field.getPoolField()));
}
if (getter != null) {
code.setMaxStack(2);
assert getter instanceof Integer || getter instanceof Long;
if (getter instanceof Integer) {
ins.add(new LDC(instructions, (int) getter));
ins.add(new IMul(instructions));
} else {
ins.add(new LDC(instructions, (long) getter));
ins.add(new LMul(instructions));
}
}
InstructionType returnType;
if (field.getType().isPrimitive() && field.getType().getDimensions() == 0) {
switch(field.getType().toString()) {
case "B":
case "C":
case "I":
case "S":
case "Z":
returnType = InstructionType.IRETURN;
break;
case "D":
returnType = InstructionType.DRETURN;
break;
case "F":
returnType = InstructionType.FRETURN;
break;
case "J":
returnType = InstructionType.LRETURN;
break;
default:
throw new RuntimeException("Unknown type");
}
} else {
returnType = InstructionType.ARETURN;
}
ins.add(new Return(instructions, returnType));
clazz.addMethod(getterMethod);
++injectedGetters;
}
use of net.runelite.asm.attributes.Code 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());
}
use of net.runelite.asm.attributes.Code 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());
}
use of net.runelite.asm.attributes.Code in project runelite by runelite.
the class ClassGroup method lookup.
public void lookup() {
for (ClassFile cf : this.getClasses()) {
for (Method m : cf.getMethods()) {
Code code = m.getCode();
if (code == null) {
continue;
}
code.getInstructions().lookup();
}
}
}
Aggregations