Search in sources :

Example 51 with MethodVisitor

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

the class AsmClassGenerator method storeThisInstanceField.

private void storeThisInstanceField(FieldExpression expression) {
    MethodVisitor mv = controller.getMethodVisitor();
    FieldNode field = expression.getField();
    boolean setReferenceFromReference = field.isHolder() && expression.isUseReferenceDirectly();
    String ownerName = (field.getOwner().equals(controller.getClassNode())) ? controller.getInternalClassName() : BytecodeHelper.getClassInternalName(field.getOwner());
    OperandStack operandStack = controller.getOperandStack();
    if (setReferenceFromReference) {
        // rhs is ready to use reference, just put it in the field
        mv.visitVarInsn(ALOAD, 0);
        operandStack.push(controller.getClassNode());
        operandStack.swap();
        mv.visitFieldInsn(PUTFIELD, ownerName, field.getName(), BytecodeHelper.getTypeDescription(field.getType()));
    } else if (field.isHolder()) {
        // rhs is normal value, set the value in the Reference
        operandStack.doGroovyCast(field.getOriginType());
        operandStack.box();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, ownerName, expression.getFieldName(), BytecodeHelper.getTypeDescription(field.getType()));
        mv.visitInsn(SWAP);
        mv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/Reference", "set", "(Ljava/lang/Object;)V", false);
    } else {
        // rhs is normal value, set normal value
        operandStack.doGroovyCast(field.getOriginType());
        mv.visitVarInsn(ALOAD, 0);
        operandStack.push(controller.getClassNode());
        operandStack.swap();
        mv.visitFieldInsn(PUTFIELD, ownerName, field.getName(), BytecodeHelper.getTypeDescription(field.getType()));
    }
}
Also used : OperandStack(org.codehaus.groovy.classgen.asm.OperandStack) FieldNode(org.codehaus.groovy.ast.FieldNode) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 52 with MethodVisitor

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

the class AsmClassGenerator method onLineNumber.

public void onLineNumber(ASTNode statement, String message) {
    MethodVisitor mv = controller.getMethodVisitor();
    if (statement == null)
        return;
    int line = statement.getLineNumber();
    this.currentASTNode = statement;
    if (line < 0)
        return;
    if (!ASM_DEBUG && line == controller.getLineNumber())
        return;
    controller.setLineNumber(line);
    if (mv != null) {
        Label l = new Label();
        mv.visitLabel(l);
        mv.visitLineNumber(line, l);
    }
}
Also used : Label(org.objectweb.asm.Label) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 53 with MethodVisitor

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

the class AsmClassGenerator method visitClassExpression.

/**
     * load class object on stack
     */
public void visitClassExpression(ClassExpression expression) {
    ClassNode type = expression.getType();
    MethodVisitor mv = controller.getMethodVisitor();
    if (BytecodeHelper.isClassLiteralPossible(type) || BytecodeHelper.isSameCompilationUnit(controller.getClassNode(), type)) {
        if (controller.getClassNode().isInterface()) {
            InterfaceHelperClassNode interfaceClassLoadingClass = controller.getInterfaceClassLoadingClass();
            if (BytecodeHelper.isClassLiteralPossible(interfaceClassLoadingClass)) {
                BytecodeHelper.visitClassLiteral(mv, interfaceClassLoadingClass);
                controller.getOperandStack().push(ClassHelper.CLASS_Type);
                return;
            }
        } else {
            BytecodeHelper.visitClassLiteral(mv, type);
            controller.getOperandStack().push(ClassHelper.CLASS_Type);
            return;
        }
    }
    String staticFieldName = getStaticFieldName(type);
    referencedClasses.put(staticFieldName, type);
    String internalClassName = controller.getInternalClassName();
    if (controller.getClassNode().isInterface()) {
        internalClassName = BytecodeHelper.getClassInternalName(controller.getInterfaceClassLoadingClass());
        mv.visitFieldInsn(GETSTATIC, internalClassName, staticFieldName, "Ljava/lang/Class;");
    } else {
        mv.visitMethodInsn(INVOKESTATIC, internalClassName, "$get$" + staticFieldName, "()Ljava/lang/Class;", false);
    }
    controller.getOperandStack().push(ClassHelper.CLASS_Type);
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) InterfaceHelperClassNode(org.codehaus.groovy.ast.InterfaceHelperClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) InterfaceHelperClassNode(org.codehaus.groovy.ast.InterfaceHelperClassNode) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 54 with MethodVisitor

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

the class AsmClassGenerator method visitConstructorOrMethod.

protected void visitConstructorOrMethod(MethodNode node, boolean isConstructor) {
    controller.resetLineNumber();
    Parameter[] parameters = node.getParameters();
    String methodType = BytecodeHelper.getMethodDescriptor(node.getReturnType(), parameters);
    String signature = BytecodeHelper.getGenericsMethodSignature(node);
    int modifiers = node.getModifiers();
    if (isVargs(node.getParameters()))
        modifiers |= Opcodes.ACC_VARARGS;
    MethodVisitor mv = cv.visitMethod(modifiers, node.getName(), methodType, signature, buildExceptions(node.getExceptions()));
    controller.setMethodVisitor(mv);
    visitAnnotations(node, mv);
    for (int i = 0; i < parameters.length; i++) {
        visitParameterAnnotations(parameters[i], i, mv);
    }
    // Add parameter names to the MethodVisitor (jdk8+ only)
    if (getCompileUnit().getConfig().getParameters()) {
        for (int i = 0; i < parameters.length; i++) {
            // TODO handle ACC_SYNTHETIC for enum method parameters?
            mv.visitParameter(parameters[i].getName(), 0);
        }
    }
    if (controller.getClassNode().isAnnotationDefinition() && !node.isStaticConstructor()) {
        visitAnnotationDefault(node, mv);
    } else if (!node.isAbstract()) {
        Statement code = node.getCode();
        mv.visitCode();
        // fast path for getter/setters etc.
        if (code instanceof BytecodeSequence && ((BytecodeSequence) code).getInstructions().size() == 1 && ((BytecodeSequence) code).getInstructions().get(0) instanceof BytecodeInstruction) {
            ((BytecodeInstruction) ((BytecodeSequence) code).getInstructions().get(0)).visit(mv);
        } else {
            visitStdMethod(node, isConstructor, parameters, code);
        }
        //mv.visitInsn(NOP);
        try {
            mv.visitMaxs(0, 0);
        } catch (Exception e) {
            throw new GroovyRuntimeException("ASM reporting processing error for " + controller.getClassNode() + "#" + node.getName() + " with signature " + node.getTypeDescriptor() + " in " + sourceFile + ":" + node.getLineNumber(), e);
        }
    }
    mv.visitEnd();
}
Also used : CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) WhileStatement(org.codehaus.groovy.ast.stmt.WhileStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) DoWhileStatement(org.codehaus.groovy.ast.stmt.DoWhileStatement) ContinueStatement(org.codehaus.groovy.ast.stmt.ContinueStatement) BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) Parameter(org.codehaus.groovy.ast.Parameter) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) RuntimeParserException(org.codehaus.groovy.syntax.RuntimeParserException) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 55 with MethodVisitor

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

the class AsmClassGenerator method storeStaticField.

private void storeStaticField(FieldExpression expression) {
    MethodVisitor mv = controller.getMethodVisitor();
    FieldNode field = expression.getField();
    boolean holder = field.isHolder() && !controller.isInClosureConstructor();
    controller.getOperandStack().doGroovyCast(field);
    String ownerName = (field.getOwner().equals(controller.getClassNode())) ? controller.getInternalClassName() : BytecodeHelper.getClassInternalName(field.getOwner());
    if (holder) {
        controller.getOperandStack().box();
        mv.visitFieldInsn(GETSTATIC, ownerName, expression.getFieldName(), BytecodeHelper.getTypeDescription(field.getType()));
        mv.visitInsn(SWAP);
        mv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/Reference", "set", "(Ljava/lang/Object;)V", false);
    } else {
        mv.visitFieldInsn(PUTSTATIC, ownerName, expression.getFieldName(), BytecodeHelper.getTypeDescription(field.getType()));
    }
    controller.getOperandStack().remove(1);
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) 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