Search in sources :

Example 1 with MethodVisitor

use of org.objectweb.asm.MethodVisitor in project groovy by apache.

the class DgmConverter method createInvokeMethod.

private static void createInvokeMethod(CachedMethod method, ClassWriter cw, Class returnType, String methodDescriptor) {
    MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC, "invoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 1);
    BytecodeHelper.doCast(mv, method.getParameterTypes()[0].getTheClass());
    loadParameters(method, 2, mv);
    mv.visitMethodInsn(INVOKESTATIC, BytecodeHelper.getClassInternalName(method.getDeclaringClass().getTheClass()), method.getName(), methodDescriptor, false);
    BytecodeHelper.box(mv, returnType);
    if (method.getReturnType() == void.class) {
        mv.visitInsn(ACONST_NULL);
    }
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 2 with MethodVisitor

use of org.objectweb.asm.MethodVisitor in project groovy by apache.

the class DgmConverter method createConstructor.

private static void createConstructor(ClassWriter cw) {
    MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(Ljava/lang/String;Lorg/codehaus/groovy/reflection/CachedClass;Ljava/lang/Class;[Ljava/lang/Class;)V", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitVarInsn(ALOAD, 3);
    mv.visitVarInsn(ALOAD, 4);
    mv.visitMethodInsn(INVOKESPECIAL, "org/codehaus/groovy/reflection/GeneratedMetaMethod", "<init>", "(Ljava/lang/String;Lorg/codehaus/groovy/reflection/CachedClass;Ljava/lang/Class;[Ljava/lang/Class;)V", false);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 3 with MethodVisitor

use of org.objectweb.asm.MethodVisitor in project groovy by apache.

the class DgmConverter method createDoMethodInvokeMethod.

private static void createDoMethodInvokeMethod(CachedMethod method, ClassWriter cw, String className, Class returnType, String methodDescriptor) {
    MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "doMethodInvoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", null, null);
    mv.visitCode();
    if (method.getParamsCount() == 2 && method.getParameterTypes()[0].isNumber && method.getParameterTypes()[1].isNumber) {
        mv.visitVarInsn(ALOAD, 1);
        BytecodeHelper.doCast(mv, method.getParameterTypes()[0].getTheClass());
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, className, "getParameterTypes", "()[Lorg/codehaus/groovy/reflection/CachedClass;", false);
        mv.visitInsn(ICONST_0);
        mv.visitInsn(AALOAD);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitInsn(ICONST_0);
        mv.visitInsn(AALOAD);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/codehaus/groovy/reflection/CachedClass", "coerceArgument", "(Ljava/lang/Object;)Ljava/lang/Object;", false);
        // cast argument to parameter class, inclusive unboxing
        // for methods with primitive types
        Class type = method.getParameterTypes()[1].getTheClass();
        BytecodeHelper.doCast(mv, type);
    } else {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEVIRTUAL, className, "coerceArgumentsToClasses", "([Ljava/lang/Object;)[Ljava/lang/Object;", false);
        mv.visitVarInsn(ASTORE, 2);
        mv.visitVarInsn(ALOAD, 1);
        BytecodeHelper.doCast(mv, method.getParameterTypes()[0].getTheClass());
        loadParameters(method, 2, mv);
    }
    mv.visitMethodInsn(INVOKESTATIC, BytecodeHelper.getClassInternalName(method.getDeclaringClass().getTheClass()), method.getName(), methodDescriptor, false);
    BytecodeHelper.box(mv, returnType);
    if (method.getReturnType() == void.class) {
        mv.visitInsn(ACONST_NULL);
    }
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : CachedClass(org.codehaus.groovy.reflection.CachedClass) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 4 with MethodVisitor

use of org.objectweb.asm.MethodVisitor in project groovy by apache.

the class AsmClassGenerator method visitGStringExpression.

public void visitGStringExpression(GStringExpression expression) {
    MethodVisitor mv = controller.getMethodVisitor();
    mv.visitTypeInsn(NEW, "org/codehaus/groovy/runtime/GStringImpl");
    mv.visitInsn(DUP);
    int size = expression.getValues().size();
    BytecodeHelper.pushConstant(mv, size);
    mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
    for (int i = 0; i < size; i++) {
        mv.visitInsn(DUP);
        BytecodeHelper.pushConstant(mv, i);
        expression.getValue(i).visit(this);
        controller.getOperandStack().box();
        mv.visitInsn(AASTORE);
    }
    controller.getOperandStack().remove(size);
    List strings = expression.getStrings();
    size = strings.size();
    BytecodeHelper.pushConstant(mv, size);
    mv.visitTypeInsn(ANEWARRAY, "java/lang/String");
    for (int i = 0; i < size; i++) {
        mv.visitInsn(DUP);
        BytecodeHelper.pushConstant(mv, i);
        controller.getOperandStack().pushConstant((ConstantExpression) strings.get(i));
        controller.getOperandStack().box();
        mv.visitInsn(AASTORE);
    }
    controller.getOperandStack().remove(size);
    mv.visitMethodInsn(INVOKESPECIAL, "org/codehaus/groovy/runtime/GStringImpl", "<init>", "([Ljava/lang/Object;[Ljava/lang/String;)V", false);
    controller.getOperandStack().push(ClassHelper.GSTRING_TYPE);
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 5 with MethodVisitor

use of org.objectweb.asm.MethodVisitor in project groovy by apache.

the class CompareIdentityExpression method visit.

@Override
public void visit(final GroovyCodeVisitor visitor) {
    if (visitor instanceof AsmClassGenerator) {
        AsmClassGenerator acg = (AsmClassGenerator) visitor;
        WriterController controller = acg.getController();
        controller.getTypeChooser().resolveType(leftExpression, controller.getClassNode());
        controller.getTypeChooser().resolveType(rightExpression, controller.getClassNode());
        MethodVisitor mv = controller.getMethodVisitor();
        leftExpression.visit(acg);
        controller.getOperandStack().box();
        rightExpression.visit(acg);
        controller.getOperandStack().box();
        Label l1 = new Label();
        mv.visitJumpInsn(IF_ACMPNE, l1);
        mv.visitInsn(ICONST_1);
        Label l2 = new Label();
        mv.visitJumpInsn(GOTO, l2);
        mv.visitLabel(l1);
        mv.visitInsn(ICONST_0);
        mv.visitLabel(l2);
        controller.getOperandStack().replace(ClassHelper.boolean_TYPE, 2);
    } else {
        super.visit(visitor);
    }
}
Also used : WriterController(org.codehaus.groovy.classgen.asm.WriterController) Label(org.objectweb.asm.Label) AsmClassGenerator(org.codehaus.groovy.classgen.AsmClassGenerator) 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