Search in sources :

Example 91 with MethodVisitor

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);
}
Also used : Implementation(net.bytebuddy.implementation.Implementation) MethodVisitor(org.objectweb.asm.MethodVisitor) Test(org.junit.Test)

Example 92 with MethodVisitor

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();
}
Also used : RefType(soot.RefType) ShortType(soot.ShortType) BooleanType(soot.BooleanType) SootTypeType(org.robovm.compiler.util.generic.SootTypeType) WildcardType(org.robovm.compiler.util.generic.WildcardType) SootMethodType(org.robovm.compiler.util.generic.SootMethodType) SootClassType(org.robovm.compiler.util.generic.SootClassType) ByteType(soot.ByteType) Type(org.robovm.compiler.util.generic.Type) DoubleType(soot.DoubleType) GenericArrayType(org.robovm.compiler.util.generic.GenericArrayType) FloatType(soot.FloatType) IntType(soot.IntType) ImplForType(org.robovm.compiler.util.generic.ImplForType) CharType(soot.CharType) LongType(soot.LongType) ParameterizedType(org.robovm.compiler.util.generic.ParameterizedType) PrimType(soot.PrimType) VoidType(soot.VoidType) ArrayList(java.util.ArrayList) PrimType(soot.PrimType) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 93 with MethodVisitor

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();
}
Also used : SootMethod(soot.SootMethod) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 94 with MethodVisitor

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();
}
Also used : MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 95 with MethodVisitor

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();
}
Also used : MethodVisitor(org.objectweb.asm.MethodVisitor)

Aggregations

MethodVisitor (org.objectweb.asm.MethodVisitor)630 Label (org.objectweb.asm.Label)186 ClassWriter (org.objectweb.asm.ClassWriter)116 Type (org.objectweb.asm.Type)59 ClassNode (org.codehaus.groovy.ast.ClassNode)57 FieldVisitor (org.objectweb.asm.FieldVisitor)56 ClassVisitor (org.objectweb.asm.ClassVisitor)47 ClassReader (org.objectweb.asm.ClassReader)43 ArrayList (java.util.ArrayList)32 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)30 Test (org.junit.Test)29 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)29 List (java.util.List)23 LinkedList (java.util.LinkedList)22 Parameter (org.codehaus.groovy.ast.Parameter)22 InterfaceHelperClassNode (org.codehaus.groovy.ast.InterfaceHelperClassNode)18 AsmClassGenerator (org.codehaus.groovy.classgen.AsmClassGenerator)18 BytecodeExpression (org.codehaus.groovy.classgen.BytecodeExpression)18 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)17 Method (java.lang.reflect.Method)16