use of org.objectweb.asm.MethodVisitor in project byte-buddy by raphw.
the class SerializedConstantTest method testSerialization.
@Test
public void testSerialization() throws Exception {
MethodVisitor methodVisitor = mock(MethodVisitor.class);
Implementation.Context implementationContext = mock(Implementation.Context.class);
SerializedConstant.of(FOO).apply(methodVisitor, implementationContext);
verify(methodVisitor).visitLdcInsn(contains(FOO));
verifyZeroInteractions(implementationContext);
}
use of org.objectweb.asm.MethodVisitor in project robovm by robovm.
the class ObjCBlockPlugin method generateTargetMethod.
private void generateTargetMethod(String owner, SootMethod targetMethod, Type[] actualGenericTypes, soot.Type[] actualRawTypes, soot.Type[] unboxedTypes, Set<String> usedBoxMethods, Set<String> usedUnboxMethods, ClassWriter cw) {
String name = targetMethod.getName();
String signature = getGenericSignature(Arrays.asList(actualGenericTypes).subList(1, actualGenericTypes.length), actualGenericTypes[0]);
String desc = getDescriptor(targetMethod);
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, name, desc, signature, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, owner, "objCBlock", "L" + getInternalName(org_robovm_objc_ObjCBlock) + ";");
mv.visitMethodInsn(INVOKEVIRTUAL, getInternalName(org_robovm_objc_ObjCBlock), "invoke", "()J");
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, owner, "objCBlock", "L" + getInternalName(org_robovm_objc_ObjCBlock) + ";");
for (int i = 1, var = 1; i < actualRawTypes.length; i++, var++) {
soot.Type from = actualRawTypes[i];
if (from == LongType.v()) {
mv.visitVarInsn(LLOAD, var);
// longs need 2 slots
var++;
} else if (from == FloatType.v()) {
mv.visitVarInsn(FLOAD, var);
} else if (from == DoubleType.v()) {
mv.visitVarInsn(DLOAD, var);
// doubles need 2 slots
var++;
} else if (from instanceof PrimType) {
// boolean, byte, short, char and int are loaded using ILOAD
mv.visitVarInsn(ILOAD, var);
} else {
// Reference
mv.visitVarInsn(ALOAD, var);
}
soot.Type to = unboxedTypes[i];
if (from != to) {
mv.visitTypeInsn(CHECKCAST, getInternalName(from));
// Unbox the value on the top of the stack.
String unboxDesc = getDescriptor(Collections.singletonList(from), to);
usedUnboxMethods.add(unboxDesc);
mv.visitMethodInsn(INVOKESTATIC, owner, "unbox", unboxDesc);
}
}
// Now the function pointer, block and all arguments are on the stack
// (unboxed if needed). Call the invoke() bridge method.
List<soot.Type> paramTypes = new ArrayList<>();
paramTypes.add(LongType.v());
paramTypes.add(org_robovm_objc_ObjCBlock.getType());
paramTypes.addAll(Arrays.asList(unboxedTypes).subList(1, unboxedTypes.length));
mv.visitMethodInsn(INVOKESTATIC, owner, "invoke", getDescriptor(paramTypes, unboxedTypes[0]));
if (unboxedTypes[0] != actualRawTypes[0]) {
// Box the value on the top of the stack.
String boxDesc = getDescriptor(Collections.singletonList(unboxedTypes[0]), actualRawTypes[0]);
usedBoxMethods.add(boxDesc);
mv.visitMethodInsn(INVOKESTATIC, owner, "box", boxDesc);
}
if (actualRawTypes[0] == VoidType.v()) {
mv.visitInsn(RETURN);
} else if (actualRawTypes[0] == LongType.v()) {
mv.visitInsn(LRETURN);
} else if (actualRawTypes[0] == FloatType.v()) {
mv.visitInsn(FRETURN);
} else if (actualRawTypes[0] == DoubleType.v()) {
mv.visitInsn(DRETURN);
} else if (actualRawTypes[0] instanceof PrimType) {
mv.visitInsn(IRETURN);
} else {
mv.visitInsn(ARETURN);
}
mv.visitMaxs(0, 0);
mv.visitEnd();
}
use of org.objectweb.asm.MethodVisitor in project robovm by robovm.
the class AnnotationImplPlugin method generateHashCodeMethod.
private void generateHashCodeMethod(Clazz clazz, ClassWriter cw) {
String implName = clazz.getInternalName() + IMPL_CLASS_NAME_SUFFIX;
// int hashCode();
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "hashCode", "()I", null, null);
mv.visitCode();
mv.visitInsn(ICONST_0);
for (SootMethod method : clazz.getSootClass().getMethods()) {
String fieldName = getFieldName(method);
mv.visitVarInsn(ALOAD, 0);
mv.visitInsn(DUP);
mv.visitFieldInsn(GETFIELD, implName, fieldName, "Ljava/lang/Object;");
mv.visitLdcInsn(method.getName());
mv.visitMethodInsn(INVOKESPECIAL, BASE_CLASS, "hash", "(Ljava/lang/Object;Ljava/lang/String;)I");
mv.visitInsn(IADD);
}
mv.visitInsn(IRETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
use of org.objectweb.asm.MethodVisitor in project robovm by robovm.
the class AnnotationImplPlugin method generateConstructor.
private void generateConstructor(Clazz clazz, ClassWriter cw) {
String implName = clazz.getInternalName() + IMPL_CLASS_NAME_SUFFIX;
// Default constructor
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitLdcInsn(Type.getObjectType(clazz.getInternalName()));
mv.visitMethodInsn(INVOKESPECIAL, BASE_CLASS, "<init>", "(Ljava/lang/Class;)V");
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, implName, "$setDefaults", "()V");
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
use of org.objectweb.asm.MethodVisitor in project robovm by robovm.
the class AnnotationImplPlugin method generateFactoryMethod.
private void generateFactoryMethod(Clazz clazz, ClassWriter cw) throws IOException {
String implName = clazz.getInternalName() + IMPL_CLASS_NAME_SUFFIX;
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "$create", "()Ljava/lang/Object;", null, null);
mv.visitCode();
if (clazz.getSootClass().getMethodCount() == 0) {
// This annotation has no members. Just call $createSingleton().
mv.visitMethodInsn(INVOKESTATIC, implName, "$createSingleton", "()Ljava/lang/Object;");
} else {
mv.visitTypeInsn(NEW, implName);
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, implName, "<init>", "()V");
}
mv.visitInsn(ARETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
Aggregations